Sort by a SELECT Alias
Return first_name, last_name, full_name, and salary, ordered by the full_name alias ascending.
- Sorting
Exercise brief
Understand the request
Directory product manager A display-name export should use its readable SELECT alias as the sort key.
Build full_name = first_name + space + last_name and sort employees alphabetically by full_name. Show first_name, last_name, full_name, salary.
Return
- Build full_name from first_name and last_name.
- Reference full_name in ORDER BY.
Constraints
- Use a SELECT-list alias as the sort expression.
- Do not use an ordinal column position.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
employees
first_nameTEXTlast_nameTEXTsalaryDECIMAL
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
You can sort by an alias defined in the SELECT list on most engines (PostgreSQL, MySQL, SQLite). Strict standards require repeating the expression.
Hint 2
Concatenation is engine-specific: `||` (Postgres/SQLite/Oracle), `+` (SQL Server), `CONCAT(a, b, c)` (works EVERYWHERE).
Hint 3
ORDER BY full_name (or repeat the expression: ORDER BY first_name || ' ' || last_name)
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT first_name, last_name, (first_name || ' ' || last_name) AS full_name, salary FROM employees ORDER BY full_name;Why this works
Prefer `CONCAT()` when writing portable SQL. The `||` operator is treated as logical OR by MySQL with default settings — running this query on MySQL would return all-NULL full_name values. CONCAT() avoids the trap entirely.
Success check
Every employee is returned in ascending full-name order.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| first_name | last_name | full_name | salary |
|---|---|---|---|
| Alex | Miller | Alex Miller | 72000 |
| Amy | Taylor | Amy Taylor | 78000 |
| Chris | Anderson | Chris Anderson | 68000 |
| David | Brown | David Brown | 70000 |
| Emma | Thomas | Emma Thomas | 52000 |
| Jane | Smith | Jane Smith | 55000 |
| John | Doe | John Doe | 75000 |
| Lisa | Davis | Lisa Davis | 58000 |
| Mike | Johnson | Mike Johnson | 80000 |
| Rachel | Garcia | Rachel Garcia | 64000 |
Previewing 10 of 12 expected rows. Run the query in the editor to inspect the full result.
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
LIMIT & OFFSET
Practice deterministic top-N, cutoff ties, offset pagination, composite keyset cursors, and resumable bounded batches.
Ranking & NTH Value
Solve deterministic ranking, top-N, distribution, positional-frame, and rolling-window problems.
SELECT Statements
Select columns, filter rows, remove duplicates, and order query results.
Open the interactive workspace and practice across SQL topics.