login

Using SQL LIKE

SQL LIKE is used to search string in a column of database table based on pattern matching. LIKE keyword usually uses in WHERE clause to limits the result set based on pattern matching. SQL provides two wildcard characters for use with LIKE. They are '%' (means any string of zero or more characters) and '_' (means a single character). Here is the syntax of using LIKE:

SELECT field_list 
FROM table 
WHERE field LIKE pattern

Let's take a look at following examples to see how SQL LIKE works.

We use the sample database table employees

name      salary 
--------  -------
jack      3000.00
mary      2500.00
newcomer  2000.00
anna      2800.00
Tom       2700.00
foo       4700.00

Now if you want to retrieve all employee whose name starts with 'j' character, you can use the query like this:

SELECT name,salary  
FROM employees  
WHERE name LIKE 'j%'

The database server returns this result set.

name    salary 
------  -------
jack    3000.00

You can use wildcard in both starting and (or) ending or even anywhere in the pattern string. But remember that LIKE only work with SQL character type. Here is an example of using '_' wildcard.

SELECT name, salary  
FROM employees  
WHERE name LIKE '_a%'

You've searched for any name start with any character, followed by character 'a' and end with any number of character. So the output result is:

name    salary 
------  -------
jack    3000.00
mary    2500.00
Read On