Getting the "more than one row returned by a subquery used as an expression" error in SQL? This guide explains what causes this error and how to fix it with practical examples.
What Causes This Error?
The subquery returns multiple rows error (21000) occurs when:
- Scalar subquery returning multiple values
- Using = instead of IN with subquery
- Subquery expected to return single value but data has duplicates
How to Fix It
- Use IN instead of = when subquery may return multiple rows.
- Add LIMIT 1 if you only need one result.
- Use aggregate functions (MAX, MIN, AVG) to get single value.
- Add more specific WHERE conditions to narrow results.
Example: Wrong vs Correct
❌ Code That Causes the Error
SELECT * FROM products WHERE price = (SELECT price FROM products WHERE category = 'Electronics'); -- Error: more than one row returned
✅ Corrected Code
SELECT * FROM products WHERE price IN (SELECT price FROM products WHERE category = 'Electronics'); -- Correct: IN handles multiple rows
Quick Checklist
- [ ] Verify column/table names are spelled correctly
- [ ] Check data types match expected values
- [ ] Review query syntax for missing keywords
- [ ] Ensure referenced tables/columns exist
Related SQL Errors
If you're troubleshooting SQL errors, you might also encounter: