Skip to content
AgileLogics
5 min read

Filtering Rows with WHERE

Prerequisites: Choosing columns, aliases, and calculations

WHERE keeps only the rows that match a condition.

Syntax

SELECT columns
FROM table
WHERE condition;

Example

SELECT name, price
FROM products
WHERE price > 50;

Expected output

+-----------------------------+--------+
|            name             | price  |
+-----------------------------+--------+
| Mechanical Keyboard         |     89 |
| 27-inch Monitor             | 249.99 |
| Noise-Cancelling Headphones |    199 |
| Webcam                      |   59.9 |
+-----------------------------+--------+

The database checks the condition for every row and returns those where it's true.

Comparison operators

Operator Meaning
= equal to
<> or != not equal to
> < greater / less than
>= <= greater / less or equal

SQL uses a single = for comparison. <> is the standard "not equal"; != works in every major database too.

Text and dates

Text values go in single quotes:

SELECT name, city
FROM customers
WHERE city = 'London';
+--------------+--------+
|     name     |  city  |
+--------------+--------+
| Ada Lovelace | London |
| Alan Turing  | London |
+--------------+--------+

Dates are written as text in 'YYYY-MM-DD' format and compare in calendar order:

SELECT id, ordered_on, status
FROM orders
WHERE ordered_on >= '2025-04-01';
+----+------------+---------+
| id | ordered_on | status  |
+----+------------+---------+
|  6 | 2025-04-02 | pending |
|  7 | 2025-04-10 | shipped |
|  8 | 2025-04-22 | pending |
+----+------------+---------+

Common mistakes

  • Double quotes for text: WHERE city = "London". In standard SQL (and PostgreSQL), double quotes mean a column name, so this fails or behaves unexpectedly. Use single quotes for text values.
  • Case mismatches: 'london' doesn't equal 'London' in most databases. (MySQL's default settings compare text case-insensitively — one of the dialect differences to watch for.)

Practice

List the name and category of every product in the Accessories category.

Show solution
SELECT name, category
FROM products
WHERE category = 'Accessories';
+---------------------+-------------+
|        name         |  category   |
+---------------------+-------------+
| Mechanical Keyboard | Accessories |
| USB-C Hub           | Accessories |
| Laptop Stand        | Accessories |
+---------------------+-------------+