Search code examples
sqloracle-databasesubqueryora-01427

ORA-01427 single-row subquery returns more that one row


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')
      );

Solution

  • 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.