Reading history

SQL WHERE Clause: Filter Rows With AND, OR, IN, LIKE

4 min read 0 views

Learn the SQL WHERE clause: comparison operators, AND, OR and NOT, IN, BETWEEN, LIKE wildcards and IS NULL, with tested examples.

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 WHERE filters rows
  • Comparison operators and combining conditions
  • Shortcuts: IN and BETWEEN
  • Pattern matching with LIKE
  • Why = NULL never 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.
  • AND needs both conditions to be true; OR needs 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 several OR conditions on one column.
  • BETWEEN a AND b includes both ends: 3.4 and 3.7 would both match.
  • LIKE matches patterns: % means “any number of characters” and _ means “exactly one character”. 'N%' finds names starting with N.
  • IS NULL finds missing values. cgpa = NULL matches nothing, because NULL means “unknown” and comparing anything with unknown gives unknown.
  • NOT reverses a condition. Note that Nadia (CSE) is excluded here, but a row whose dept was NULL would 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

  1. Double quotes around text. In standard SQL, double quotes mean a column name. Use single quotes for values.
  2. = NULL instead of IS NULL.
  3. Mixing AND and OR without brackets.
  4. Assuming LIKE is case-insensitive everywhere. It is in SQLite and MySQL by default, but case-sensitive in PostgreSQL (use ILIKE there).

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

  • WHERE keeps only rows that match a condition.
  • Combine conditions with AND, OR, NOT, and use brackets.
  • IN, BETWEEN and LIKE shorten common filters.
  • Test missing values with IS NULL / IS NOT NULL.

Next: SQL ORDER BY and LIMIT.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *