ORDER BY & Sorting SQL Topic exerciseMediumVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

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_nameTEXT
  • last_nameTEXT
  • salaryDECIMAL

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_namelast_namefull_namesalary
AlexMillerAlex Miller72000
AmyTaylorAmy Taylor78000
ChrisAndersonChris Anderson68000
DavidBrownDavid Brown70000
EmmaThomasEmma Thomas52000
JaneSmithJane Smith55000
JohnDoeJohn Doe75000
LisaDavisLisa Davis58000
MikeJohnsonMike Johnson80000
RachelGarciaRachel Garcia64000

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

SQL Practice Online

Open the interactive workspace and practice across SQL topics.