SQL LIKE keyword is a keyword that is used in SQL WHERE clause. Basically, SQL LIKE keyword allow us to do a search based on a pattern rather than specifying exactly what is desired (as in SQL IN Keyword) or spell out a range (as in SQL BETWEEN Keyword). The syntax for SQL Like Keyword is given below:
SELECT "column_name"
FROM "table_name"
WHERE "column_name" LIKE {PATTERN}
PATTERN is nothing but a wildcards. Here are some examples that how to make pattern in SQL.
| 'A_Z' | All string that starts with 'A', another character, and end with 'Z'. For example, 'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not because there are two characters between A and Z as an alternative of one. |
| 'ABC%' | All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both satisfy the condition |
| '%XYZ' | All strings that end with 'XYZ'. e.g. 'WXYZ' and 'ZZXYZ' would both satisfy the condition. |
| '%TE%' | All string that contain the pattern 'TE' anywhere. like, 'Kate' and 'Cute' would both satisfy the condition. |
let us assum below table that contains the contact informations about the students. table name: students_information
| student_id | name | address | location | city |
| ST101 | stalin | #4b,k street | west mambalam | Chennai |
| ST102 | arun | #46A,appolo street | west mambalam | Chennai |
| ST103 | anjelina | #23b,h street | T.Nagar | Chennai |
| ST104 | aniruthra | #4b,3rd street | mandaveli | Chennai |
Executed Query:
SELECT * FROM students_information WHERE name LIKE '%ru%'
Result:
| student_id | name | address | location | city |
| ST102 | arun | #46A,appolo street | west mambalam | Chennai |
| ST104 | aniruthra | #4b,3rd street | mandaveli | Chennai |
Description:
The query will return the result that which name is contain the pattern 'ru' in any place of the name.
Executed Query:
SELECT * FROM students_information WHERE location LIKE 'we%'
Result:
| student_id | name | address | location | city |
| ST101 | stalin | #4b,k street | west mambalam | Chennai |
| ST102 | arun | #46A,appolo street | west mambalam | Chennai |
Description:
The query will return the result that which is contain the pattern 'we' in start of the location.