Lesson 3
Choosing rows: WHERE
For readers who finished lesson 2 and can choose columns with SELECT.
- Filtering rows with WHERE
- Text and dates in quotes
- AND and OR
- IN and BETWEEN
- Patterns with LIKE
- 5 exercises
Every query runs on real PostgreSQL, in your browser. It downloads once, about 6 MB, then starts in a few seconds.
For a reference page on the same statements, read SQL WHERE Clause Explained with Operators and Examples.
The idea
WHERE keeps the rows for which a condition is true
A condition after WHERE is tested against every row on its own. The rows that pass make up the answer; the others are left out. The table itself does not change.
Try it
Find the products under 50
The comparisons are =, <> for not equal, <, <=, > and >=.
select name, price from products where price < 50;
6 products cost less than 50.
Try it
Text and dates go in single quotes
A text value is written in single quotes. It is compared exactly: 'romania', with a small r, finds nobody.
select first_name, last_name from customers where country = 'Romania';
42 customers live in Romania.
A date is written as date '2026-01-01', year first.
Watch out
Double quotes name a column, not a value
select first_name from customers where country = "Romania";
ERROR: column "Romania" does not exist
PostgreSQL reads a name in double quotes as the name of a column, finds no column called Romania, and stops. Write text values in single quotes.
Try it
AND needs both conditions to be true
Join two conditions with and, and a row has to pass both. With or, passing one of them is enough.
select order_id, order_date from orders where status = 'cancelled' and order_date >= date '2026-01-01';
41 cancelled orders were placed in 2026. Each of them passes both tests.
Watch out
AND is worked out before OR
These are meant to be the Audio and Storage products under 100:
select name, category, price from products
where category = 'Audio' or category = 'Storage' and price < 100;
| name | category | price |
|---|---|---|
| Noise-Cancelling Headset | Audio | 149.00 |
| Desk Speakers | Audio | 79.00 |
| USB Microphone | Audio | 99.00 |
| Memory Card 256 GB | Storage | 34.90 |
The headset costs 149. PostgreSQL read the condition as Audio, or (Storage and under 100). Parentheses, (category = 'Audio' or category = 'Storage') and price < 100, give the 3 products meant.
Try it
IN tests a list of values
category in ('Audio', 'Storage') is true when the category is any value in the list. It replaces the two conditions joined by or, and the parentheses with them.
select name, category, price
from products
where category in ('Audio', 'Storage')
and price < 100;The 3 Audio and Storage products under 100.
Try it
BETWEEN tests a range, both ends included
select name, price from products where price between 40 and 60;
4 products. A price of exactly 40 or 60 would count too: between includes both ends.
Try it
LIKE finds text that matches a pattern
In a LIKE pattern, % stands for any text, including none. '%Monitor%' matches every name with Monitor anywhere in it. Capitals count, as with =.
select name from products where name like '%Monitor%';
4 products have Monitor in their name, at the start, in the middle or at the end.
Exercise 1 of 5
Show the name and price of every product under 50.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select name, price from products where price < 50
Exercise 2 of 5
Show the first and last name of every customer in Romania.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select first_name, last_name from customers where country = 'Romania'
Exercise 3 of 5
Show the id and date of every order that was cancelled and placed in 2026.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select order_id, order_date from orders where status = 'cancelled' and order_date >= date '2026-01-01'
Exercise 4 of 5
Show the name, category and price of the Audio and Storage products that cost under 100.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select name, category, price from products where category in ('Audio', 'Storage') and price < 100
Exercise 5 of 5
Show the name of every product with SSD in its name.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select name from products where name like '%SSD%'