Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 02 / Topic 5 of 9

IN, BETWEEN, LIKE and ILIKE

Three shortcuts that replace clumsy chains of OR:

IN — is the value in this list?

SELECT first_name, city FROM customers
WHERE city IN ('Pune', 'Delhi', 'Chennai');
 first_name |  city   
------------+---------
 Asha       | Pune
 Ravi       | Delhi
 Meera      | Chennai
 Vikram     | Pune
(4 rows)

BETWEEN — inclusive range, both ends in:

SELECT name, price FROM products
WHERE price BETWEEN 35 AND 120
ORDER BY price;
        name        | price  
--------------------+--------
 SQL for Beginners  |  35.00
 Desk Lamp          |  45.00
 Postgres Deep Dive |  55.00
 Chef Knife         |  80.00
 Coffee Maker       | 120.00
(5 rows)

(Both 35.00 and 120.00 made the cut — BETWEEN includes its endpoints.)

LIKE — pattern matching for text. % matches anything (including nothing), _ matches exactly one character:

SELECT name FROM products WHERE name LIKE 'Laptop%';
     name      
---------------
 Laptop Pro 14
 Laptop Air 13
(2 rows)

LIKE is case-sensitive: 'phone%' would miss Phone X. Postgres adds ILIKE — same patterns, case folded:

SELECT name, price
FROM products
WHERE name ILIKE '%laptop%'
   OR name ILIKE 'phone%';
     name      |  price  
---------------+---------
 Laptop Pro 14 | 1200.00
 Laptop Air 13 |  950.00
 Phone X       |  799.00
 Phone Lite    |  399.00
(4 rows)

(ILIKE is a PostgreSQL extension — handy daily, but expect plain LIKE plus lower() elsewhere.)

🧪 Quick quiz — answer from memory, then check.

  1. Does price BETWEEN 35 AND 120 include a product priced exactly 120?
  2. What does 'Phone' LIKE 'phone%' return?
Show the answers
  1. Yes — BETWEEN is inclusive on both ends.
  2. false — LIKE is case-sensitive. ILIKE matches regardless of case.

Done here? The practice set for Part 2 — 8 exercises with solutions — is on the part page.

Search the guide