Home > Enterprise >  How can we handle multiple rows returned from a subquery inside a case statement?
How can we handle multiple rows returned from a subquery inside a case statement?

Time:02-16

Write a SELECT query if department id provided at run time is 100 then it will return all the employee details those belong to department 100 else i wanted to print all the employees from employee table.

i have written below query :-

SELECT * FROM EMPLOYEES
WHERE department_id IN (
                        CASE &InputDept
                        WHEN 100 THEN 100
                        ELSE (SELECT DISTINCT department_id FROM DEPARTMENTS) END ) ;

This works fine when input is 100 but returned "Single-row subquery returns more than one row".

i understand that case is a statement which works like "if-then-else" with single value, also i tried with decode but no luck.

This can be easily done in PL/SQL , but is this possible with SELECT query?

CodePudding user response:

Skip the case expression, use regular AND/OR instead:

SELECT * FROM EMPLOYEES
WHERE (&InputDept = 100 and department_id = 100)
   OR (&InputDept <> 100 and department_id IN
                        (SELECT department_id FROM DEPARTMENTS)) ;

CodePudding user response:

It's better to use a query like this:

SELECT * FROM EMPLOYEES
WHERE department_id = CASE &InputDept WHEN 100 THEN 100 END
UNION ALL
SELECT * FROM EMPLOYEES
WHERE (&InputDept != 100 or &InputDept is NULL)
AND department_id IN (SELECT DISTINCT department_id FROM DEPARTMENTS);

Do not forget about employees without department_id, ie NULLs.

This query will be optimized by optimizer at runtime to do not execute unneeded part of union all

CodePudding user response:

Yet another alternative (example based on Scott's sample schema, ran in SQL*Plus):

SQL> select *
  2  from emp
  3  where deptno in (select deptno from dept
  4                   where &&inputdept <> 10
  5                   union all
  6                   select &&inputdept from dual
  7                   where &&inputdept = 10
  8                  );
Enter value for inputdept: 10

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7782 CLARK      MANAGER         7839 09.06.81       2450                    10
      7839 KING       PRESIDENT            17.11.81       5000                    10
      7934 MILLER     CLERK           7782 23.01.82       1300                    10

SQL> undefine inputdept
SQL> /
Enter value for inputdept: 25

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17.12.80        800                    20
      7499 ALLEN      SALESMAN        7698 20.02.81       1600        300         30
      7521 WARD       SALESMAN        7698 22.02.81       1250        500         30
      7566 JONES      MANAGER         7839 02.04.81       2975                    20
      7654 MARTIN     SALESMAN        7698 28.09.81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01.05.81       2850                    30
      7782 CLARK      MANAGER         7839 09.06.81       2450                    10
      7788 SCOTT      ANALYST         7566 09.12.82       3000                    20
      7839 KING       PRESIDENT            17.11.81       5000                    10
      7844 TURNER     SALESMAN        7698 08.09.81       1500          0         30
      7876 ADAMS      CLERK           7788 12.01.83       1100                    20
      7900 JAMES      CLERK           7698 03.12.81        950                    30
      7902 FORD       ANALYST         7566 03.12.81       3000                    20
      7934 MILLER     CLERK           7782 23.01.82       1300                    10

14 rows selected.

SQL>
  • Related