SQL Tutorial menu

SQL LIKE, IN and BETWEEN

LIKE, IN and BETWEEN are short ways to write common WHERE conditions.

LIKE: Search for a Pattern

LIKE compares text with a pattern. Two wildcards stand for unknown characters:

WildcardMeaning
%Any number of characters (zero, one or many)
_Exactly one character

Names that start with "A":

Example

sql
SELECT name
FROM students
WHERE name LIKE 'A%';
name
Ali Khan
Ayesha Siddiqui

Names that contain "Iq" anywhere:

Example

sql
SELECT name
FROM students
WHERE name LIKE '%Iq%';
name
Ayesha Siddiqui
Sana Iqbal

"Siddiqui" also matches, because it contains the letters "iq". In MySQL and SQL Server LIKE usually ignores upper and lower case. In PostgreSQL it does not, and you would use ILIKE to ignore case.

Names whose second letter is "a":

Example

sql
SELECT name
FROM students
WHERE name LIKE '_a%';
name
Fatima Noor
Hamza Raza
Sana Iqbal

More LIKE Patterns

PatternMatches
'K%'Starts with K
'%n'Ends with n
'%an%'Contains "an"
'_____'Exactly five characters
'A%n'Starts with A and ends with n

Use NOT LIKE to find values that do not match.

IN: Match Any Value in a List

IN is a short way to write several OR conditions on the same column:

Example

sql
SELECT name, city
FROM students
WHERE city IN ('Lahore', 'Islamabad');
namecity
Ayesha SiddiquiLahore
Fatima NoorIslamabad
Sana IqbalLahore

This is the same as WHERE city = 'Lahore' OR city = 'Islamabad', but shorter. NOT IN gives the opposite.

IN with a Subquery

The list can come from another query. These are the courses that have at least one student:

Example

sql
SELECT title
FROM courses
WHERE id IN (SELECT course_id FROM students);
title
Web Development
Python Programming
Graphic Designing

Digital Marketing is missing because no student has course_id 4.

BETWEEN: Pick a Range

BETWEEN selects values inside a range. Both ends are included.

Example

sql
SELECT name, age
FROM students
WHERE age BETWEEN 19 AND 21;
nameage
Ali Khan19
Ayesha Siddiqui21
Fatima Noor20

NOT BETWEEN returns the values outside the range:

Example

sql
SELECT name, marks
FROM students
WHERE marks NOT BETWEEN 70 AND 90;
namemarks
Bilal Ahmed64
Fatima Noor92

BETWEEN also works with text and dates, for example WHERE join_date BETWEEN '2026-01-01' AND '2026-06-30'.

NOT IN has a trap: if the list contains a NULL, it returns no rows at all. When the list comes from a subquery, add WHERE column IS NOT NULL inside the subquery to be safe.

Exercise

Exercise

Which condition finds names that end with the letter "r"?

Learn SQL with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp