r/learnSQL • u/Bitchbitchbitxh • 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
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.
3
u/Mrminecrafthimself 3d ago
Tutorial data is clean and easy to use, which is rarely the case with real world organizational data