Module 1: SQL Foundations50 min

Pattern Matching: LIKE, IN, BETWEEN

Progress Tracking

Log in to save this lesson and continue from where you left off.

Log in

Beyond Exact Matches

So far, your WHERE clauses check for exact matches: department = 'HR', salary = 90000. But real questions are often fuzzier:

  • Find products that start with 'Sh'
  • Get orders from any of these three months
  • Show salaries between 50,000 and 100,000

That's what LIKE, IN, and BETWEEN are for. They give you flexible ways to match patterns and ranges.

LIKE: Pattern Matching for Text

LIKE lets you search for patterns in text using two wildcard characters:

  • % — matches any sequence of characters (including none)
  • _ — matches exactly one character

Pattern Matching with LIKE

SQL
SELECT *
FROM orders
WHERE order_details LIKE 'Sh%';
Table: techcorp_workforce
idfirst_namelast_namedepartmentsalaryphone_numberjoining_date
1SarahMitchellHR95000555-01012021-03-15
2MichaelChenHR88000555-01022022-06-01
3EmilyRodriguezHR825002021-09-20
4DavidParkHR80000555-01042023-01-10
5LisaThompsonHR650002021-04-05
1
Find Names Starting With a Letter

Find employees whose first name starts with S from `techcorp_workforce`.

Tables: techcorp_workforce

The % after 'Sh' means "Sh followed by anything." So Shoes, Shirts, and Shorts would all match.

Ends With

Table: techcorp_workforce
idfirst_namelast_namedepartmentsalaryphone_numberjoining_date
1SarahMitchellHR95000555-01012021-03-15
2MichaelChenHR88000555-01022022-06-01
3EmilyRodriguezHR825002021-09-20
4DavidParkHR80000555-01042023-01-10
5LisaThompsonHR650002021-04-05
SQL
SELECT
  first_name,
  last_name
FROM techcorp_workforce
WHERE last_name LIKE '%a';
SQL
SELECT
  first_name,
  last_name,
  department
FROM techcorp_workforce
WHERE first_name LIKE '%i%';

Single Character Matching with _

The underscore matches exactly one character. This is useful when you know the pattern length.

SQL
-- Find 3-letter product names
WHERE order_details LIKE '___'

-- Find names starting with any letter, then 'oe'
WHERE first_name LIKE '_oe'

Underscores are less common than %, but handy for fixed-length codes or IDs.

Practice: Underscore Wildcards

Table: worker
worker_idfirst_namelast_namesalaryjoining_datedepartment
1MonikaArora1000002014-02-20HR
2NiharikaVerma800002014-06-11Admin
3VishalSinghal3000002014-02-20HR
4AmitahSingh5000002014-02-20Admin
5VivekBhati5000002014-06-11Admin
2
First Names With Six Letters Ending in 'h'
View solution

Find all workers whose first name contains 6 letters and also ends with the letter 'h'. Display all information about the workers in output.

Tables: worker

Case Sensitivity

In PostgreSQL, LIKE is case-sensitive. 'Sh%' won't match 'shoes' or 'SHIRTS'.

If you need case-insensitive matching, use ILIKE (PostgreSQL only) or LOWER():

PostgreSQL
-- PostgreSQL: ILIKE is case-insensitive
WHERE order_details ILIKE 'sh%'

-- Works in all dialects: convert to lowercase first
WHERE LOWER(order_details) LIKE 'sh%'

Practice: LIKE

Table: google_file_store
filenamecontents
draft1.txtThe stock exchange predicts a bull market which would make many investors happy.
draft2.txtThe stock exchange predicts a bull market which would make many investors happy, but analysts warn of possibility of too much optimism and that in fact we are awaiting a bear market.
final.txtThe stock exchange predicts a bull market which would make many investors happy, but analysts warn of possibility of too much optimism and that in fact we are awaiting a bear market. As always predicting the future market is an uncertain game and all investors should follow their instincts and best practices.
3
Find drafts which contains the word 'optimism'
View solution

Find drafts which contains the word 'optimism'.

Tables: google_file_store

IN: Matching a List of Values

Remember chaining ORs to check multiple values? IN does the same thing, but cleaner.

Instead of:

SQL
WHERE department = 'HR'
  OR department = 'Admin'
  OR department = 'Engineering'

Write:

SQL
WHERE department IN ('HR', 'Admin', 'Engineering')

Much better. Let's see it in action:

4
Try IN Filtering

Find all employees in the HR, Admin, or Account departments using IN.

Tables: techcorp_workforce

This returns all HR, Admin, and Account employees.

IN with Numbers

IN works with any data type, not just text:

SQL
SELECT
  id,
  cust_id,
  order_details
FROM orders
WHERE cust_id IN (3, 7, 15);

This gets all orders from customers with IDs 3, 7, or 15.

NOT IN: Excluding Values

Add NOT to exclude values from the list:

SQL
SELECT
  first_name,
  last_name,
  department
FROM techcorp_workforce
WHERE department NOT IN ('Admin');

This returns everyone except Admin employees.

