MyMisPal
← SQL lessons
Lesson 3 of 12

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 / eitherAND OR NOT
in a listIN ('Riyadh','Jeddah')
between two valuesBETWEEN 1000 AND 2000
matches a patternLIKE 'Net%'
empty valueIS 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;

Exercises

Exercise 1

Show first_name and salary for employees earning more than 12000.

Exercise 2

Show name and city for customers in Riyadh or Jeddah.

Exercise 3

Show name and price for products priced between 400 and 1200 inclusive.

Spotted a mistake on this page, or have an idea to improve the site?