0% found this document useful (0 votes)
9 views10 pages

LIKE Keyword - Notes Lyst2976

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

LIKE Keyword - Notes Lyst2976

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

LIKE Operator

The LIKE command is used in a WHERE clause to search for a specified


pattern in a column.

You can use two wildcards with LIKE:

● % - Represents zero, one, or multiple characters


● _ - Represents a single character (MS Access uses a question mark (?)
instead)

Scenario 1: Write a query to display first name and last name of the employee
whose first name starts with ‘AN’

Here we need to match first two characters as an after that any number of
characters means ‘an%’is the pattern matching

SELECT
first_name,last_name
FROM
employee
WHERE
first_name
LIKE 'an%';

Output:

first_name last_name

andy lumb

anjel nair

1
Scenario 2: Write a query to display the first name and last name of the
employee whose first name has the second character ‘o’.

Here we are matching for second character as o, so there should be any single
character before o(_o) and after o any number of characters so ‘_o%’ is the
pattern matching

SELECT
first_name,last_name
FROM
employee
WHERE
first_name
LIKE '_o%';

Output:

first_name last_name

tom taylor

rohan sharma

john king

Scenario 3: Write a query to display first name and last name of the employee
whose last name ends with ‘r’

SELECT first_name,last_name FROM sql_notes.employee WHERE


last_name LIKE '%r';

2
Output:

first_name last_name

tom taylor

anjel nair

ram kumar

Scenario 4: Write a query to display first name and last name of the employee
whose last name contain substring ‘MA’

SELECT first_name,last_name FROM sql_notes.employee WHERE


last_name LIKE '%ma%';

Output:
first_name last_name

ram kumar

rohan sharma

Scenario 5: Write a query to display first name and last name of the employee
whose first name begins with ‘r’ and at least 3 characters in length

SELECT first_name,last_name FROM sql_notes.employee WHERE


first_name LIKE 'r__%';

Output
first_name last_name

ram kumar

3
rohan sharma

Scenario 6: Write a query to display first name and last name of the employee
whose first name begins contains ‘A’

SELECT
first_name, last_name
FROM
employee
WHERE
first_name
LIKE '%a%';

Output:

first_name last_name

andy lumb

anjel nair

ram kumar

rohan sharma

Scenario 7: Write a query to display first name and last name of the employee
whose last name has last third character ‘L’

SELECT
first_name,last_name

4
FROM
employee
WHERE
last_name LIKE '%l__';

Output:
first_name last_name

tom taylor

mike whalen

Scenario 8: Write a query to display first name and last name of the employee
whose last name has third character and last character ‘a’

SELECT
first_name,last_name
FROM
employee
WHERE
last_name LIKE '__a%a';

Output:

first_name last_name

rohan sharma

Scenario 9: Write a query to display first name and last name of the employee
whose first name has two consecutive ‘ll’

SELECT
first_name,last_name

5
FROM
employee
WHERE
first_name LIKE '%ll%';

Output:

first_name last_name

kelly davis

Scenario 10: Write a query to display first name and last name of the employee
whose first name is 5 characters long and has third character ‘h’

SELECT
first_name,last_name
FROM
employee
WHERE
first_name LIKE '__h__';

Output:

first_name last_name

rohan sharma

Scenario 11: Write a query to display first name and last name of the employee
whose last name consists of character ‘a’ twice in it

SELECT

6
first_name,last_name
FROM
employee
WHERE
last_name LIKE '%a%a%';

Output:

first_name last_name

rohan sharma

Scenario 12: Write a query to display first name, last name and salary of the
employee who are earning 6 digit salary

SELECT
first_name,last_name,salary
FROM
employee
WHERE
salary LIKE '______';

Output:
first_name last_name salary

john king 124200

Scenario 13: Write a query to display first name, last name and salary of the
employee whose salary starts with 4

7
SELECT
first_name,last_name,salary
FROM
employee
WHERE
salary LIKE '4%';

8
Output:
first_name last_name salary

andy lumb 42200

anjel nair 42200

Scenario 14: Write a query to display first name, last name and hire date of the
employee who are hired in the year 2018

SELECT first_name,last_name,hire_date
FROM employee
WHERE hire_date LIKE '2018%';

Output:
first_name last_name hire_date

ram kumar 2018-12-26

Scenario 15: Write a query to display first name, last name and hire date of the
employee who are hired in the month of february

SELECT first_name,last_name,hire_date
FROM employee
WHERE hire_date LIKE '____-02%';

Output:
first_name last_name hire_date

andy lumb 2021-02-27

rohan sharma 2021-02-09

9
john king 2021-02-09

Scenario 16: Write a query to display first name, last name and hire date of the
employee whose salary has last 3 digit as 0’s

SELECT
first_name,last_name,salary
FROM
employee
WHERE
salary LIKE '%000';

Output:
first_name last_name salary

kelly davis 78000

10

You might also like