LIKE
clauseLIKE
clause is used in the condition in SQL query with the WHERE
clause. LIKE
clause compares data with an expression using wildcard operators to match pattern given in the condition.
There are two wildcard operators that are used in LIKE
clause.
%
: represents zero, one or more than one character._
: represents only a single character.LIKE
clauseConsider the following Student table.
s_id | s_Name | age |
---|---|---|
101 | Adam | 15 |
102 | Alex | 18 |
103 | Abhi | 17 |
SELECT * FROM Student WHERE s_name LIKE 'A%';
The above query will return all records where s_name starts with character 'A'.
s_id | s_Name | age |
---|---|---|
101 | Adam | 15 |
102 | Alex | 18 |
103 | Abhi | 17 |
_
and %
SELECT * FROM Student WHERE s_name LIKE '_d%';
The above query will return all records from Student table where s_name contain 'd' as second character.
s_id | s_Name | age |
---|---|---|
101 | Adam | 15 |
%
onlySELECT * FROM Student WHERE s_name LIKE '%x';
The above query will return all records from Student table where s_name contain 'x' as last character.
s_id | s_Name | age |
---|---|---|
102 | Alex | 18 |