SQL LIKE Operator – SQL TUTORIAL | Software Testing Material

SQL LIKE Operator:

The SQL LIKE operator is used in a WHERE clause to compare a value to similar values using wildcard operators.

There are two wildcard operators used in conjunction with the Like operator.

Percentage sign ( % )

Underscore sign ( _ )

SQL LIKE Syntax:

1
SELECT column_name(s) FROM table_name WHERE column_name LIKE somevalue;

Example – Like Operator:

1
SELECT * FROM SCOREBOARD WHERE Playername LIKE ‘Sachin’;

In the below image, you could find two SELECT queries. First SELECT query returns the complete records of the table SCOREBOARD where as the second SELECT query returns the values only if the Playername is Sachin. We used WHERE clause in the second SELECT query with the LIKE operator.

Example – Like Operator with Wildcard %:

1
SELECT * FROM SCOREBOARD WHERE Playername LIKE ‘D%’;

In the below image, you could find two SELECT queries. First SELECT query returns the complete records of the table SCOREBOARD where as the second SELECT query returns the values only if the Playername starts with letter D. We used WHERE clause in the second SELECT query with the LIKE operator and wildcard %.

Example – Like Operator with Wildcard % At The Beginning:

1
SELECT * FROM SCOREBOARD WHERE Playername LIKE ‘%h%’;

In the below image, you could find two SELECT queries. First SELECT query returns the complete records of the table SCOREBOARD where as the second SELECT query returns the values only if the Playername consists the letter h. We used WHERE clause in the second SELECT query with the LIKE operator and wildcard before and after the letter h

Example – Like Operator with Wildcard _:

1
SELECT * FROM SCOREBOARD WHERE Playername LIKE ‘_achin’;

Example – Like Operator with Wildcard _ Before And After:

1
SELECT * FROM SCOREBOARD WHERE Playername LIKE ‘_achi_’;

Example – Like Operator with Wildcard combination % and _:

1
SELECT * FROM SCOREBOARD WHERE Playername LIKE ‘_a%’;

NOT LIKE:

1
SELECT * FROM Table_Name WHERE SomeColumn_Name NOT LIKE ‘%Some_Value%’;

In the below image, you could find two SELECT queries. First SELECT query returns the complete records of the table SCOREBOARD where as the second SELECT query returns the values from the tables but it eliminates the values which has letter SacWe used WHERE clause in the second SELECT query with the NOT LIKE operator and the letters Sac with wild card % before and after the letters Sac.

In the next tutorial, we will see How To Use IN OPERATOR in SQL

Check out the complete SQL Tutorial by clicking on below link:

SQL Tutorial – Complete