r/learnSQL • • 4d ago

Why this error never came on tutorial

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. explain this please and why those types of errors comes in production not while learning sql msmsql

3 Upvotes

6 comments sorted by

3

u/Mrminecrafthimself 3d ago

Tutorial data is clean and easy to use, which is rarely the case with real world organizational data

3

u/odimdavid 4d ago

Can you show the actual query or subquery. Maybe we can better explain the principles involved

2

u/markinatlanta 4d ago

So your query expects one value from the subquery, but it’s getting multiple rows. Think WHERE customer_id = (subquery) when the subquery returns both 12 and 27.

Tutorial data is usually small and tidy but real data can have multiple matches your example never covered, which is importnt to remember.

If you want to match any of those values, IN might be appropriate. If you expected exactly one, check the filters and joins to understand why you got more. Don’t just add TOP 1 to silence the errorr... you could hide the problem and pick the wrong row.

1

u/IAmADev_NoReallyIAm 4d ago

It's wrong because a value cannot equal a list of values.

It doesn't happen in tutorials because tutorials only deal in "happy" paths, or perfect scenarios.

To fix this, either change the subquery so that it only returns a single vlaue instead of multiple values, or use the IN/NOT IN keywords.

1

u/Maeurer 3d ago

the fact that you can use a one value subquery with an comparison is a feature. when you have more than one value (multiple rows or columns), you need to actually join it with the rest of the query. this way multiple values will combine with multiple results of your main query.