Reputation: 105
I have run into a problem with Oracle SQL the Error is:
ORA-01427 single-row subquery returns more that one row
I am trying to find the locations that have at least one vehicle with manual
transmission that has lower mileage than any luxury vehicle at that location.
Here is My Table
VID MILEAGE LOCATION VSIZE TRANSMISSION
----------------------------------------------------------------------
V-101 70 AHMD COMPACT AUTOMATIC
V-102 50 SURAT COMPACT AUTOMATIC
V-103 10 AHMD MID-SIZE MANUAL
V-104 30 AHMD MID-SIZE AUTOMATIC
V-105 15 VADODARA FULL-SIZE AUTOMATIC
V-106 20 AHMD LUXURY AUTOMATIC
V-107 50 AHMD LUXURY MANUAL
And this is my Query:
SELECT location, transmission, mileage FROM vehicles_workshop
WHERE transmission = 'MANUAL' AND mileage =
( SELECT DISTINCT mileage FROM vehicles_workshop WHERE mileage <
(SELECT MAX(mileage) FROM vehicles_workshop WHERE vsize = 'LUXURY')
);
Upvotes: 1
Views: 244
Reputation: 60472
SELECT location, transmission, mileage
FROM vehicles_workshop t1
WHERE transmission = 'MANUAL' --manual transmission
AND EXISTS
( SELECT *
FROM vehicles_workshop t2
WHERE vsize = 'LUXURY' --luxury vehicle
AND t2.location = t1.location --same location
AND t2.mileage > t1.mileage) --higher mileage
;
Upvotes: 0
Reputation: 28751
Instead of equal to operator use IN
before subquery
SELECT location, transmission, mileage FROM vehicles_workshop
WHERE transmission = 'MANUAL' AND mileage IN
( SELECT DISTINCT mileage FROM vehicles_workshop WHERE mileage <
(SELECT MAX(mileage) FROM vehicles_workshop WHERE vsize = 'LUXURY')
);
Your subquery for comparing mileage is returning more than one value but equal to operator can work to compare only single values and so error is thrown.
Upvotes: 0