← Back
LESSON 4
SQL (Core) - Filtering and sorting Query results
Prereq: None | Progress: 0%
Course Syllabus
Lesson 0 📖 What is SQL? Lesson 1 📖 SELECT queries 101 Lesson 2 📖 Queries with constraints (Pt. 1) Lesson 3 📖 Queries with constraints (Pt. 2) Lesson 4 📖 Filtering & sorting results Review 1 📖 Simple SELECT Queries Lesson 6 📖 Multi-table queries with JOINs Lesson 7 📖 OUTER JOINs Lesson 8 📖 A short note on NULLs Lesson 9 📖 Queries with expressions Lesson 10 📖 Queries with aggregates (Pt. 1) Lesson 11 📖 Queries with aggregates (Pt. 2) Lesson 12 📖 Order of execution of a Query Lesson 13 📖 Inserting rows Lesson 14 📖 Updating rows Lesson 15 📖 Deleting rows Lesson 16 📖 Creating tables Lesson 17 📖 Altering tables Lesson 18 📖 Dropping tables Lesson 19 📖 Subqueries & Nested Queries Lesson 20 📖 Set Operations Summary 🎓 Course Completion
Filtering and sorting Query results
SQL provides keywords like DISTINCT, ORDER BY, and LIMIT / OFFSET to eliminate duplicate rows, sort results alphanumerically, and handle pagination.
1. Deduplication with DISTINCT
SELECT DISTINCT director FROM movies ORDER BY director ASC;
Adding DISTINCT immediately after SELECT ensures that duplicate rows in the target set are automatically discarded.
2. Sorting with ORDER BY (ASC / DESC)
SELECT * FROM movies ORDER BY year DESC;
ORDER BY column ASC sorts low-to-high or A-to-Z (default). ORDER BY column DESC sorts high-to-low or Z-to-A.
3. Pagination with LIMIT & OFFSET
SELECT * FROM movies ORDER BY title ASC LIMIT 5 OFFSET 5;
LIMIT 5 restricts output to 5 rows maximum. OFFSET 5 skips the first 5 records before returning output.
🗄️ Schema Explorer SELECTABLE
› movies
|
🎯 Exercises Checklist:
☐ 1. List all directors of Pixar movies (alphabetically), without duplicates
☐ 2. List the last four Pixar movies released (ordered from most recent to least)
☐ 3. List the first five Pixar movies sorted alphabetically
☐ 4. List the next five Pixar movies sorted alphabetically
⚡ Practice Console
💡 Hint:
Loading hint...
🔑 Solution SQL:
SELECT * FROM movies; Data Output
No results. Run query to fetch data.