0 of 3 saved

Database Management Systems3 min read

LIKE clause

Read this chapter, write the program on this page, then pass a short check to open the next one.

In this tutorial we will learn how to use SQL LIKE clause in our query. Like clause is used as a condition in SQL query. Like clause compares data with an expression using wildcard operators.

SQL LIKE clause

In SQL, we have three types of operators. Among them, we have Logical Operators which are used for logical operations. LIKE is one of the logical operators. It is used in the condition in SQL query with the WHERE clause. LIKE operator compares data with an expression using wildcard characters to match the pattern given in the condition. It can only be applied to a string value.

SQL Wildcard Characters

A wildcard character is used to substitute one or more characters in a string. Wildcard characters are used with the SQL LIKE operator. Different databases use different wildcard characters. The following table illustrates different wildcard characters with descriptions, example, and which database use it.

like operator

Syntax of LIKE operator:

  SELECT column1, column2, ...
  FROM table_name
  WHERE column LIKE pattern;

Sample Table

Let us use the below Customers table to perform the LIKE operator.

sql table

Now, let us look at different examples of the LIKE operator using wildcard characters.

Using % character

Example 1:

The below SQL query selects all customers with a name starting with "a". In this case, the % operator is always placed at the end.

  SELECT * FROM Customers
  WHERE name LIKE 'a%';

Output:

sql table

Example 2:

The below SQL query selects all customers with a name ending with a. In this case, the % operator is always placed at the start.

  SELECT * FROM Customers
  WHERE name LIKE '%a';

Output:

sql table

Example 3:

The below SQL query selects all customers with a name that have "or" in any position. In this case, the % operator is on either end.

  SELECT * FROM Customers
  WHERE name LIKE '%or%';

Output:

sql table

Example 4:

The below SQL query selects all customers with a name that starts with "a" and ends with "o". In this case, the % operator is always placed in the middle

  SELECT * FROM Customers
  WHERE name LIKE 'a%o';

Output:

sql table

Example 5:

The below SQL query selects all customers with a name that does NOT start with "a". It uses the NOT operator.

  SELECT * FROM Customers
  WHERE name NOT LIKE 'a%';

Output:

sql table

Using _ character

Example 1:

The below SQL query selects all customers with a City starting with any character, followed by "_ondon".

  SELECT * FROM Customers
  WHERE city LIKE '_ondon';

Output:

sql table

Example 2:

The below SQL query selects all customers with a City starting with "L", followed by any character, followed by "n", followed by any character, followed by "on".

  SELECT * FROM Customers
  WHERE city LIKE 'L_n_on';

Output:

sql table

Using % and _ character

Example 1:

The below SQL query selects all customers with a name that have "r" in the second position.

  SELECT * FROM Customers
  WHERE name LIKE '_r%';

Output:

sql table

Example 2:

The below SQL query selects all customers with a name that starts with "a" and is at least 3 characters in length.

  SELECT * FROM Customers
  WHERE name LIKE 'a__%';

Output:

sql table

Conclusion

In this article, we discussed the LIKE operator of SQL. We also learned various SQL wildcards operators used by different databases. Note that all databases do not support all wildcard characters, so make sure to choose the right character from the above-given table.