Filtering rows with WHERE
Keep only the rows you are interested in.
A real PostgreSQL database runs inside your browser tab. Nothing to install.
WHERE keeps matching rows
WHERE comes after the table name and holds a condition. Only rows where the condition is true come back.
Text values go in single quotes, as in 'Pune'. Text is case sensitive, so 'pune' would match nothing here.
SELECT * FROM students WHERE city = 'Pune';Your turn: one city
Filter the table.
Practice task
Show every column for the students who live in Mumbai.
Your answer is run and checked in the lesson. Create a free account to try it.
Numbers, AND, OR and BETWEEN
Numbers are written without quotes. You can compare them with =, <, >, <= and >=.
Join conditions with AND when both must be true, and with OR when either is enough.
BETWEEN 60 AND 80 is a shorter way to write a range. It includes both ends, so 60 and 80 both match.
SELECT name, city, marks FROM students WHERE city = 'Delhi' AND marks >= 80;Your turn: a range
Both ends of the range count.
Practice task
Show the name and marks of students whose marks are from 60 to 80, including 60 and 80.
Your answer is run and checked in the lesson. Create a free account to try it.
Patterns and missing values
LIKE matches text against a pattern. The percent sign stands for any run of characters, so 'A%' means text that starts with A.
A missing value is called NULL. It is not zero and not empty text. It means no value was stored.
You cannot find it with =. Use IS NULL to find missing values and IS NOT NULL to find rows that have one.
SELECT name, email FROM students WHERE name LIKE 'A%' OR email IS NOT NULL;Your turn: missing values
Two students have no email stored.
Practice task
Show the names of the students who have no email.
Your answer is run and checked in the lesson. Create a free account to try it.