WHERE: filtering rows
WHERE keeps only the rows that meet a condition. Text and dates go inside single quotes '...'.
SQL
SELECT first_name, salary FROM employees WHERE salary >= 15000;
| equal / not equal | = <> |
| greater / less | > < >= <= |
| both / either | AND OR NOT |
| in a list | IN ('Riyadh','Jeddah') |
| between two values | BETWEEN 1000 AND 2000 |
| matches a pattern | LIKE 'Net%' |
| empty value | IS NULL IS NOT NULL |
SQL
SELECT name, city FROM customers WHERE country = 'Saudi Arabia' AND city <> 'Riyadh';
In LIKE, % means “any number of characters” and _ means “exactly one character”.
SQL
SELECT name, price FROM products WHERE name LIKE '%Drive%';
Watch outCareful: never compare with
= NULL; use IS NULL. Try: SELECT first_name FROM employees WHERE manager_id IS NULL;