SQL · Module 02: Filtering, NULL Logic & SortingTutorial 6 of 24

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.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. Text Pattern Matching with LIKE (and ILIKE)

Need to search emails, usernames, or product titles? SQL's LIKE operator uses just 2 wildcards:

WildcardMatchesPattern ExampleMatchesDoes 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)

FeaturePostgreSQLMySQL (Default Collation)
LIKECase-sensitive ('sql' ≠ 'SQL')Case-insensitive by default
ILIKE✅ Native case-insensitive LIKE❌ Not supported (use LIKE)
Portable wayLOWER(name) LIKE 'a%'LOWER(name) LIKE 'a%'
SQL
-- 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:

SQL
-- 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

DatabaseORDER BY score ASCORDER BY score DESC
PostgreSQL / OracleNULLs appear LASTNULLs appear FIRST ⚠️
MySQL / SQLite / SQL ServerNULLs appear FIRSTNULLs appear LAST

Always be explicit when sorting nullable columns in PostgreSQL:

SQL
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

SQL
-- 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

Tracing ORDER BY Tie-Breaker + LIMIT 2 OFFSET 1
Interactive SQL Execution Tracer · Step 1 of 3 (1. FROM students)
1SELECT name, score
2→FROM students
3ORDER BY score DESC, name ASC
4LIMIT 2 OFFSET 1;
Live Table State · 1. FROM students4 rows
namescore
Riya92
Meera96
Aarav92
Kabir88
What the DB engine just did: Raw table order is arbitrary. Notice Riya and Aarav are tied at 92.

OFFSET Pagination vs Keyset (Cursor) Pagination (System Design Classic!)

DimensionLIMIT 10 OFFSET 100000Keyset / 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)
SQL
-- 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

Q1.Which LIKE pattern can still take advantage of a standard B+ Tree index on the name column?
Q2.Why does LIMIT 10 OFFSET 500000 become slow on large tables?
Q3.Why should you ALWAYS pair LIMIT with ORDER BY?

Keep Exploring: Related Lessons & Tools

All Courses →