LIKE, Wildcards, IN & BETWEEN
LIKE compares text to a pattern. It is slower than exact matches on large tables but very useful for partial names or emails.
LIKE — pattern matching on text
LIKE compares text to a pattern. It is slower than exact matches on large tables but very useful for partial names or emails.
Real-life example: LIKE is like searching your phone contacts by typing the first few letters of a name.
SELECT full_name FROM students WHERE full_name LIKE 'A%';
SELECT full_name FROM students WHERE full_name LIKE '%n';Wildcards — % and _
% matches zero or more characters. _ matches exactly one character. On some databases you can use REGEXP for richer patterns.
Real-life example: % is a stretchy blank — 'A%' matches A, Ali, Aisha. _ is one single blank — 'A_sha' matches Aisha but not Ali.
-- Names with 'ee' anywhere
SELECT full_name FROM students WHERE full_name LIKE '%ee%';
-- Five-letter names starting with M
SELECT full_name FROM students WHERE full_name LIKE 'M____';IN — match any value in a list
IN checks if a column equals any value in a list. It is cleaner than long chains of OR.
Real-life example: IN is like a VIP list at the gate — Delhi, Mumbai, or Bangalore gets entry.
SELECT full_name, city
FROM students
WHERE city IN ('Delhi', 'Mumbai', 'Bangalore');
SELECT course_name
FROM courses
WHERE course_id IN (1, 3);BETWEEN — values in a range
BETWEEN low AND high includes both endpoints. Works well for numbers, dates, and sorted text.
Real-life example: BETWEEN is like 'students in grades 9 through 11' — both 9 and 11 count.
SELECT full_name, grade_level
FROM students
WHERE grade_level BETWEEN 9 AND 11;
SELECT student_id, score
FROM enrollments
WHERE score BETWEEN 70 AND 89;