Menu
Coddy logo textTech

The LIKE keyword

Part of the Fundamentals section of Coddy's SQL journey — lesson 20 of 72.

The LIKE keyword is used to check the similarities of strings. For example, if we want to fetch all of the records that the name starts with the letter a then we will use the LIKE keyword.

Two main wildcards are used:

  • % - means any number of characters
  • _ - means exactly one character

For example:

  • %a - means any string that ends with a
  • a% - means any string that starts with a
  • %a% - means any string that contains a
  • _a% - means that the letter a is the second character in the string
  • %a__ - means that the string contains a in the 3rd from last place

 To use it we will write:

SELECT col1, col2, ...
FROM table1
WHERE col1 LIKE '%a__'
challenge icon

Challenge

Easy

Available tables and columns:

  • <strong>people</strong>: <strong>id</strong>, <strong>name</strong>

Fetch all of the people whose name starts with K (uppercase) and ends with a (lowercase) and order the results by the names in descending order.

Cheat sheet

The LIKE keyword is used to check string similarities with wildcards:

  • % - means any number of characters
  • _ - means exactly one character

Common patterns:

  • %a - ends with a
  • a% - starts with a
  • %a% - contains a
  • _a% - a is the second character
  • %a__ - a is in the 3rd from last place
SELECT col1, col2, ...
FROM table1
WHERE col1 LIKE '%a__'

Try it yourself

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals