Course curriculum
SQL · LESSON 2 OF 5

Filter rows with WHERE

Use comparisons and boolean conditions to return only the rows relevant to a question.

About 8 minutes · Beginner

Keep matching rows

WHERE filters rows before the final result is returned. A comparison such as score >= 40 produces a condition for each row.

Use the students table from the introduction. The query below asks for passing scores and orders them by id.

SQL
SELECT name, score
FROM students
WHERE score >= 40
ORDER BY id;
Expected output
Asha | 72
Meera | 85

Combine conditions

AND requires both conditions; OR accepts either. Use parentheses when combining them so readers can see the intended grouping.

SQL uses = for equality and <> for not equal. NULL means a missing or unknown value: check it with IS NULL rather than = NULL.

SQL
SELECT name
FROM students
WHERE score >= 40 AND score < 80
ORDER BY id;
Expected output
Asha

Keep input separate from SQL

In application code, bind user input as parameters with your database driver. Building SQL by concatenating raw input can allow injection.

A placeholder’s exact syntax depends on the driver. The examples here contain fixed values so you can focus on query semantics.

Try it yourself

Find the name and score of students whose score is below 40.

Show a possible solution
SQL
SELECT name, score FROM students WHERE score < 40 ORDER BY id;

Keep exploring: official SQL documentation.

0 of 5 completed on this device