Filtering (WHERE, LIKE) & Sorting
SQL query search spaces are constrained using the WHERE clause, and ordered using the ORDER BY command.
1 SQL Clauses
SQL — Query Constraints
# Filter with matching strings
SELECT * FROM users WHERE username = 'balaji';
# Pattern matching with LIKE (% acts as wildcard)
SELECT * FROM users WHERE email LIKE '%@gmail.com';
# Filter with range lists
SELECT * FROM users WHERE age BETWEEN 20 AND 30;
SELECT * FROM users WHERE age IN (18, 21, 25);
# Sorting and Limiting
SELECT * FROM users
ORDER BY age DESC, created_at ASC
LIMIT 5 OFFSET 10;
2 Code Challenge
Challenge: Write a query retrieving the top 5 most expensive products whose names contain the word "phone", ordered from most to least expensive.