LIKE, IN and BETWEEN
Handout
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
Matching text patterns with LIKE
= only matches text exactly. To match a pattern, use LIKE with two wildcards:
%stands for any run of characters (including none)_stands for exactly one character
SELECT name FROM student WHERE name LIKE 'S%';
'S%' means "starts with S". '%a%' means "contains an a". '_o%' means "an o as the second letter".
Lists and ranges: IN and BETWEEN
Two more handy tests:
IN (…)matches any value in a list:WHERE form IN ('11A', '11C')BETWEEN low AND highmatches a range, including both ends:
SELECT name, score FROM student WHERE score BETWEEN 70 AND 90;
Both are shorter than writing several ORs.
Show the name of every student whose name contains the letter a. Use LIKE with %.
Click Run to see the output here.
Show the name and score of students whose score is between 70 and 90 (inclusive). Use BETWEEN.
Click Run to see the output here.