SQLancer
LESSON 4

SQL (Core) - Filtering and sorting Query results

Progress: 0%

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

Data Output

No results. Run query to fetch data.

Current Task Target

5 rows