Filtering and Sorting · Lesson 9 of 64

SQL WHERE

What you will learn

  • How WHERE keeps only the rows that meet a condition
  • The comparison operators =, <>, <, >, <= and >=
  • How to compare text, numbers and dates
  • How to count the rows that match

This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.

1. WHERE keeps the rows that match

SELECT chooses columns. WHERE chooses rows: only rows for which the condition is true are returned. It goes after FROM:

SELECT product_id, name, price FROM products WHERE price > 40 ORDER BY product_id;
product_id  name          price
----------  ------------  -----
         2  Ceramic mug    44.1
         6  Tea kettle     79.0
        10  Plant pot      42.9
        18  Highlighters   79.0
        23  Bookshelf      49.0
        29  Water bottle   86.9
        37  Foam roller    79.0
        44  Drawing kit    53.9
(8 rows)
SELECT account_id, account_type, balance_cents FROM accounts WHERE balance_cents >= 400000 ORDER BY account_id;
account_id  account_type  balance_cents
----------  ------------  -------------
       165  savings              428004
       238  checking             401600
       243  checking             432800
       398  savings              502080
(4 rows)
SELECT course_id, code, title, credits FROM courses WHERE credits >= 4 ORDER BY course_id;
course_id  code     title                   credits
---------  -------  ----------------------  -------
        1  MATH101  Foundations of Algebra        4
        3  MATH301  Statistics                    4
        4  SCI101   General Science               4
        5  SCI201   Chemistry                     4
        6  SCI202   Physics                       4
       18  CS201    Programming                   4
       19  CS301    Databases                     4
(7 rows)

The condition is checked for every row of the table, one by one. A row that does not meet it is left out.

2. The comparison operators

Operator Means
= equal to (one equals sign, not two)
<> (or !=) not equal to
< and > less than, greater than
<= and >= less than or equal, greater than or equal

3. Text conditions

Text goes in single quotes, and a comparison of text is exact: the same letters in the same case.

SELECT customer_id, first_name, last_name, country FROM customers WHERE country = 'Kenya' ORDER BY customer_id LIMIT 5;
customer_id  first_name  last_name  country
-----------  ----------  ---------  -------
         17  Bela        Mahmud     Kenya
         26  Yusuf       Perez      Kenya
         37  Priya       Uddin      Kenya
         64  Wei         Uddin      Kenya
         98  Dev         Ito        Kenya
(5 rows)

A different spelling of the case finds nothing, because 'kenya' is not 'Kenya' (for =, SQLite compares the letters exactly):

SELECT customer_id, country FROM customers WHERE country = 'kenya';
customer_id  country
-----------  -------
(0 rows)
SELECT account_id, account_type, status FROM accounts WHERE account_type = 'savings' ORDER BY account_id LIMIT 5;
account_id  account_type  status
----------  ------------  ------
         2  savings       open
         3  savings       open
         7  savings       closed
         8  savings       open
         9  savings       open
(5 rows)

A different spelling of the case finds nothing, because 'Savings' is not 'savings' (for =, SQLite compares the letters exactly):

SELECT account_id, account_type FROM accounts WHERE account_type = 'Savings';
account_id  account_type
----------  ------------
(0 rows)
SELECT section_id, course_id, term FROM sections WHERE term = '2025-Spring' ORDER BY section_id LIMIT 5;
section_id  course_id  term
----------  ---------  -----------
         2          2  2025-Spring
         5          3  2025-Spring
         7          4  2025-Spring
         8          5  2025-Spring
        10          6  2025-Spring
(5 rows)

A different spelling of the case finds nothing, because '2025-spring' is not '2025-Spring' (for =, SQLite compares the letters exactly):

SELECT section_id, term FROM sections WHERE term = '2025-spring';
section_id  term
----------  ----
(0 rows)

(To ignore case, change both sides to the same case with LOWER, lesson 1.5. Lesson 2.4 shows LIKE.)

4. Not equal

<> keeps the rows that are different from a value:

