Use the LIKE operator with % and _ wildcards to solve these exercises. The sample student table SQL dump is available for testing.
John.John.John anywhere.A and ending with n.no using a case-sensitive comparison.alex or deo.alex and John.alex or John.ro and marks in the 90s.Solution SQL file Create sample table SQL file
These exercises build on the WHERE clause and also practice combining filters with AND / OR and comparison behavior from the comparison operators tutorial.
USE my_db;
-- 1. Starts with John
SELECT * FROM student WHERE name LIKE 'John%';
-- 2. Ends with John
SELECT * FROM student WHERE name LIKE '%John';
-- 3. Contains John
SELECT * FROM student WHERE name LIKE '%John%';
-- 4. Starts with A and ends with n
SELECT * FROM student WHERE name LIKE 'A%n';
-- 5. Case-sensitive search for no
SELECT * FROM student WHERE BINARY name LIKE '%no%';
-- 6. Marks in the 90s but not 100
SELECT * FROM student WHERE CAST(mark AS CHAR) LIKE '9_';
-- 7. alex or deo
SELECT * FROM student WHERE name LIKE '%alex%' OR name LIKE '%deo%';
-- 8. alex and John
SELECT * FROM student WHERE name LIKE '%alex%' AND name LIKE '%John%';
-- 9. alex or John
SELECT * FROM student WHERE name LIKE '%alex%' OR name LIKE '%John%';
-- 10. ro in name and mark in the 90s
SELECT * FROM student WHERE name LIKE '%ro%' AND CAST(mark AS CHAR) LIKE '9_';
LIKE follows the collation of the compared string. The BINARY operator in question 5 forces a binary, case-sensitive comparison for the example.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.