2.3 Pattern Matching (LIKE), Multi-Column ORDER BY & Pagination
Search text with LIKE & ILIKE wildcards, escape literal % characters, sort cleanly with multi-column tie-breakers and NULLS LAST, and compare OFFSET vs Keyset (Cursor) Pagination at scale.
1. Text Pattern Matching with LIKE (and ILIKE)
Need to search emails, usernames, or product titles? SQL's LIKE operator uses just 2 wildcards:
| Wildcard | Matches | Pattern Example | Matches | Does NOT Match |
|---|---|---|---|---|
% | 0 or more characters | 'Py%' | 'Py', 'Python', 'PyTorch' | 'MyPython' |
%...% | Substring anywhere | '%sql%' | 'mysql', 'postgresql', 'sql' | 'sequel' |
_ | Exactly 1 character | '_at' | 'cat', 'hat', 'bat' | 'flat', 'at' |
LIKE vs ILIKE vs LOWER() (PostgreSQL vs MySQL)
| Feature | PostgreSQL | MySQL (Default Collation) |
|---|---|---|
LIKE | Case-sensitive ('sql' ≠ 'SQL') | Case-insensitive by default |
ILIKE | ✅ Native case-insensitive LIKE | ❌ Not supported (use LIKE) |
| Portable way | LOWER(name) LIKE 'a%' | LOWER(name) LIKE 'a%' |
-- Find all users with a @bigoslate.com email (portable across DBs)
SELECT name, email
FROM users
WHERE LOWER(email) LIKE '%@bigoslate.com';
-- Find literal '%' inside text (e.g., '50% off') using ESCAPE
SELECT title
FROM promos
WHERE title LIKE '%50\%%' ESCAPE '\';
Index Performance Rule:
WHERE name LIKE 'Aar%'(prefix match) CAN use a B+ Tree Index → blazing fast (O(log N)).WHERE name LIKE '%aar%'(leading%) CANNOT use a normal B+ Tree Index → forces a full table scan (O(N)). For heavy full-text search, use PostgreSQL Full-Text Search (tsvector) or Trigram (pg_trgm) indexes!
2. Multi-Column Sorting (ORDER BY) & NULLS LAST
When two rows have the same value in your primary sort column, add a secondary tie-breaker column so the output order is 100% deterministic:
-- Sort by highest score first; break ties alphabetically by name
SELECT name, course, score
FROM students
ORDER BY score DESC, name ASC;
How Different Databases Sort NULLs in ORDER BY
| Database | ORDER BY score ASC | ORDER BY score DESC |
|---|---|---|
| PostgreSQL / Oracle | NULLs appear LAST | NULLs appear FIRST ⚠️ |
| MySQL / SQLite / SQL Server | NULLs appear FIRST | NULLs appear LAST |
Always be explicit when sorting nullable columns in PostgreSQL:
SELECT name, score
FROM students
ORDER BY score DESC NULLS LAST, name ASC;
3. Pagination: LIMIT & OFFSET vs Keyset (Cursor) Pagination
Basic Page-Number Pagination (LIMIT + OFFSET)
Formula: OFFSET = (page_number - 1) * page_size
-- Page 1 (rows 1–10)
SELECT id, title FROM articles ORDER BY id DESC LIMIT 10 OFFSET 0;
-- Page 2 (rows 11–20)
SELECT id, title FROM articles ORDER BY id DESC LIMIT 10 OFFSET 10;
Watch Multi-Column ORDER BY + LIMIT / OFFSET Execute Live
| name | score |
|---|---|
| Riya | 92 |
| Meera | 96 |
| Aarav | 92 |
| Kabir | 88 |
OFFSET Pagination vs Keyset (Cursor) Pagination (System Design Classic!)
| Dimension | LIMIT 10 OFFSET 100000 | Keyset / Cursor (WHERE id < last_id LIMIT 10) |
|---|---|---|
| Speed on Page 10,000 | ❌ Slow (O(N)) — scans & discards 100,000 rows! | ✅ Instant (O(log N)) — jumps straight via Index! |
| New Row Inserted Mid-Scroll | ❌ Causes duplicate rows on the next page | ✅ Zero duplicates or skipped items |
| Jump Directly to Page 47? | ✅ Yes (admin dashboards) | ❌ Only Next / Previous (infinite scroll feeds) |
-- Keyset / Cursor Pagination (used by X, Instagram, Slack & GitHub feeds)
-- Client sends the last seen id from Page 1 (e.g., 94820):
SELECT id, title, created_at
FROM articles
WHERE id < 94820
ORDER BY id DESC
LIMIT 10;
4. Quick Check Your Understanding
Module 2.3 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Test regular expressions live with color-coded match highlights, capture group inspection, plain-English token breakdown, find-and-replace, and Python/JS snippets.
Compare two versions of text, source code, config files, or JSON payloads side-by-side using exact LCS diffing with recursive JSON key sorting and patch export.