Lesson Description
Range and pattern matching.
Lesson 2 of 4
Progress: 0/4 exercises solved (0%)
Solve all exercises below to unlock the next lesson.
Lesson Description
Range and pattern matching.
Easy Project
Mini project: Turn this lesson into a real-world query artifact by writing one clean business report query and validating output quality.
These operators clean up common patterns that would otherwise require many OR conditions.
-- Instead of: WHERE department_id = 60 OR department_id = 80 OR department_id = 90
SELECT first_name, department_id
FROM employees
WHERE department_id IN (60, 80, 90);
NOT IN excludes the listed values:
WHERE department_id NOT IN (10, 20)
Caution: NOT IN returns no rows if the list contains a NULL. Use NOT EXISTS or IS NOT NULL guards when NULL is possible.
-- Products priced 30 to 80 (both endpoints included)
SELECT name, price
FROM products
WHERE price BETWEEN 30 AND 80;
Equivalent to price >= 30 AND price <= 80.
% matches any sequence of characters. _ matches exactly one character.
-- Last names starting with 'K'
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE 'K%';
-- Exactly 5-character last names
WHERE last_name LIKE '_____';
WHERE last_name ILIKE 'k%' -- matches King, king, KING
BETWEEN works on dates too: WHERE hire_date BETWEEN '2000-01-01' AND '2010-12-31'.LIKE patterns with a leading % (e.g., '%king') cannot use an index — they force a full scan.pg_trgm or full-text search.This lesson is part of Filtering & Logic. Focus on the core idea in IN, BETWEEN, LIKE, 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.
From the products table (store sandbox), return name and price for products priced between 30 and 80 inclusive.
Return first_name and last_name of HR employees whose first name starts with the letter A.
From the hospital sandbox, return first_name and last_name of patients whose first_name starts with 'C' or 'D', ordered by first_name.
From the store sandbox, return name, category_id, and price for products where category_id is 1, 2, or 3. Use IN. Order by category_id, then name.
Lesson 2 of 4
0/4 solved (0%)
Finish this lesson to unlock next.