Skip to content
TeachTrails
Data EngineeringSQL

SQL SELECT and WHERE: Filtering Data Simply

Learn SELECT, WHERE, AND/OR, IN, LIKE, BETWEEN, NULL, COALESCE, CASE, DISTINCT, ORDER BY and LIMIT, plus the order SQL really runs in, with real results.

Sukanya Saha, Technology Consultant

11 min read

Beginner friendly

WHERE: keep only the rows you want

ShopKart · PostgreSQL
SELECT name, price
FROM products
WHERE price > 1000
ORDER BY price DESC;
Result4 rows
nameprice
Smartwatch4999.00
Bluetooth Speaker2499.00
Headphones1999.00
Laptop Stand1299.00

Comparison operators: =, <> (not equal, also !=), >, >=, <, <=. Text and dates go in single quotes.

AND, OR and brackets

ShopKart · PostgreSQL
SELECT order_id, customer_id, order_date
FROM orders
WHERE status = 'DELIVERED'
  AND order_date >= '2026-05-01'
ORDER BY order_date;
Result6 rows
order_idcustomer_idorder_date
10912026-05-02
11032026-05-04
11142026-05-09
11362026-05-20
11482026-05-22
11552026-05-28

IN, LIKE and BETWEEN

ShopKart · PostgreSQL
SELECT name, city
FROM customers
WHERE city IN ('Kolkata', 'Mumbai')
ORDER BY name;
Result4 rows
namecity
ArjunKolkata
AshaKolkata
RahulMumbai
SaraMumbai
ShopKart · PostgreSQL
SELECT name, category
FROM products
WHERE name LIKE '%Phone%'          -- % means "any characters"
   OR name LIKE 'Lap%';
Result2 rows
namecategory
Phone CaseAccessories
Laptop StandAccessories

% matches any number of characters, _ matches exactly one.

ShopKart · PostgreSQL
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;
Result4 rows
order_idorder_date
1012026-04-01
1022026-04-01
1032026-04-03
1042026-04-05

NULL: the most common beginner bug

NULL means “unknown”. Is unknown equal to unknown? Unknown. So = NULL never matches:

ShopKart · PostgreSQL
-- ❌ Looks right, returns NOTHING: NULL is never "equal" to anything
SELECT name FROM customers WHERE city = NULL;
Result0 rows

(no rows)

ShopKart · PostgreSQL
-- ✅ Use IS NULL / IS NOT NULL
SELECT name FROM customers WHERE city IS NULL;
Result1 row
name
Vikram
ExpressionResult
NULL = NULLNULL (not true!)
NULL + 5NULL
NULL AND falsefalse
NULL OR truetrue
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:

ShopKart · PostgreSQL
SELECT name, COALESCE(city, 'Unknown') AS city
FROM customers
ORDER BY customer_id DESC
LIMIT 3;
Result3 rows
namecity
VikramUnknown
SaraMumbai
ArjunKolkata

CASE: if/else inside a query

ShopKart · PostgreSQL
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;
Result6 rows
namepriceprice_band
Smartwatch4999.00Premium
Bluetooth Speaker2499.00Premium
Headphones1999.00Mid-range
Laptop Stand1299.00Mid-range
Charger499.00Budget
Phone Case299.00Budget

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

ShopKart · PostgreSQL
SELECT DISTINCT city
FROM customers
ORDER BY city;
Result6 rows
city
Bengaluru
Chennai
Delhi
Kolkata
Mumbai

DISTINCT treats NULL as one value, so the unknown city appears once (PostgreSQL sorts NULLs last when ascending).

ShopKart · PostgreSQL
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;
Result3 rows
nameprice
Smartwatch4999.00
Bluetooth Speaker2499.00
Headphones1999.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:

📊 Logical order of a query

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
FROM/JOIN, WHERE, GROUP BY, HAVING, SELECT (including window functions), DISTINCT, ORDER BY, LIMIT/OFFSET. It explains why aliases work in ORDER BY but not in WHERE.
Q2How do you handle NULLs in SQL?Fresher
Test with IS NULL / IS NOT NULL, replace with COALESCE (or NULLIF to create them), remember aggregates ignore NULLs except COUNT(*), and be careful with NOT IN when the list can contain NULL (use NOT EXISTS).
Q3Why can NOT IN return no rows?1–3 yrs
If the subquery or list contains a NULL, x NOT IN (..., NULL) is never true (it becomes unknown), so every row is filtered out. NOT EXISTS doesn't have this problem.
Q4Why is WHERE order_date BETWEEN '2026-04-01' AND '2026-04-30' risky for timestamps?1–3 yrs
For a timestamp column, '2026-04-30' means midnight, so anything later on the 30th is excluded. Use order_ts >= '2026-04-01' AND order_ts < '2026-05-01'.
Q5What's the difference between WHERE and HAVING?1–3 yrs
WHERE filters individual rows before grouping and can't use aggregates. HAVING filters groups after GROUP BY and can use aggregates like COUNT(*) > 2.

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. 1. Which finds customers with no city?

  2. 2. Which runs FIRST?

  3. 3. name LIKE 'Lap%' matches...

  4. 4. COALESCE(city, 'Unknown') returns...

SQL for Data Engineers

Part 2 of 12: SQL SELECT and WHERE