Ravindra BagaleCourses & study guides Track your progress

4. WHERE: Keep Only the Rows You Need

4.5 IN, BETWEEN and LIKE (% and _) with Real Filters

Why. Lists, ranges and patterns cover most analyst filters.

What. IN lists values. BETWEEN is inclusive range. LIKE uses % (any) and _ (one char).

How. WHERE city IN ('Pune','Mumbai') / amount BETWEEN 500 AND 900 / name LIKE 'A%'

Steps

  1. Cities in a list with IN.
  2. Amounts BETWEEN 600 AND 1000.
  3. Customer names starting with P.

Examples

Example 1 · dataset: raja_coffee

SELECT order_id, city, amount_inr
FROM orders
WHERE city IN ('Pune','Goa')
  AND amount_inr BETWEEN 600 AND 1000;

Output

order_idcityamount_inr
1001Pune840.0
1003Goa890.0
1006Pune890.0
1009Pune700.0
1021Pune620.0
1024Pune720.0

Meaning. Pune or Goa, mid-size amounts.

Example 2 · dataset: raja_coffee

SELECT name, city FROM customers WHERE name LIKE 'P%';

Output

namecity
Priya DeshmukhKolhapur

Meaning. Names starting with P (Priya pattern).

Ravindra Bagale's Tip

BETWEEN is inclusive on both ends — 600 and 1000 both qualify.