Use IN Instead of Chaining ORs

IN is cleaner than OR chains and easier to modify. Use it whenever you're checking a column against multiple specific values.

NOT IN and NULL Values

Watch out for NOT IN with NULL values. If your list contains any NULLs, the entire NOT IN check returns no rows. This happens because comparisons with NULL return NULL (unknown), not true or false. We'll explain this fully in Lesson 1.6.

Practice: IN

Table: olympics_athletes_events
idnamesexageheightweightteamnocgamesyearseasoncitysporteventmedal
3520Guillermo J. AmparanMMexicoMEX1924 Summer1924SummerParisAthleticsAthletics Men's 800 metres
35394Henry John FinchettMGreat BritainGBR1924 Summer1924SummerParisGymnasticsGymnastics Men's Rings
21918Georg Frederik Ahrensborg ClausenM28DenmarkDEN1924 Summer1924SummerParisCyclingCycling Men's Road Race Individual
110345Marinus Cornelis Dick SigmondM26NetherlandsNED1924 Summer1924SummerParisFootballFootball Men's Football
54193Thodore Tho JeitzM26LuxembourgLUX1924 Summer1924SummerParisGymnasticsGymnastics Men's Individual All-Around
5
Find all athletes who were older than 40 years when they won either Bronze or Silver medals
View solution

Find all athletes who were older than 40 years when they won either Bronze or Silver medals.

Tables: olympics_athletes_events

BETWEEN: Range Filtering

BETWEEN filters for values within a range, including both endpoints.

6
Try BETWEEN Filtering

Find employees with a salary between 70000 and 100000 (inclusive).

Tables: techcorp_workforce
BETWEEN Is Inclusive

BETWEEN includes both endpoints. The query above includes employees earning exactly 70,000 and exactly 100,000.

BETWEEN is Shorthand

BETWEEN is equivalent to using >= and <= with AND:

SQL
-- These are identical:
WHERE salary BETWEEN 70000 AND 100000

WHERE salary >= 70000
  AND salary <= 100000

BETWEEN is just cleaner to read.

BETWEEN with Dates

BETWEEN is especially useful for date ranges:

SQL
SELECT
  first_name,
  last_name,
  joining_date
FROM techcorp_workforce
WHERE joining_date BETWEEN '2022-01-01' AND '2022-12-31';

This gets all employees who joined in 2022. Now try it with orders — get all orders from March 2019.

Keep in mind that Oracle doesn't implicitly cast string literals to dates — use a DATE literal or TO_DATE(). The other three dialects accept the plain string format directly.

Table: orders
idcust_idorder_dateorder_detailstotal_order_cost
132019-03-04Coat100
232019-03-01Shoes80
332019-03-07Skirt30
472019-02-01Coat25
572019-03-10Shoes80
7
Find Orders Within a Date Range

Find all orders placed during March 2019.

Tables: orders
BETWEEN and Timestamps

If your date column includes time (timestamps), BETWEEN '2019-03-01' AND '2019-03-31' might miss records from March 31st after midnight. For timestamps, it's safer to use: WHERE date_col >= '2019-03-01' AND date_col < '2019-04-01'. We'll cover this in Module 5.

NOT BETWEEN

Exclude a range with NOT BETWEEN:

SQL
SELECT
  first_name,
  last_name,
  salary
FROM techcorp_workforce
WHERE salary NOT BETWEEN 70000 AND 100000;

This returns employees earning less than 70,000 or more than 100,000.

Combine BETWEEN with other conditions.

Table: lyft_rides
indexweatherhourtravel_distancegasoline_cost
0cloudy724.471.13
1cloudy2323.671.99
2sunny1720.930.86
3rainy229.580.85
4rainy716.110.95
8
Find all Lyft rides which happened on rainy days before noon
View solution

Find all Lyft rides which happened on rainy days before noon.

Tables: lyft_rides

Combining LIKE, IN, and BETWEEN

These operators work with AND and OR just like any other condition:

SQL
SELECT
  first_name,
  last_name,
  department,
  salary
FROM techcorp_workforce
WHERE department IN ('HR', 'Engineering')
  AND salary BETWEEN 80000 AND 200000
  AND first_name LIKE 'V%';

This finds HR or Engineering employees, earning 80k to 200k, whose first name starts with V.

Try It

Table: orders
idcust_idorder_dateorder_detailstotal_order_cost
132019-03-04Coat100
232019-03-01Shoes80
332019-03-07Skirt30
472019-02-01Coat25
572019-03-10Shoes80
9
Combine LIKE and BETWEEN

Find orders where the product name starts with 'S' and the total cost is between 50 and 100.

Tables: orders

Key Takeaways

  • LIKE uses % (any characters) and _ (single character) for pattern matching.
  • IN checks if a value matches any item in a list.
  • BETWEEN filters for values within a range (inclusive on both ends).
  • All three can be negated with NOT.
  • These operators combine with AND/OR like any other condition.

What's Next

There's one more filtering concept to cover: NULL values. They behave differently than you might expect, and handling them correctly is essential. That's the focus of the next lesson.