Day 2 is where you start thinking like a backend developer instead of just writing syntax.
You already know SELECT, WHERE, and sorting.
Today we master logical filtering + translating English into SQL.
We continue using PostgreSQL or MySQL locally.
If you completed Day 1, you already have the users table.
If not, recreate it:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT,
country VARCHAR(50),
signup_year INT,
purchases INT
);
INSERT INTO users VALUES
(1, 'Alice', 25, 'USA', 2023, 5),
(2, 'Bob', 30, 'UK', 2024, 2),
(3, 'Carol', 22, 'USA', 2024, 8),
(4, 'David', 35, 'Canada', 2022, 1),
(5, 'Eve', 28, 'UK', 2023, 4),
(6, 'Frank', 40, 'USA', 2021, 10),
(7, 'Grace', 19, 'Germany', 2024, 0);Both conditions must be true.
SELECT * FROM users
WHERE country = 'USA' AND age > 25;At least one condition must be true.
SELECT * FROM users
WHERE country = 'USA' OR country = 'UK';SQL evaluates logical conditions using Boolean algebra.
Order of evaluation:
- Parentheses
- NOT
- AND
- OR
Example:
SELECT * FROM users
WHERE country = 'USA'
AND (age > 30 OR purchases > 5);Without parentheses, your logic may change.
Cleaner alternative to multiple ORs.
Instead of:
WHERE country = 'USA' OR country = 'UK'Use:
WHERE country IN ('USA', 'UK')This improves readability and execution planning.
Range filtering.
SELECT * FROM users
WHERE age BETWEEN 20 AND 30;Important:
BETWEEN is inclusive.
Equivalent to:
age >= 20 AND age <= 30Used for string searching.
%→ any number of characters_→ single character
Example:
Users whose name starts with A:
SELECT * FROM users
WHERE name LIKE 'A%';Names ending with 'e':
WHERE name LIKE '%e';Names that contain the letter 'o'
WHERE name LIKE '%o%'- PostgreSQL:
LIKEis case-sensitive. UseILIKEfor case-insensitive. - MySQL: Usually case-insensitive depending on collation.
NULL is NOT zero. NULL means “unknown” or “missing.”
SELECT * FROM users
WHERE purchases IS NULL;Never use:
WHERE purchases = NULLThat will NOT work.
This is what interviewers test.
English:
Find users who signed up in 2024 and made more than 3 purchases.
SQL:
SELECT * FROM users
WHERE signup_year = 2024
AND purchases > 3;English:
Find users from USA or UK who are older than 25.
SQL:
SELECT * FROM users
WHERE country IN ('USA', 'UK')
AND age > 25;English:
Find users who are NOT from Canada and signed up before 2024.
SQL:
SELECT * FROM users
WHERE country <> 'Canada'
AND signup_year < 2024;Or:
WHERE NOT country = 'Canada'English:
Users from USA who are older than 30 OR have more than 8 purchases.
Correct:
SELECT * FROM users
WHERE country = 'USA'
AND (age > 30 OR purchases > 8);If you remove parentheses, the logic changes.
Find users younger than 25.
Find users from Germany or Canada.
Find users aged between 25 and 35 who are from the UK.
Find users whose name starts with 'C'.
Find users who:
- Are from USA
- Signed up after 2022
- AND have more than 5 purchases
Find users who:
- Are from USA or UK
- AND are younger than 30
- BUT exclude those with 8 purchases
Use:
- https://leetcode.com/problemset/database/
- https://www.hackerrank.com/domains/sql
- https://sqlzoo.net/
Focus only on filtering problems today.
When you run:
SELECT * FROM users
WHERE age > 30;The database:
-
Parses query
-
Creates execution plan
-
Decides:
- Full table scan?
- Use index?
-
Filters rows
-
Returns result
Without index: It scans every row.
With index: It uses a B-tree structure to jump directly to relevant rows.
We go deeper into indexing on Day 6.
Explain how SQL’s three-valued logic (TRUE, FALSE, UNKNOWN) affects NULL comparisons.
You should now:
✔ Write multi-condition filters confidently ✔ Translate English to SQL ✔ Understand Boolean logic in queries ✔ Know how NULL behaves ✔ Think like a backend engineer
Imagine a startup app.
Find:
- Active users (purchases > 0)
- Dormant users (purchases = 0)
- VIP users (purchases > 7)
- Users from top 2 countries
- Users under 25 from USA or Germany
Write all queries.
Tomorrow (Day 3):
We enter Aggregations — the most important topic for SQL job interviews.
That’s where SQL starts becoming powerful.