Surfalytics
SQL course overview

Basics of selection I · Topic 7 Free

Pattern matching: LIKE

Data model — the tables you will query
N 1 N 1 N 1 N 1 cohorts id PK INT name TEXT start_date DATE format TEXT students id PK INT name TEXT city TEXT country TEXT cohort_id FK INT background TEXT joined_at DATE courses id PK INT title TEXT category TEXT difficulty TEXT hours INT enrollments id PK INT student_id FK INT course_id FK INT enrolled_at DATE completed_at DATE score INT job_offers id PK INT student_id FK INT company TEXT role TEXT city TEXT salary_usd INT remote BOOLEAN offer_date DATE

How the tables join

Relationship Cardinality Join condition
students.cohort_id → cohorts.id many students → one cohort JOIN cohorts ON students.cohort_id = cohorts.id
enrollments.student_id → students.id many enrollments → one student JOIN students ON enrollments.student_id = students.id
enrollments.course_id → courses.id many enrollments → one course JOIN courses ON enrollments.course_id = courses.id
job_offers.student_id → students.id many job offers → one student JOIN students ON job_offers.student_id = students.id

In short: each student belongs to one cohort. Students enroll in courses through enrollments, and their job offers land in job_offers.

= needs the exact value. LIKE matches text against a pattern.

The two wildcards

WildcardMatches
%any sequence of characters, including nothing
_exactly one character
WHERE title LIKE 'Data%'     -- starts with "Data"
WHERE title LIKE '%SQL%'     -- contains "SQL" anywhere
WHERE name  LIKE '%ova'      -- ends with "ova"
WHERE name  LIKE 'J_hn%'     -- John, Jahn... one character between J and hn

A pattern with no wildcards behaves like =.

Case sensitivity: ILIKE

LIKE is case-sensitive: '%engineer%' will not match 'Data Engineer'. Postgres adds ILIKE — same syntax, ignores case:

WHERE role ILIKE '%engineer%'

(Standard-SQL alternative: WHERE LOWER(role) LIKE '%engineer%' — useful to know for other databases.)

NOT LIKE

Exclude a pattern:

WHERE title NOT LIKE '%SQL%'

Performance note

A pattern starting with % (like '%analytics') cannot use a normal index — the database must scan every row. Fine on small tables; on billions of rows this becomes a real design question. Keep it in the back of your mind for interviews.

When patterns get more complex than “starts with / contains / ends with”, you need regular expressions — the next topic.

Practice

A real Postgres database runs in your browser. Solve the tasks, or just experiment.