SQL Lesson 2: WHERE — Filtering Rows

📎 This article includes 2 downloadable practice files ↓

⏱ 3 min read

📘 SQL Beginner Course · Lesson 2 of 12

In this article
  1. Comparisons
  2. AND, OR and brackets
  3. IN: a list of values
  4. BETWEEN: ranges
  5. LIKE: patterns
  6. NULL: missing values
  7. Where beginners go wrong
  8. Practice

SELECT chooses columns; WHERE chooses rows. It’s the SQL version of the filter arrows in Excel, except you can save it, combine it and run it on millions of rows.

Comparisons

SELECT name, price FROM products WHERE price > 5000;
SELECT name, city  FROM customers WHERE region = 'South';
SELECT order_id    FROM orders WHERE status <> 'Delivered';   -- not equal (also !=)

Text and dates go in single quotes. Numbers don’t.

AND, OR and brackets

SELECT order_id, qty FROM orders
WHERE status = 'Delivered' AND qty >= 8;

SELECT name FROM customers
WHERE region = 'North' AND (city = 'Delhi' OR city = 'Jaipur');
⚠️ AND is evaluated before OR. Without brackets, region = ‘North’ AND city = ‘Delhi’ OR city = ‘Mumbai’ also returns Mumbai rows from any region. When you mix AND and OR, always use brackets.

IN: a list of values

SELECT order_id, status FROM orders WHERE status IN ('Pending', 'Shipped');
SELECT name FROM customers WHERE city NOT IN ('Delhi', 'Mumbai');

BETWEEN: ranges

SELECT order_id, order_date FROM orders
WHERE order_date BETWEEN '2026-07-01' AND '2026-07-15';

BETWEEN includes both ends. Dates in SQLite are stored as text in YYYY-MM-DD form, which sorts and compares correctly.

LIKE: patterns

Pattern Matches
'%Traders%' Contains “Traders”
'S%' Starts with S
'%Co' Ends with Co
'_a%' Second letter is a (_ = exactly one character)

In SQLite LIKE ignores case for English letters; in PostgreSQL use ILIKE for that.

NULL: missing values

SELECT name FROM customers WHERE gstin IS NULL;       -- no GSTIN recorded
SELECT name FROM customers WHERE gstin IS NOT NULL;

NULL means “unknown”, not zero or blank text. = NULL never matches anything; always use IS NULL.

💡 Build WHERE clauses one condition at a time and check the row count after each. It’s much easier to find which condition is wrong.

Where beginners go wrong

Mistake Fix
WHERE gstin = NULL IS NULL
WHERE region = South Quotes: ‘South’
Mixing AND/OR without brackets Add brackets
Dates as ’01/07/2026′ Use ISO ‘2026-07-01’

Practice

Download shop.db and this lesson’s exercise file below. Open the database in the free DB Browser for SQLite (or sqlite3 shop.db), paste the exercises into the Execute SQL tab and write a query for each task. The expected result is printed under every task; answers are at the bottom of the file.

📎 Practice files for this article

  • 📄
    Practice database (SQLite)shop.db: 12 customers, 10 products and 300 orders (April-September 2026).
    ⬇ DB · 28 KB
  • 🗃️
    Lesson 2 exercisesTasks with the expected result under each, and answers at the bottom.
    ⬇ SQL · 2 KB

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong