Helix Systems' HR team is preparing a department directory for the employee handbook.
Write a query to return each department's name and office location, listed alphabetically by department name.
Assumptions:
- The
departmentstable contains every department at Helix Systems. - Department names are unique, so no tie-breaker is required.
Output:
- One row per department, with columns
nameandlocation, sorted bynameascending.
Schema · hr 4 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
name,
location
FROM
departments
ORDER BY
name The shape
A single ORDER BY name is enough — ascending is the default direction, and department names are unique, so no tiebreaker is needed to make the directory deterministic.
Clause by clause
SELECT name, locationreturns the two columns the handbook needs: the department name and where the office sits.FROM departmentsreads every department on file. There's no filter; the directory shows the full list.ORDER BY namesorts the rows alphabetically by department name. PostgreSQL applies the database's collation, which for the default locale means dictionary ordering — A before B, B before C, and so on. The clause stops there because the prompt confirms department names are unique; the moment the primary key can't tie, no secondary key is needed to break ties.
Why this and not ORDER BY name ASC
Both queries return the exact same rows in the exact same order. ASC is the default direction for every sort key, so writing it explicitly is a style choice, not a correctness one. The shorter form reads more naturally for ascending sorts; the explicit form is worth reaching for only when the query mixes directions and reading ASC alongside DESC keeps the intent clearer at a glance.
You practiced sorting text alphabetically with a single ORDER BY key. Ascending is the default, so ORDER BY name and ORDER BY name ASC produce identical results — the recurring shape behind any A-to-Z directory listing.