Computer ScienceExam code: 0984

Database Concepts

Databases

A WHERE condition often needs to match a pattern in text rather than an exact value. SQL uses the LIKE keyword with the % wildcard, which stands for "any sequence of characters" (including no characters at all).

PatternMeaning
'J%'Starts with "J" (e.g. "John", "Jane", "Jake")
'%son'Ends with "son" (e.g. "Johnson", "Wilson")
'%ar%'Contains "ar" anywhere (e.g. "Mary", "Carter", "Bar")
'_'Underscore matches exactly one character (e.g. 'M_n' matches "Man" or "Min")

Pattern: countries starting with C

SELECT Name, Country
FROM Customers
WHERE Country LIKE 'C%';

For the table from section 6, this returns:

NameCountry
Maya HassanCanada

The other three records (Ireland, Japan, India) are excluded because their country names do not start with C.

Pattern: names containing "as"

SELECT Name
FROM Customers
WHERE Name LIKE '%as%';

Returns Maya Hassan. The % on both sides means "as" can appear anywhere in the name.

Build on this topic