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

Sort Salaries from Lowest to Highest

Return first_name, last_name, salary, and department_id for every employee, ordered by salary from lowest to highest.

  • Sorting

Exercise brief

Understand the request

Compensation operations analyst A salary review export must begin with the lowest current salary and remain reproducible.

List employees sorted by salary in ascending order (lowest first). Show first_name, last_name, salary, department_id.

Return

  • Return one row per employee.
  • Order the final result by salary ascending.

Constraints

  • Use an explicit ORDER BY salary ASC clause.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • first_nameTEXT
  • last_nameTEXT
  • salaryDECIMAL
  • department_idINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

ORDER BY <column> sorts the result. The default direction is ascending (smallest first).

Hint 2

You can write `ORDER BY salary ASC` explicitly — the ASC keyword is optional.

Hint 3

SELECT first_name, last_name, salary, department_id FROM employees ORDER BY salary;

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT first_name, last_name, salary, department_id FROM employees ORDER BY salary;

Why this works

ORDER BY is the LAST step in logical query processing — it runs after WHERE, GROUP BY, HAVING, and SELECT. Without ORDER BY, the engine may return rows in any order it finds convenient (insertion order is NOT guaranteed).

Success check

All 12 employees appear exactly once in ascending salary order.

Expected result

Use this output to verify values, aliases, ordering, and row count.

first_namelast_namesalarydepartment_id
EmmaThomas520002
JaneSmith550002
LisaDavis580002
TomWilson620003
RachelGarcia640003
SarahWilliams650003
ChrisAnderson680003
DavidBrown700001
AlexMiller720001
JohnDoe750001

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.