Patterns, order, and limits

Lesson 5 of 18 · SQL foundations

LIKE matches a text pattern: % represents zero or more characters and _ exactly one character. In SQLite, default LIKE case folding covers ASCII, not all Unicode. Collations and behavior vary across engines.

SELECT title FROM books WHERE title LIKE '%o%' ORDER BY title;

The titles are Clay Roads, Moss Atlas, and River Notes. A pattern like '_oss%' matches Moss Atlas: one character, then oss, then any suffix. Literal wildcard characters require an ESCAPE clause and a chosen escape character.

SELECT title, pages FROM books WHERE pages IS NOT NULL
ORDER BY pages DESC, id ASC LIMIT 2;

This returns Clay Roads (200) and Moss Atlas (120). DESC means descending; ASC, the default, means ascending. The id tie-breaker makes equal-page rows deterministic. LIMIT 2 caps the result after ordering; without ordering, the chosen two are not predictable. Other dialects may use FETCH FIRST or TOP.

Chapter index · Course index