GATE

LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards used in conjunction with the LIKE operator:LIKE Operator

  1. The percent sign (%)
  2. The underscore (_)

The percent sign represents zero, one or multiple characters and The underscore represents a single character.

Syntax:- SELECT column1, column2, … FROM table_name WHERE columnn LIKE pattern;

This SQL tutorial explains how to use the SQL LIKE condition (to perform pattern matching) with syntax, examples, and practice exercises.

In this example, we have a table called Employ with the following data:



 

Id Name Age
1 Sunil 30
2 SuNiL 330
3 Sunita 3
4 Vineet 20
5 Rahul 30
6 Rohan
7 R 320
8 Sn 40
9 Snp 60
10 Qrs 30
11 Psz 30
12 Ers 40
Coding

Example :- 1 Select Name from Employ where Name like ‘s’ ;

Solution :- It will select the name which contain value as s only but here there is no such value in column Name so it will output as NO ROWS SELECTED.

Example :- 2 Select Name from Employ where Name like ‘s_’ ;

Solution :- It will select the name which starts with ‘s’ and is 2 letter word and second position consist of any letter. Hence output will be as follow:-

Name
Sn

Example :- 3  Select Name from Employ where Name like ‘%s’ ;

Solution :- It will select the name which ends with ‘s’ and length may be zero or infinite.Hence output will be as follow:-

Name
Qrs
Ers

Example :- 4 Select Name from Employ where Name like ‘%s%’ ;

Solution :- It will select the name in which string consist of ‘s’.Hence output will be as follow:-

 

Name
Sunil
SuNiL
Sunita
Sn
Snp
Qrs
Psz
Ers

 

Example :- 5 Select Name from Employ where Name like ‘%s_’ ;

Solution :-It will select the string in which second last letter is ‘s’ and minimum length of string is 2.Hence output will be as follow:-

Name
Sn
Psz

Note:- These is very important topic as far as competitive coding, placements or GATE is concerned.So practice it as much as possible with various patterns and if anyone of you have any doubt let us know in the comment box below and if you have any other doubts regarding gate feel free to write to us.

To Download the pdf of these post Click Here..!!

Myself Shubham an computer science engineer with lots of dream having an unseen attitude with lots of thoughts

Tagged , , , ,

About Shubham Lashkan

Myself Shubham an computer science engineer with lots of dream having an unseen attitude with lots of thoughts
View all posts by Shubham Lashkan →

Leave a Reply