A table can hold millions of rows, but you rarely want all of them. The WHERE clause is the filter: it keeps only the rows that match a condition, such as students from Dhaka or orders above 1,000 taka. This lesson covers comparison operators, AND/OR/NOT, IN, BETWEEN, LIKE pattern matching and the special rules for NULL.
In this lesson you will learn
- How
WHEREfilters rows - Comparison operators and combining conditions
- Shortcuts:
INandBETWEEN - Pattern matching with
LIKE - Why
= NULLnever works, and what to use instead
Sample data used in this course
All examples run against a small university database. The students table has the columns id, name, city, dept, cgpa and 8 rows; Nadia’s CGPA is missing (NULL). Later lessons add departments, courses and enrollments. See SQL SELECT for the full table.
Filtering with Conditions
-- Students from Dhaka
SELECT name, city, cgpa FROM students WHERE city = 'Dhaka';
-- CSE students with CGPA above 3.7
SELECT name, dept, cgpa FROM students WHERE dept = 'CSE' AND cgpa > 3.7;
-- Students from Khulna or Sylhet
SELECT name, city FROM students WHERE city = 'Khulna' OR city = 'Sylhet';
Output
+--------+-------+------+
| name | city | cgpa |
+--------+-------+------+
| Nusrat | Dhaka | 3.82 |
| Tania | Dhaka | 3.95 |
| Rafi | Dhaka | 2.98 |
+--------+-------+------+
(3 rows)
+--------+------+------+
| name | dept | cgpa |
+--------+------+------+
| Nusrat | CSE | 3.82 |
| Tania | CSE | 3.95 |
+--------+------+------+
(2 rows)
+-------+--------+
| name | city |
+-------+--------+
| Imran | Khulna |
| Mitu | Sylhet |
+-------+--------+
(2 rows)
- Text values go in single quotes:
'Dhaka'. Numbers do not. ANDneeds both conditions to be true;ORneeds at least one.- Comparison operators:
=,<>(or!=) for “not equal”,<,>,<=,>=.
IN, BETWEEN, LIKE, NULL and NOT
SELECT name, city FROM students WHERE city IN ('Chattogram', 'Rajshahi');
SELECT name, cgpa FROM students WHERE cgpa BETWEEN 3.4 AND 3.7;
SELECT name FROM students WHERE name LIKE 'N%';
SELECT name, cgpa FROM students WHERE cgpa IS NULL;
SELECT name, dept FROM students WHERE NOT dept = 'CSE';
Output
+--------+------------+
| name | city |
+--------+------------+
| Arif | Chattogram |
| Sabbir | Rajshahi |
| Nadia | Chattogram |
+--------+------------+
(3 rows)
+--------+------+
| name | cgpa |
+--------+------+
| Arif | 3.41 |
| Mitu | 3.67 |
| Sabbir | 3.55 |
+--------+------+
(3 rows)
+--------+
| name |
+--------+
| Nusrat |
| Nadia |
+--------+
(2 rows)
+-------+------+
| name | cgpa |
+-------+------+
| Nadia | NULL |
+-------+------+
(1 row)
+--------+------+
| name | dept |
+--------+------+
| Arif | EEE |
| Imran | BBA |
| Rafi | EEE |
| Sabbir | BBA |
+--------+------+
(4 rows)
IN (...)is a short way to write severalORconditions on one column.BETWEEN a AND bincludes both ends: 3.4 and 3.7 would both match.LIKEmatches patterns:%means “any number of characters” and_means “exactly one character”.'N%'finds names starting with N.IS NULLfinds missing values.cgpa = NULLmatches nothing, becauseNULLmeans “unknown” and comparing anything with unknown gives unknown.NOTreverses a condition. Note that Nadia (CSE) is excluded here, but a row whosedeptwasNULLwould also be excluded.
AND Before OR
AND is evaluated before OR, just as multiplication comes before addition. So WHERE dept = 'CSE' OR dept = 'EEE' AND cgpa > 3.5 means “all CSE students, plus EEE students above 3.5”. Use brackets to say what you mean: WHERE (dept = 'CSE' OR dept = 'EEE') AND cgpa > 3.5.
Common Mistakes
- Double quotes around text. In standard SQL, double quotes mean a column name. Use single quotes for values.
= NULLinstead ofIS NULL.- Mixing
ANDandORwithout brackets. - Assuming
LIKEis case-insensitive everywhere. It is in SQLite and MySQL by default, but case-sensitive in PostgreSQL (useILIKEthere).
Practice Exercises
Exercise 1. Find EEE students with a CGPA below 3.5.
Show answer
SELECT name, cgpa FROM students WHERE dept = 'EEE' AND cgpa < 3.5;
-- Arif 3.41, Rafi 2.98
Exercise 2. Find students whose name ends in “a”.
Show answer
SELECT name FROM students WHERE name LIKE '%a';
-- Tania, Nadia
Frequently Asked Questions
Can I use WHERE with UPDATE and DELETE?
Yes, and you almost always should. Without WHERE, an UPDATE or DELETE affects every row. See INSERT, UPDATE and DELETE.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping. HAVING filters groups after GROUP BY. See GROUP BY and HAVING.
Summary
WHEREkeeps only rows that match a condition.- Combine conditions with
AND,OR,NOT, and use brackets. IN,BETWEENandLIKEshorten common filters.- Test missing values with
IS NULL/IS NOT NULL.
Next: SQL ORDER BY and LIMIT.
