Skip to content
Advertisement

Error “more than one row returned by a subquery used as an expression”

I have the following 3 tables:

airport

area

booking

And I have this query

Running the query I get the error:

I don’t understand what the problem is.

I added an SQL Fiddle to play with: http://sqlfiddle.com/#!17/8a09f/1

Advertisement

Answer

A subquery in the SELECT list is only allowed to return a single value. Not multiple rows, nor multiple columns. If you want any of those, the standard replacement is a LATERAL subquery. See:

The more severe problem with your query is that it doesn’t make sense at all.

This might be what you want, returning the list of all commissions (if any) for a given airport and a given area:

fiddle

(But you get no results for com while asking for airport 2 and area 3, for which there are no matching entries.)

Your subquery was uncorrelated. Assuming you really meant to link to the given airport via airport_id.

LEFT JOIN area AS p made no sense in combination with the condition WHERE p.areaid = 3. That’s a hidden [INNER] JOIN. See:

The filter b.booking_date <= '2022-09-30T23:59:59' may be slightly incorrect, too (unless your data is guaranteed to have a 1-minute resolution). Either way, b.booking_date < '2022-10-01' is the proper way to include “all of September”. '2022-10-01' is a valid timestamp literal, short for '2022-10-01T00:00:00'.

Assuming the value in commission is really meant to be a percentage. (But I removed the rounding I first had. You didn’t ask for that.)

BTW, if you actually want the sum – a single value – a correlated (!) subquery becomes an option again:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement