- This topic is empty.
-
AuthorPosts
-
February 2, 2026 at 9:15 am #5988
❓ Question
I’m working on a CS50 SQL problem using SQLite.
I have anaddressestable with values like:165 Wadsworth Place 51 Edinboro Street 251 Purchase StreetWhen I run this query, I either get a syntax error or no results, even though the address clearly exists:
SELECT address FROM addresses WHERE address LIKE '%165';Why doesn’t this return
165 Wadsworth Place, and what is the correct way to write this query?
✅ Answer
This is a classic SQL wildcard misunderstanding — and one of the most common mistakes beginners make in CS50.
Let’s break it down step by step.
🧠 Understanding
LIKEand%in SQLIn SQL, the
%symbol is a wildcard that represents any number of characters.But where you place
%matters a lot.Meaning of different patterns:
Pattern What it matches '165%'Strings that start with 165'%165'Strings that end with 165'%165%'Strings that contain 165anywhere
❌ Why your query returned nothing
You wrote:
WHERE address LIKE '%165';This means:
“Find addresses that end with 165”
But your data is:
165 Wadsworth PlaceThis address starts with
165— it does not end with it.So SQLite is correct to return no rows.
✅ Correct query for your data
Since house numbers usually appear at the start of an address, this is the correct query:
SELECT address FROM addresses WHERE address LIKE '165%';✅ Output:
165 Wadsworth Place
❌ Another common syntax error to avoid
This query will always fail:
WHERE address LIKE %'165';Why?
%must be inside quotes- There must be a space after
LIKE
✅ Correct syntax:
column_name LIKE 'pattern'
🔍 When you’re unsure of column names
Always inspect the table first:
.schema addressesor:
PRAGMA table_info(addresses);This prevents guessing and saves time.
🧪 Bonus: Useful variations
Find addresses containing a street name
SELECT address FROM addresses WHERE address LIKE '%Wadsworth%';Case-safe search
SELECT address FROM addresses WHERE UPPER(address) LIKE '165%';
🎯 Key takeaway (CS50 exam tip)
- House numbers → start of string
- Street names → middle
- Suffixes (Street, Place, Ave) → end
So always choose your
%placement based on real-world data structure, not guesswork.
🏁 Final correct solution
SELECT address FROM addresses WHERE address LIKE '165%';
-
AuthorPosts
- You must be logged in to reply to this topic.

