SQL LIKE clause

In SQL query, the LIKE clause is used in the condition with the WHERE clause. The LIKE clause compares data with an expression by using wildcard operators to match the pattern present in the condition.

 

Wildcard operators

In SQL, there are two types of wildcard operators that are used in the LIKE clause.

  • Percent sign %: It represents zero, one, or more than one character.
  • Underscore sign _: It represents only a single character.

 

Example of LIKE clause

You can take a look at this table, student;

s_idnameage
11Alexa21
12Nick 
13Ada Chan 

 

SELECT * FROM Student WHERE s_name LIKE 'A%';

The query above is used to return all records where s_name starts with the character 'A'.

s_idnameage
11Alexa21
13Ada Chan 

 

Using _ and %

SELECT * FROM Student WHERE s_name LIKE '_d%';

This query is used to return all records from the Student table where s_name contains 'd' as the second character.

s_idnameage
13Ada Chan 

 

Using % only

SELECT * FROM Student WHERE s_name LIKE '%x';

This query can be used to return all records from the Student table where s_name contains 'x' as the last character.

s_idnameage
11Alexa21