Fix "Subquery Returned More Than 1 Value" in SQL
By Sharon Ben-Moshe · September 11, 2026 · 2 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
How to resolve SQL Server Msg 512 and PostgreSQL 21000: Subquery returned more than 1 value. Replace scalar subqueries with JOINs, IN, or TOP 1.
Quick answer
This error (SQL Server Msg 512, PostgreSQL 21000) happens when a scalar subquery used in a SELECT list or comparison (=, <, >) returns multiple rows instead of exactly one. Resolve it by replacing '=' with 'IN', rewriting the scalar subquery as an INNER/LEFT JOIN, or restricting the subquery to a single row using LIMIT 1 or TOP 1 with ORDER BY.
FAQs
What is a scalar subquery in SQL?
A scalar subquery is a subquery enclosed in parentheses that must return exactly one column and at most one row. SQL engines treat it as a single value expression.
Why does '=' fail with multiple rows while 'IN' succeeds?
The equality operator '=' requires a single comparison value on each side. The IN operator tests set membership and accepts zero, one, or thousands of rows.
Is adding TOP 1 or LIMIT 1 always the right solution?
Not always. If the business logic expects a one-to-one relationship, multiple rows indicate dirty data or a missing join key. Adding LIMIT 1 masks underlying data defects by arbitrarily picking a row.
Can an aggregate function prevent this error?
Yes. If you need a single summarizing value (such as MAX, MIN, or AVG), using an aggregate inside the subquery guarantees that it returns exactly one row even when multiple matches exist.
Keep reading
Fix BigQuery "Unrecognized Name" Error: Scoping and Syntax Rules
By Sharon Ben-Moshe · Sep 29, 2026
Fix Google BigQuery's "Unrecognized name: column" error. Learn alias visibility rules across WHERE and QUALIFY, case sensitivity, and struct unnesting.
Read postSQLite "Datatype Mismatch" (Error 20): Type Affinity and Strict Tables
By Sharon Ben-Moshe · Sep 26, 2026
Understand SQLite Error 20 (datatype mismatch). Learn how SQLite type affinity, CHECK constraints, and STRICT tables enforce data integrity.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.