The WHERE Clause
So far every query has returned all twelve students. Useful databases hold thousands of rows and you almost never want all of them. The where clause is how you say which ones — and it is the single most used piece of SQL there is.
1Two clauses, two directions
This is the idea to fix in your head before the syntax, because everything else in the chapter builds on it.
Cuts the table into vertical stripes. Narrows each row.
Cuts the table into horizontal slices. Keeps fewer students.
select <columns> from <table name> where <condition>;
└── which columns ──┘ └── which rows ──┘Operate the two cuts separately and watch which of the two numbers moves.
Tick columns to change what select takes. Choose a condition to change what where keeps. Watch which number moves.
| id | name | percent |
|---|---|---|
| 1099 | Veena | 90.5 |
| 1187 | Anjali | 91 |
| 1250 | Sohan | 66 |
| 1402 | Lhamu | 95.2 |
| 2035 | Ravi | 83.75 |
| 2044 | Karma | 78.5 |
| 2199 | Diana | 88.25 |
| 2500 | Bhim | NULL |
| 3033 | Tom | 64.25 |
| 3120 | Pema | NULL |
| 3301 | Farhan | 72.8 |
| 3612 | Nima | 59.4 |
Ticking columns never changes the row count, and changing the condition never changes the column count. That is the whole difference between the two clauses.
2A first condition
Two rows instead of twelve. MySQL took each row in turn, asked is this row's city equal to Gangtok?, and kept the row only when the answer was yes. That is the whole mechanism — a question asked once per row.
city == 'Gangtok'. SQL has no assignment operator inside a condition, so a single = is the comparison. Writing == here is a syntax error.3The six relational operators
| Operator | Means | Example |
|---|---|---|
| = | equal to | city = 'Gangtok' |
| < | less than | percent < 70 |
| > | greater than | percent > 78 |
| <= | less than or equal to | grade <= 10 |
| >= | greater than or equal to | dob >= '2009-01-01' |
| <> or != | not equal to | grade <> 12 |
<> and != do exactly the same thing in MySQL. <> is the older, standard SQL spelling and the one the class notes use; != will be familiar from Python. Either is accepted in an exam.4Comparing numbers
Six of the twelve. Note who is missing: Bhim and Pema have NULL in percent, and an unknown value is not greater than 78 — it is not less than 78 either. Rows with NULL quietly fall out of every comparison, which is the reason IS NULL exists and gets its own lesson shortly.
5Comparing dates and text
Dates compare exactly as you would hope: earlier is “less than”. Students born in 2009 or later:
The date is quoted, exactly as when inserting one, and in the same 'YYYY-MM-DD' form. This is where the year-first format pays off: because it sorts correctly as text, a plain >= gives the right answer.
6Not equal
Eight rows, and four students are in grade 12 — eight plus four is twelve, so every row is accounted for. That arithmetic is a quick way to sanity-check a condition and its opposite.
7Recap
Columns from select, rows from where.
A single equals sign is the comparison in SQL.
Both mean 'not equal to'.
A row with NULL in the compared column is left out of any comparison.
Which clause decides how many rows come back?
How do you test for equality in a where clause?
Two students have NULL in percent. How many rows does where percent > 78 return out of 12, if 6 non-NULL students score above 78?