Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL : ERROR: more than one row returned by a subquery used as an expression

Tags:

sql

The thing is that it does return one row.

Here's the thing.

SELECT...

FROM...

WHERE...

GROUP BY...

HAVING randomNumber > (SELECT value FROM.....)

Whenever I have signs such as =, > it always returns me this error. When I do IN it doesn't.

Are you not supposed to use comparison signs when comparing to another table?

like image 665
AnEventHorizon Avatar asked Oct 03 '10 05:10

AnEventHorizon


People also ask

What would happen if more than one rows are returned from subquery?

When the subquery returns one or more rows of values, the subquery is only evaluated once and then the row(s) of values is returned to outer query to use.

How do you fix subquery returned more than 1 value this is not permitted when the subquery follows != <= >= Or when the subquery is used as an expression?

Answer: This error message appears when you try to use subquery (correlated or not) that returns more than one value to the calling query. This usually indicates that there are duplicate entries in the column of a table where it's expected to be unique.

Can a subquery return more than 1 value?

This is not permitted when the subquery follows =, != , <, <= , >, >= or when the subquery is used as an expression. 'Document' (RDOC)

Which subquery return single value?

A scalar subquery is a subquery that returns a single value. This is the simplest form of a subquery, and can be used in most places a literal or single column value is valid.


1 Answers

When you type:

SomeValue IN (SELECT ...)

it is equivalent to using:

SomeValue = ANY (SELECT ...)

Don't use the second notation - but it illustrates a point. When the SELECT returns more than one value, you must use ANY or ALL with the comparator. When you omit ANY or ALL, then you must have a SELECT that returns exactly one value.

like image 178
Jonathan Leffler Avatar answered Sep 19 '22 09:09

Jonathan Leffler