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?
6
u/Flying_Saucer_Attack Jun 25 '26
This is just an ad?
16
u/chapaj Jun 25 '26
And LIKE can kill your use of indexes. Avoid it if you can.
15
u/Tiktoktoker Jun 25 '26
Be careful using between with DATETIME vs date only.
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
yeah, that really clogged up the query /sarcasm
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.