SQL LIKE Clause - The Coding Shala

Home >> Learn SQL >> SQL Like Clause

SQL LIKE Clause

The SQL 'Like' is used in a WHERE Clause to search for a specific pattern in a column means SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with LIKE operator - 
  1.  The percent sign(%) - represents zero, one or multiple characters.
  2.  The underscore(_) - represents a single character.
  • These symbols can be used together.
SQL LIKE Clause - The Coding Shala

SQL LIKE Clause Syntax

The basic SQL LIKE Clause syntax is as follows - 

 SELECT column1, column2, ...  
 FROM table_name  
 WHERE columnN LIKE pattern;

SQL LIKE Clause Examples 

The following 'emp' table will be used for the examples - 
Id     Name     Age     Address     Salary  
 1     Akshay     22     Pune        40000  
 2     Mohit      21     Delhi       42000  
 3     Akash      21     Delhi       45000  
 4     Nikhil     24     Mumbai      50000  
 5     Smith      24     Pune        50000  
 6     Akshay     22     Pune        50000  
 7     Nikhil     24     Mumbai      43000  

The following query will return all the rows that have Name starts with 'A' - 

SQL >> select * from emp  
        where Name like 'A%';  

 Output >>  
 Id     Name     Age     Address     Salary  
 1     Akshay     22     Pune       40000  
 3     Akash      21     Delhi      45000  
 6     Akshay     22     Pune       50000  

The following SQL query return all the records that have the second character of the Name is 'o' - 

SQL >> select * from emp  
        where Name like '_o%';;  

 Output >>  
 Id     Name     Age     Address     Salary  
 2     Mohit     21      Delhi       42000


Other Posts You May Like
Prev<< SQL Having Clause                                                                      NEXT >>SQL With Clause
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to Create a Simple Calculator - The Coding Shala

N-th Tribonacci Number Solution - The Coding Shala

Java Program to Convert Binary to Decimal - The Coding Shala

LeetCode - Shuffle the Array Solution - The Coding Shala

Introduction to Kotlin Programming Language for Backend Development - The Coding Shala