LIKE matches text against a pattern. Two wildcards build the pattern: % stands for any number of characters (including none) and _ stands for exactly one character.
In MySQL, LIKE ignores upper and lower case by default. In PostgreSQL it is case-sensitive, and you use ILIKE to ignore case.
WHERE column LIKE 'a%' -- starts with a
WHERE column LIKE '%a' -- ends with a
WHERE column LIKE '%a%' -- contains a
WHERE column LIKE 'a__' -- a followed by exactly 2 characters
WHERE column NOT LIKE '%a%'
These questions use the employees table (30 rows). Create it first with the copy-and-paste setup script. Here is the data, so you can check your answers by eye.
See the employees table
Table: employees
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1001 | James | Bond | 45300 | 1998-01-19 | M | 2020-11-29 | Marketing |
| 1002 | Robert | Lee | 69410 | 1998-09-02 | M | 2019-12-26 | IT |
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1004 | John | Cole | 36264 | 1999-02-01 | M | 2021-04-14 | HR |
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1008 | William | Hill | 26392 | 1999-10-21 | M | 2019-09-30 | Business Development |
| 1009 | Linda | Potter | 55231 | 1998-01-24 | F | 2021-08-07 | Finance |
| 1010 | Richard | Martin | 25600 | 1996-08-07 | M | 2021-03-23 | IT |
| 1011 | Susan | Hall | 54152 | 1999-12-27 | F | 2020-08-07 | Finance |
| 1012 | Joseph | Turner | 60556 | 1997-05-13 | M | 2019-09-02 | Marketing |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1014 | Thomas | Ryan | 23676 | 1996-10-12 | M | 2022-03-02 | IT |
| 1015 | Charles | Williams | 57226 | 2000-01-29 | M | 2019-11-05 | NULL |
| 1016 | Jessica | Clark | NULL | 2000-01-25 | F | 2020-01-24 | IT |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
| 1020 | Daniel | Elliott | 44012 | 1999-01-21 | M | 2023-01-08 | IT |
| 1021 | David | Mills | NULL | 1998-01-24 | M | 2019-12-26 | IT |
| 1022 | Sarah | Harrison | 44692 | 1996-03-20 | F | 2020-05-03 | Finance |
| 1023 | Matthew | Hill | 41365 | 1998-05-16 | M | 2019-05-23 | IT |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development |
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
| 1027 | Barbara | Anderson | NULL | 1999-12-27 | F | 2021-03-14 | HR |
| 1028 | Jessica | Clark | 63836 | 1998-01-24 | F | 2021-05-23 | IT |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance |
| 1030 | Joe | Brooks | 44000 | 1999-08-05 | M | 2021-03-09 | Business Development |
How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.
Question 1Select all employees whose first name ends with 'y'.
Show the answer and the output
SELECT * FROM employees WHERE first_name LIKE '%y';Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1003 | Mary | Wood | 39518 | 1997-11-07 | F | 2019-09-12 | Business Development |
| 1013 | Mary | Gill | 58246 | 1999-01-21 | F | 2021-04-30 | Business Development |
| 1017 | Nancy | Parker | 66900 | 1996-10-24 | F | 2022-01-09 | Finance |
| 1018 | Betty | Barker | 35288 | 1998-11-13 | F | 2019-12-26 | HR |
| 1024 | Anthony | Miller | 56794 | 1996-06-30 | M | 2019-02-28 | Business Development |
| 1029 | Nancy | Cole | 63669 | 2000-01-26 | F | 2019-12-26 | Finance |
Question 2Select all employees whose date of birth is in April.
Show the answer and the output
SELECT * FROM employees WHERE MONTH(birth_date) = 4;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1019 | Christopher | Gibson | 59341 | 1998-04-23 | M | 2019-05-02 | Marketing |
You can also write birth_date LIKE '%-04-%' in MySQL, because a date is converted to text. It works, but MONTH() is clearer and works in more databases.
Question 3Select emp_no, first_name and department of employees whose department does not contain the word 'ing'.
Show the answer and the output
SELECT emp_no, first_name, department
FROM employees
WHERE department NOT LIKE '%ing%';Output
| emp_no | first_name | department |
|---|---|---|
| 1002 | Robert | IT |
| 1003 | Mary | Business Development |
| 1004 | John | HR |
| 1006 | Michael | HR |
| 1007 | David | IT |
| 1008 | William | Business Development |
| 1009 | Linda | Finance |
| 1010 | Richard | IT |
| 1011 | Susan | Finance |
| 1013 | Mary | Business Development |
| 1014 | Thomas | IT |
| 1016 | Jessica | IT |
Showing the first 12 of 24 rows.
Employees with no department (NULL) are not returned.
Different from the PDF: The PDF pattern was '%ing% ' with a trailing space, which matches nothing, so the query returned every employee. Fixed to '%ing%'.
Question 4Select all employees whose last name starts with 'w' and whose emp_no is more than 1015.
Show the answer and the output
SELECT * FROM employees WHERE last_name LIKE 'w%' AND emp_no > 1015;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1025 | Mark | Wood | 65120 | 1998-10-29 | M | 2020-07-30 | Business Development |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
Question 5Select first_name and last_name of employees whose first name starts with 'j' and is at least 4 characters long.
Show the answer and the output
SELECT first_name, last_name FROM employees WHERE first_name LIKE 'j___%';Output
| first_name | last_name |
|---|---|
| James | Bond |
| John | Cole |
| Jennifer | Gill |
| Joseph | Turner |
| Jessica | Clark |
| Jessica | Clark |
j___% means: j, then exactly three more characters (___), then anything (%). That makes at least 4 characters.
Question 6Select all employees whose last name contains 'ill' and whose salary is between 30000 and 50000.
Show the answer and the output
SELECT * FROM employees
WHERE last_name LIKE '%ill%' AND salary BETWEEN 30000 AND 50000;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1005 | Jennifer | Gill | 48941 | 1996-06-05 | F | 2019-06-28 | Marketing |
| 1007 | David | Mills | 39400 | 1998-04-26 | M | 2021-10-17 | IT |
| 1023 | Matthew | Hill | 41365 | 1998-05-16 | M | 2019-05-23 | IT |
Question 7Select emp_no, first_name and last_name of employees whose first name starts with 'S' and whose last name contains at least two 'l' characters.
Show the answer and the output
SELECT emp_no, first_name, last_name
FROM employees
WHERE first_name LIKE 'S%' AND last_name LIKE '%l%l%';Output
| emp_no | first_name | last_name |
|---|---|---|
| 1011 | Susan | Hall |
Question 8Select all employees whose joining date is in June 2022 or June 2021, and whose emp_no is not between 1011 and 1020.
Show the answer and the output
SELECT * FROM employees
WHERE (joining_date LIKE '2022-06-%' OR joining_date LIKE '2021-06-%')
AND emp_no NOT BETWEEN 1011 AND 1020;Output
| emp_no | first_name | last_name | salary | birth_date | gender | joining_date | department |
|---|---|---|---|---|---|---|---|
| 1006 | Michael | Watts | 23800 | 1999-04-16 | M | 2022-06-09 | HR |
| 1026 | Elizabeth | Walker | 47030 | 1998-08-11 | F | 2021-06-18 | NULL |
Because AND is checked before OR, the brackets are needed here.
Different from the PDF: The PDF had no brackets around the two OR conditions, so AND ran first and the emp_no rule applied to only one month. Brackets added.
Want these as PDFs? Download the question PDF or the answer PDF to practise offline.
