SQL Basics | DML: Data Manipulation Language | LIKE

LIKE is another operator used in the WHERE clause. Basically, LIKE lets you find the data you need according to a pattern.

LIKE syntax

The LIKE clause syntax is as follows.

SELECT "field_name"
FROM "table_name"
WHERE "field_name" LIKE {pattern};

{pattern} includes wildcards. The following are some examples.

  • ‘A_Z’: A string that starts with A, has any single character after it, and ends with Z. ABZ and A2Z match this pattern, but AKKZ does not because there are two characters between A and Z, not one.
  • ‘ABC%’: A string that starts with ABC. For example, ABCD and ABCABC match this pattern.
  • ’%XYZ’: A string that ends with XYZ. For example, WXYZ and ZZXYZ match this pattern.
  • ’%AN%’: A string that contains AN. For example, LOS ANGELES and SAN FRANCISCO match this pattern.

When the last example above is used with the store_information table, it works as follows.

store_information table

store_name sales txn_date
Los Angeles 1500 Jan-05-2018
San Diego 250 Jan-07-2018
San Francisco 300 Jan-08-2018
Boston 700 Jan-08-2018

Enter the following command.

SELECT *
FROM Store_Information
WHERE Store_Name LIKE '%AN%';

The result is as follows.

store_name sales txn_date
LOS ANGELES 1500 Jan-05-2018
SAN DIEGO 250 Jan-07-2018
SAN FRANCISCO 300 Jan-08-2018