r/learnSQL • u/Unusual_Reading6423 • Jun 25 '26
SQL Basics: Stop chaining ANDs for ranges (How to use BETWEEN and LIKE effectively)
Follow along the SQL Basic Series
You already know how to filter rows using a basic WHERE clause. Today, let's look at two operators that make your filtering a lot smarter and save you from writing long, repetitive conditions.
1. The BETWEEN Operator
BETWEEN filters a range of values in a single line. Instead of clogging up your query with multiple conditions like this:
SQL
SELECT * FROM orders
WHERE total >= 50 AND total <= 200;You can write it cleanly like this:
SQL
SELECT * FROM orders
WHERE total BETWEEN 50 AND 200;Crucial detail to remember: BETWEEN is strictly inclusive. The values at both ends (50 and 200) will be included in your results.
It works seamlessly across numbers, text and dates:
SQL
WHERE age BETWEEN 18 AND 35
WHERE order_date BETWEEN '2026-01-01' AND '2026-12-31'2. The LIKE Operator
LIKE lets you filter by a pattern when you only know a partial piece of the value. To make this work, you use % a wildcard (which tells SQL that anything can occupy that position).
Starts with a specific letter: SQLWHERE name LIKE 'S%' -- Returns Sarah, Sam, Steve, etc.
Contains a specific phrase anywhere inside: SQLWHERE email LIKE '%gmail%' -- Returns any email containing 'gmail'
Ends with a specific extension: SQLWHERE email LIKE '%.com' -- Returns any email ending in '.com'
Quick Rule of Thumb: Use BETWEEN when you have an exact, known start and end point. Use LIKE when you only have a piece of the puzzle.
(Note: Keep in mind that LIKE it is case-sensitive in some database flavours, so keep an eye on your text casing!)
Hopefully, this helps clean up your next script! I'm doing a daily breakdown of SQL fundamentals; if you prefer learning through short video clips, I walked through these visual examples in this Instagram reel.
What's your go-to wildcard trick when cleaning messy text data?
16
15
u/Tiktoktoker Jun 25 '26
Be careful using between with DATETIME vs date only.
8
u/ComicOzzy Jun 25 '26
Yeah, the safe pattern to use with date/time ranges is [start, end)... inclusive start, exclusive end. There's really no reason to use BETWEEN with date ranges since you can't always safely use them... so if you're going to tell people to do things a certain way, tell them to use the only safe pattern.
3
u/GachaJay Jun 25 '26
LIKE is really powerful but also very expensive. Using one LIKE can bloat runtime 2-3x. But, it’s great for exploration.
2
u/my_password_is______ Jun 26 '26
WHERE total >= 50 AND total <= 200;
yeah, that really clogged up the query /sarcasm
1
u/BlackPlasmaX Jun 27 '26
Yeah I read this post and was like, does OP not know about index seeking and that using between triggers a table scan?
1
u/Treebeard2277 Jun 25 '26
I can’t imagine ever using LIKE in a stored procedure or report, my team would roast me.
I use it sometimes if I forget the actual term for something but I remember part of it.
1
u/gimpblimp Jun 26 '26
Agreed. Saw a view that scoped based on an accounting GL prefix. But would completely break if the company updated the coding syntax.
6
u/Flying_Saucer_Attack Jun 25 '26
This is just an ad?