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

Rank Salaries from Highest to Lowest

Return first_name, last_name, salary, and department_id for every employee, ordered by salary descending.

  • Sorting

Exercise brief

Understand the request

Compensation partner Leaders need the same salary population ranked with the highest salary first.

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

Return

  • Return one row per employee.
  • Place the highest salary first.

Constraints

  • Use ORDER BY salary DESC.

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

Add the keyword DESC after the column name to reverse the order.

Hint 2

There is no DESCENDING — only DESC. ASC and DESC are the only direction keywords.

Hint 3

ORDER BY salary DESC

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 DESC;

Why this works

ASC is "smallest first" for numbers, "earliest first" for dates, "alphabetical first" for text. DESC is the inverse for each. The direction keyword belongs to a single sort column — `ORDER BY a, b DESC` means a ASC, b DESC.

Success check

All employees appear exactly once from highest to lowest salary.

Expected result

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

first_namelast_namesalarydepartment_id
MikeJohnson800001
AmyTaylor780001
JohnDoe750001
AlexMiller720001
DavidBrown700001
ChrisAnderson680003
SarahWilliams650003
RachelGarcia640003
TomWilson620003
LisaDavis580002

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.