Lesson Description
Sort rows and trim the result.
Lesson 3 of 5
Progress: 0/4 exercises solved (0%)
Solve all exercises below to unlock the next lesson.
Lesson Description
Sort rows and trim the result.
Easy Project
Mini project: Turn this lesson into a real-world query artifact by writing one clean business report query and validating output quality.
ORDER BY sorts the result set. LIMIT caps the number of rows returned.
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC;
ASC (ascending, A→Z, 0→9) is the default. DESC reverses it.
Rows are sorted by the first column, then by the second for ties:
SELECT first_name, last_name, department_id, salary
FROM employees
ORDER BY department_id ASC, salary DESC;
-- Top 5 earners
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5;
Use with LIMIT for pagination:
-- Rows 11–20 ordered by last name
SELECT first_name, last_name
FROM employees
ORDER BY last_name
LIMIT 10 OFFSET 10;
When two rows have identical sort values, the order between them is undefined. Add a unique column (like id) as a final sort key to guarantee a deterministic result:
ORDER BY salary DESC, id ASC
ORDER BY is almost always the last clause in a query.LIMIT without ORDER BY returns an arbitrary subset — rarely what you want.SELECT can be used in ORDER BY.This lesson is part of SQL Fundamentals. Focus on the core idea in ORDER BY and LIMIT, then validate with deliberate practice.
What to master
Common mistakes
High-level strategy
Task ladder
Transparent data checks
Retention loop
Logical reasoning for commands
WHERE
Why: Limits rows to only the business-relevant subset.
Memory cue: Filter early to reduce noise.
ORDER BY
Why: Makes output deterministic and reviewable.
Memory cue: No ORDER BY means no guaranteed row order.
AS alias
Why: Makes output columns readable for teams and reports.
Memory cue: If the name is clear, the query is easier to trust.
Concept check
3 quick questions. One at a time. Instant score at the end.
Return the first_name, last_name, and salary of the 5 highest-paid employees, ordered by salary descending.
From the products table in the store sandbox, return the name and price of the 3 cheapest products, ordered by price ascending.
Return first_name and last_name of all employees ordered by last_name ascending, then first_name ascending.
From the store sandbox, return name and price of the 3 most expensive products that are in stock (stock > 0), ordered by price descending.
Lesson 3 of 5
0/4 solved (0%)
Finish this lesson to unlock next.