1242ERRORTier 1 — Safe✅ HIGH confidenceSubquery returns more than 1 row
What this means
Error 1242 (SQLSTATE 21000) is raised when a scalar subquery — one used in a context that expects a single value (e.g., WHERE col = (subquery)) — returns more than one row. The comparison operator = requires exactly one value on the right side.
Why it happens
- 1Subquery in WHERE col = (...) returns multiple rows instead of one
- 2Subquery used as a scalar expression in SELECT returns multiple rows
- 3Missing LIMIT 1 or missing aggregation on a subquery expected to be scalar
How to reproduce
Equality comparison against a subquery that returns multiple rows.
SELECT * FROM orders
WHERE customer_id = (
SELECT id FROM customers WHERE country = 'PT'
);
-- Multiple customers exist in PortugalFix 1: Use IN instead of = for multi-row subqueries
When any matching row is acceptable.
SELECT * FROM orders
WHERE customer_id IN (
SELECT id FROM customers WHERE country = 'PT'
);Why this works
IN accepts a set of values, making it the correct operator when the subquery may return multiple rows.
Fix 2: Add LIMIT 1 or aggregation to force a scalar result
When only one specific row is expected (e.g., the most recent).
SELECT * FROM orders
WHERE customer_id = (
SELECT id FROM customers WHERE country = 'PT'
ORDER BY created_at DESC
LIMIT 1
);Why this works
LIMIT 1 guarantees at most one row is returned, making the subquery safe to use with =.
Fix 3: Use EXISTS for existence checks
When checking if any matching row exists.
SELECT * FROM orders o
WHERE EXISTS (
SELECT 1 FROM customers c
WHERE c.id = o.customer_id AND c.country = 'PT'
);Why this works
EXISTS short-circuits on the first match and is not affected by how many rows the subquery returns.
What not to do
Blindly add LIMIT 1 without an ORDER BY
Why it's wrong: Without ORDER BY, the row returned is non-deterministic and results will vary across queries.
Sources
📚 Official docs: https://mariadb.com/kb/en/subqueries/
🔧 Source ref: MariaDB Server error code 1242 / ER_SUBQUERY_NO_1_ROW
📖 Further reading: MariaDB Subqueries
📖 Further reading: MariaDB IN Operator
Confidence assessment
✅ HIGH confidence
Stable.
See also
🔗 Related errors
📄 Reference pages