Lesson 2 of 4

Progress: 0/4 exercises solved (0%)

← Previous

Solve all exercises below to unlock the next lesson.

IN, BETWEEN, LIKE

Lesson Description

Range and pattern matching.

Easy Project

Mini project: Turn this lesson into a real-world query artifact by writing one clean business report query and validating output quality.

IN, BETWEEN, LIKE — concise filtering

These operators clean up common patterns that would otherwise require many OR conditions.

IN — match a list of values

-- Instead of: WHERE department_id = 60 OR department_id = 80 OR department_id = 90
SELECT first_name, department_id
FROM employees
WHERE department_id IN (60, 80, 90);

NOT IN excludes the listed values:

WHERE department_id NOT IN (10, 20)

Caution: NOT IN returns no rows if the list contains a NULL. Use NOT EXISTS or IS NOT NULL guards when NULL is possible.

BETWEEN — inclusive range check

-- Products priced 30 to 80 (both endpoints included)
SELECT name, price
FROM products
WHERE price BETWEEN 30 AND 80;

Equivalent to price >= 30 AND price <= 80.

LIKE — pattern matching

% matches any sequence of characters. _ matches exactly one character.

-- Last names starting with 'K'
SELECT first_name, last_name
FROM employees
WHERE last_name LIKE 'K%';

-- Exactly 5-character last names
WHERE last_name LIKE '_____';

ILIKE — case-insensitive LIKE (PostgreSQL extension)

WHERE last_name ILIKE 'k%'   -- matches King, king, KING

Tips

  • BETWEEN works on dates too: WHERE hire_date BETWEEN '2000-01-01' AND '2010-12-31'.
  • LIKE patterns with a leading % (e.g., '%king') cannot use an index — they force a full scan.
  • For large string searches, consider pg_trgm or full-text search.

Learning context

This lesson is part of Filtering & Logic. Focus on the core idea in IN, BETWEEN, LIKE, then validate with deliberate practice.

What to master

  • Understand clause order and intent
  • Write deterministic result queries
  • Use clear aliases and readable structure

Common mistakes

  • Skipping ORDER BY in final outputs
  • Using joins before validating base rows
  • Not checking row counts after changes

High-level strategy

  • Start simple, then layer complexity
  • Run often and compare expected shape
  • Refactor with CTEs when logic grows

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

Logical reasoning for commands

WHERE

Why: Limits rows to only the business-relevant subset.

Memory cue: Filter early to reduce noise.

ORDER BY

Why: Makes output deterministic and reviewable.

Memory cue: No ORDER BY means no guaranteed row order.

AS alias

Why: Makes output columns readable for teams and reports.

Memory cue: If the name is clear, the query is easier to trust.

Concept check

Quick check: IN, BETWEEN, LIKE

3 quick questions. One at a time. Instant score at the end.

Exercise 1

Mid-priced products

Commerce Operations Intelligenceeasy

From the products table (store sandbox), return name and price for products priced between 30 and 80 inclusive.

Run a query to see results here.
Exercise 2

First names starting with A

Enterprise Workforce Analyticseasy

Return first_name and last_name of HR employees whose first name starts with the letter A.

Run a query to see results here.
Exercise 3

Patient names starting with C or D

Clinical Care Performanceeasy

From the hospital sandbox, return first_name and last_name of patients whose first_name starts with 'C' or 'D', ordered by first_name.

Run a query to see results here.
Exercise 4

Products in categories 1, 2, or 3

Commerce Operations Intelligenceeasy

From the store sandbox, return name, category_id, and price for products where category_id is 1, 2, or 3. Use IN. Order by category_id, then name.

Run a query to see results here.

Lesson 2 of 4

0/4 solved (0%)

← Previous

Finish this lesson to unlock next.