SELECT status, COUNT(*) AS orders FROM orders WHERE status <> 'delivered' GROUP BY status ORDER BY status;
status     orders
---------  ------
cancelled     170
paid          186
pending       135
shipped       231
(4 rows)
SELECT kind, COUNT(*) AS rows_found FROM transactions WHERE kind <> 'deposit' GROUP BY kind ORDER BY kind;
kind          rows_found
------------  ----------
fee                  403
interest             308
transfer_in          539
transfer_out         539
withdrawal          2519
(5 rows)
SELECT enrolled_year, COUNT(*) AS students FROM students WHERE enrolled_year <> 2026 GROUP BY enrolled_year ORDER BY enrolled_year;
enrolled_year  students
-------------  --------
         2023        83
         2024       102
         2025        86
(3 rows)

(GROUP BY and COUNT are the subject of chapter 3; here they just make the result short.)

5. Dates are compared as text

A date in the form YYYY-MM-DD (lesson 1.7) compares correctly as text, so >= and < work for "on or after" and "before":

SELECT order_id, order_date, status FROM orders WHERE order_date >= '2026-06-01' ORDER BY order_date, order_id LIMIT 5;
order_id  order_date  status
--------  ----------  ---------
     205  2026-06-01  shipped
     231  2026-06-01  delivered
     319  2026-06-01  delivered
    1232  2026-06-01  delivered
    1298  2026-06-01  delivered
(5 rows)
SELECT account_id, opened_on FROM accounts WHERE opened_on >= '2025-06-01' ORDER BY opened_on, account_id LIMIT 5;
account_id  opened_on
----------  ----------
       150  2025-06-12
       216  2025-06-12
       280  2025-06-12
       161  2025-06-13
       162  2025-06-17
(5 rows)
SELECT student_id, first_name, birth_date FROM students WHERE birth_date < '2010-01-01' ORDER BY birth_date, student_id LIMIT 5;
student_id  first_name  birth_date
----------  ----------  ----------
       280  Quinn       2007-01-05
        19  Arjun       2007-01-06
       348  Olga        2007-01-08
        67  Ibrahim     2007-01-17
         8  Hugo        2007-01-25
(5 rows)

6. How many rows matched?

COUNT(*) counts rows. With WHERE it counts only the matching ones:

SELECT COUNT(*) AS expensive_products FROM products WHERE price > 40;
expensive_products
------------------
                 8
(1 row)
SELECT COUNT(*) AS big_accounts FROM accounts WHERE balance_cents >= 400000;
big_accounts
------------
           4
(1 row)
SELECT COUNT(*) AS four_credit_courses FROM courses WHERE credits >= 4;
four_credit_courses
-------------------
                  7
(1 row)

Worked example: from a question to a WHERE

A question in words becomes a condition:

Question Column Operator Value Condition
"products that cost more than 40" price > 40 price > 40
"customers from Kenya" country = 'Kenya' country = 'Kenya'
"orders placed in or after June 2026" order_date >= '2026-06-01' order_date >= '2026-06-01'

Find the column, choose the operator, write the value (text and dates in single quotes, numbers without). The tables are made up.

Common mistakes

  • Two equals signs (==). SQL uses one: =.
  • Text without quotes. WHERE country = Kenya looks for a column called Kenya.
  • Wrong case in text. 'kenya' does not match 'Kenya'.
  • Numbers in quotes. price > '40' compares with text and can give odd results.
  • Dates in another format. Use YYYY-MM-DD.

Key takeaways

  • WHERE keeps the rows whose condition is true.
  • Operators: =, <>, <, >, <=, >=.
  • Text and dates go in single quotes; text is compared exactly; dates compare correctly as YYYY-MM-DD text.
  • COUNT(*) with WHERE counts the matching rows.

Practice

1. Write a condition for "price at most 20".

Show answer

WHERE price <= 20

2. What is wrong with WHERE city == 'Leeds'?

Show answer

SQL uses a single equals sign: WHERE city = 'Leeds'.

3. Why does WHERE country = 'kenya' return nothing when the data has Kenya?

Show answer

Text is compared exactly, including capital letters, so 'kenya' is a different text.

4. How do you keep rows dated before 1 January 2026?

Show answer

WHERE order_date < '2026-01-01' (with the real date column).

5. What does SELECT COUNT(*) FROM products WHERE price > 40; return?

Show answer

One number: how many products cost more than 40.

6. Which operator means "not equal to"?

Show answer

<> (many databases also accept !=).