CodeEraFree roadmap

SQL Practice / LIKE and wildcards

SQL LIKE and Wildcards: practice questions with answers

8 questions with solved queries and real output. Outputs checked on MariaDB 10.11 (MySQL-compatible) on 23 November 2026.

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1002RobertLee694101998-09-02M2019-12-26IT
1003MaryWood395181997-11-07F2019-09-12Business Development
1004JohnCole362641999-02-01M2021-04-14HR
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1007DavidMills394001998-04-26M2021-10-17IT
1008WilliamHill263921999-10-21M2019-09-30Business Development
1009LindaPotter552311998-01-24F2021-08-07Finance
1010RichardMartin256001996-08-07M2021-03-23IT
1011SusanHall541521999-12-27F2020-08-07Finance
1012JosephTurner605561997-05-13M2019-09-02Marketing
1013MaryGill582461999-01-21F2021-04-30Business Development
1014ThomasRyan236761996-10-12M2022-03-02IT
1015CharlesWilliams572262000-01-29M2019-11-05NULL
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1017NancyParker669001996-10-24F2022-01-09Finance
1018BettyBarker352881998-11-13F2019-12-26HR
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1020DanielElliott440121999-01-21M2023-01-08IT
1021DavidMillsNULL1998-01-24M2019-12-26IT
1022SarahHarrison446921996-03-20F2020-05-03Finance
1023MatthewHill413651998-05-16M2019-05-23IT
1024AnthonyMiller567941996-06-30M2019-02-28Business Development
1025MarkWood651201998-10-29M2020-07-30Business Development
1026ElizabethWalker470301998-08-11F2021-06-18NULL
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1028JessicaClark638361998-01-24F2021-05-23IT
1029NancyCole636692000-01-26F2019-12-26Finance
1030JoeBrooks440001999-08-05M2021-03-09Business 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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1003MaryWood395181997-11-07F2019-09-12Business Development
1013MaryGill582461999-01-21F2021-04-30Business Development
1017NancyParker669001996-10-24F2022-01-09Finance
1018BettyBarker352881998-11-13F2019-12-26HR
1024AnthonyMiller567941996-06-30M2019-02-28Business Development
1029NancyCole636692000-01-26F2019-12-26Finance

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1006MichaelWatts238001999-04-16M2022-06-09HR
1007DavidMills394001998-04-26M2021-10-17IT
1019ChristopherGibson593411998-04-23M2019-05-02Marketing

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_nofirst_namedepartment
1002RobertIT
1003MaryBusiness Development
1004JohnHR
1006MichaelHR
1007DavidIT
1008WilliamBusiness Development
1009LindaFinance
1010RichardIT
1011SusanFinance
1013MaryBusiness Development
1014ThomasIT
1016JessicaIT

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1025MarkWood651201998-10-29M2020-07-30Business Development
1026ElizabethWalker470301998-08-11F2021-06-18NULL

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_namelast_name
JamesBond
JohnCole
JenniferGill
JosephTurner
JessicaClark
JessicaClark

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1005JenniferGill489411996-06-05F2019-06-28Marketing
1007DavidMills394001998-04-26M2021-10-17IT
1023MatthewHill413651998-05-16M2019-05-23IT

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_nofirst_namelast_name
1011SusanHall

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_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1006MichaelWatts238001999-04-16M2022-06-09HR
1026ElizabethWalker470301998-08-11F2021-06-18NULL

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.