Tuesday, May 22, 2012

ORA-01427: single-row subquery returns more than one row

Everyday developers, testers and newbie’s face ORA-01427 error and I have seen many users looking for a solution for this error.

First let’s understand why users receive this error.

SQL> select count(*) from employee where employee_id = (select employee_id from salary where gross_sal < 5000 );
ERROR at line 1:
ORA-01427: single-row subquery returns more than one row

This error is raised when a sub-query returns more than one row.

Make sure you use multiple row sub query operator instead of single row sub-query operator to resolve the issue.

SQL> select count(*) from employee where employee_id in (select employee_id from salary where gross_sal < 5000 );

Now this query works fine.

Single row sub query operators are = (equal) , < (less than) , <=( less than or equal) , >(greater than) , >= (greater than or equal), <>(not equal ) ,!=(not equal)

Multiple row sub query operators are IN (equal to any member in the list), ANY (compare value to each value returned by the subquery), ALL (compare value to every value returned by the subquery)

Few guide lines for using Sub queries:
1. Use single-row operators with sing row sub queries
2. Use multiple-row operators with multiple row sub queries
3. Make sure you enclose sub queries in parentheses
4. Always keep sub queries on the right side of the comparison operator
5. You can use inline view to avoid ORA-01427 error, please refer http://docs.oracle.com/cd/E11882_01/server.112/e25554/qradv.htm#DWHSG08032

Regards
Satishbabu Gunukula
http://www.oracleracexpert.com/

2 comments:

  1. I'm trying to to an update query that takes the timestamp from one column and updates another in the same table and I'm getting the single row subquery error.
    What am I doing wrong?

    UPDATE TRACKING_DATA_T T
    SET T.TD_TIMESTAMP_UTC = (
    SELECT T.TD_RUN_START_DTM +( I."TZ_OFFSET" / 24)
    FROM TRACKING_DATA_T T , IDS_LIST I
    WHERE
    T.TD_TRACKING_DATA_SITE = I.ZIP
    and T.td_last_updt_dtm Between To_Date ('2014/05/14 16:00:00','yyyy/mm/dd hh24:mi:ss')AND TO_DATE ('2014/05/14 16:15:00','yyyy/mm/dd hh24:mi:ss'));

    ReplyDelete