SQL LIKE Exercises

SQL LIKE pattern matching exercise

Use the LIKE operator with % and _ wildcards to solve these exercises. The sample student table SQL dump is available for testing.

  1. Display names that start with John.
  2. Display names that end with John.
  3. Display names containing John anywhere.
  4. Display names starting with A and ending with n.
  5. Display names containing lowercase no using a case-sensitive comparison.
  6. Display marks in the 90s but not 100.
  7. Display names containing alex or deo.
  8. Display names containing both alex and John.
  9. Display names containing alex or John.
  10. Display names containing ro and marks in the 90s.

Solution SQL file Create sample table SQL file

SQL LIKE exercise with AND, OR and NOT combinations

These exercises build on the WHERE clause and also practice combining filters with AND / OR and comparison behavior from the comparison operators tutorial.

Solutions

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.



Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer