SQL Error Guide

SQL Error: Subquery Returns Multiple Rows (Fix + Examples)

Fix the SQL "more than one row returned by a subquery used as an expression" error. Learn what causes it and see examples of how to resolve it.

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

  1. Use IN instead of = when subquery may return multiple rows.
  2. Add LIMIT 1 if you only need one result.
  3. Use aggregate functions (MAX, MIN, AVG) to get single value.
  4. 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:

Related Content

From Our Blog

Still stuck?

Practice SQL in our sandbox environment with instant error feedback.