WHERE: keep only the rows you want
SELECT name, price
FROM products
WHERE price > 1000
ORDER BY price DESC;| name | price |
|---|---|
| Smartwatch | 4999.00 |
| Bluetooth Speaker | 2499.00 |
| Headphones | 1999.00 |
| Laptop Stand | 1299.00 |
Comparison operators: =, <> (not equal, also !=), >, >=, <, <=. Text and dates go in single quotes.
AND, OR and brackets
SELECT order_id, customer_id, order_date
FROM orders
WHERE status = 'DELIVERED'
AND order_date >= '2026-05-01'
ORDER BY order_date;| order_id | customer_id | order_date |
|---|---|---|
| 109 | 1 | 2026-05-02 |
| 110 | 3 | 2026-05-04 |
| 111 | 4 | 2026-05-09 |
| 113 | 6 | 2026-05-20 |
| 114 | 8 | 2026-05-22 |
| 115 | 5 | 2026-05-28 |
IN, LIKE and BETWEEN
SELECT name, city
FROM customers
WHERE city IN ('Kolkata', 'Mumbai')
ORDER BY name;| name | city |
|---|---|
| Arjun | Kolkata |
| Asha | Kolkata |
| Rahul | Mumbai |
| Sara | Mumbai |
SELECT name, category
FROM products
WHERE name LIKE '%Phone%' -- % means "any characters"
OR name LIKE 'Lap%';| name | category |
|---|---|
| Phone Case | Accessories |
| Laptop Stand | Accessories |
% matches any number of characters, _ matches exactly one.
SELECT order_id, order_date
FROM orders
WHERE order_date BETWEEN '2026-04-01' AND '2026-04-05' -- both ends included
ORDER BY order_date;| order_id | order_date |
|---|---|
| 101 | 2026-04-01 |
| 102 | 2026-04-01 |
| 103 | 2026-04-03 |
| 104 | 2026-04-05 |
NULL: the most common beginner bug
NULL means “unknown”. Is unknown equal to unknown? Unknown. So = NULL never matches:
-- ❌ Looks right, returns NOTHING: NULL is never "equal" to anything
SELECT name FROM customers WHERE city = NULL;(no rows)
-- ✅ Use IS NULL / IS NOT NULL
SELECT name FROM customers WHERE city IS NULL;| name |
|---|
| Vikram |
| Expression | Result |
|---|---|
| NULL = NULL | NULL (not true!) |
| NULL + 5 | NULL |
| NULL AND false | false |
| NULL OR true | true |
| COUNT(city) | Skips NULLs |
| city NOT IN ('Pune', NULL) | Never true: a classic trap |
Replace NULLs for display with COALESCE, which returns the first value that isn't NULL:
SELECT name, COALESCE(city, 'Unknown') AS city
FROM customers
ORDER BY customer_id DESC
LIMIT 3;| name | city |
|---|---|
| Vikram | Unknown |
| Sara | Mumbai |
| Arjun | Kolkata |
CASE: if/else inside a query
SELECT name, price,
CASE
WHEN price >= 2000 THEN 'Premium'
WHEN price >= 500 THEN 'Mid-range'
ELSE 'Budget'
END AS price_band
FROM products
ORDER BY price DESC;| name | price | price_band |
|---|---|---|
| Smartwatch | 4999.00 | Premium |
| Bluetooth Speaker | 2499.00 | Premium |
| Headphones | 1999.00 | Mid-range |
| Laptop Stand | 1299.00 | Mid-range |
| Charger | 499.00 | Budget |
| Phone Case | 299.00 | Budget |
CASE checks conditions top to bottom and uses the first that's true. Without ELSE, non-matching rows get NULL.
DISTINCT, ORDER BY and LIMIT
SELECT DISTINCT city
FROM customers
ORDER BY city;| city |
|---|
| Bengaluru |
| Chennai |
| Delhi |
| Kolkata |
| Mumbai |
DISTINCT treats NULL as one value, so the unknown city appears once (PostgreSQL sorts NULLs last when ascending).
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;| name | price |
|---|---|
| Smartwatch | 4999.00 |
| Bluetooth Speaker | 2499.00 |
| Headphones | 1999.00 |
ORDER BY price DESC sorts high to low (ASC is the default). LIMIT 3 keeps three rows. SQL Server uses SELECT TOP 3; the standard form is FETCH FIRST 3 ROWS ONLY.
The order SQL really runs in
You write SELECT first, but the database runs it almost last. This one diagram explains many confusing errors:
1. FROM / JOIN
Which tables
2. WHERE
Filter rows
3. GROUP BY
Make groups
4. HAVING
Filter groups
5. SELECT
Pick/compute columns, window functions
6. ORDER BY → LIMIT
Sort, then cut
- You can't use a SELECT alias in WHERE (WHERE runs before SELECT names it).
- You can use it in ORDER BY (which runs after SELECT).
- You can't filter on an aggregate in WHERE; that's what HAVING is for (next lesson).
🎯 Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1What is the logical order of execution of a SELECT?Fresher
Q2How do you handle NULLs in SQL?Fresher
Q3Why can NOT IN return no rows?1–3 yrs
Q4Why is WHERE order_date BETWEEN '2026-04-01' AND '2026-04-30' risky for timestamps?1–3 yrs
Q5What's the difference between WHERE and HAVING?1–3 yrs
Key takeaways
- WHERE filters rows; combine conditions with AND/OR and brackets.
- NULL is unknown: IS NULL to find it, COALESCE to replace it.
- CASE adds if/else logic; DISTINCT removes duplicate rows.
- Knowing the logical run order prevents many errors.
Frequently asked questions
What is the order of execution of a SQL query?
FROM (and JOINs) → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. That's why you can't use a SELECT alias in WHERE, but you can in ORDER BY.
Why does WHERE column = NULL return nothing?
NULL means unknown, and comparing anything to unknown gives unknown (not true), so no row passes. Use IS NULL or IS NOT NULL.
Is BETWEEN inclusive?
Yes, both ends are included. For timestamps, prefer >= start AND < next_day, so you don't miss times later on the last day.
Is LIKE case-sensitive?
In PostgreSQL, yes. Use ILIKE (PostgreSQL, Snowflake, Databricks) or LOWER(name) LIKE '%phone%' for case-insensitive matching.
Is the order of rows guaranteed without ORDER BY?
No. Without ORDER BY, the database may return rows in any order, and it can change between runs. Always ORDER BY when order matters.
🧠 Quick check: SELECT and WHERE
1. Which finds customers with no city?
2. Which runs FIRST?
3. name LIKE 'Lap%' matches...
4. COALESCE(city, 'Unknown') returns...