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.
In SQL, there are two types of wildcard operators that are used in the LIKE clause.
You can take a look at this table, student;
s_id | name | age |
---|---|---|
11 | Alexa | 21 |
12 | Nick | |
13 | Ada 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_id | name | age |
---|---|---|
11 | Alexa | 21 |
13 | Ada Chan |
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_id | name | age |
---|---|---|
13 | Ada Chan |
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_id | name | age |
---|---|---|
11 | Alexa | 21 |