Based on the SQLD exam syllabusReport an error
If a join attaches two tables side by side, a set operator combines two SELECT results vertically. It combines, subtracts, or picks the overlap, row by row. Today we compare using the jobs in department 10 and department 20.
| Department | Jobs (number of employees) |
|---|---|
| 10 | CLERK(1), MANAGER(1), PRESIDENT(1) — 3 rows |
| 20 | ANALYST(2), CLERK(2), MANAGER(1) — 5 rows |
| 30 | CLERK(1), MANAGER(1), SALESMAN(4) — 6 rows |
| Operator | Meaning | Duplicates | Jobs of dept 10 ○ dept 20 |
|---|---|---|---|
UNION | Union | Removed | ANALYST, CLERK, MANAGER, PRESIDENT |
UNION ALL | Union | Kept | 3 + 5 = 8 rows |
INTERSECT | Intersection | Removed | CLERK, MANAGER |
MINUS | Difference | Removed | 10 − 20 = PRESIDENT |
SELECT JOB FROM EMP WHERE DEPTNO = 10 UNION SELECT JOB FROM EMP WHERE DEPTNO = 20 ORDER BY JOB;JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENTSELECT JOB FROM EMP WHERE DEPTNO = 10 UNION ALL SELECT JOB FROM EMP WHERE DEPTNO = 20 ORDER BY JOB;JOB
---------
ANALYST
ANALYST
CLERK
CLERK
CLERK
MANAGER
MANAGER
PRESIDENTIn a WHERE =, NULL and NULL are not equal. Duplicate detection in set operators is different: it treats two NULLs as the same row.
SELECT COUNT(*) FROM (SELECT COMM FROM EMP UNION SELECT COMM FROM EMP); COUNT(*)
----------
5| Operator | Extra work |
|---|---|
UNION ALL | None. It just concatenates the results |
UNION, INTERSECT, MINUS | Sort or hash-compare the entire result to find duplicates |
UNION ALL is also used to attach a total row beneath the detail rows. The constant 'Total' goes into the ENAME position.
SELECT ENAME, SAL FROM EMP WHERE DEPTNO = 10 UNION ALL SELECT 'Total', SUM(SAL) FROM EMP WHERE DEPTNO = 10;ENAME SAL
---------- ----------
CLARK 2450
KING 5000
MILLER 1300
Total 8750SELECT JOB FROM EMP WHERE DEPTNO = 10 INTERSECT SELECT JOB FROM EMP WHERE DEPTNO = 20 ORDER BY JOB;JOB
---------
CLERK
MANAGERSELECT JOB FROM EMP WHERE DEPTNO = 10 MINUS SELECT JOB FROM EMP WHERE DEPTNO = 20 ORDER BY JOB;JOB
---------
PRESIDENTSELECT JOB FROM EMP WHERE DEPTNO = 20 MINUS SELECT JOB FROM EMP WHERE DEPTNO = 10 ORDER BY JOB;JOB
---------
ANALYSTUse MINUS to find "departments with no employees". It solves the same question as yesterday's NOT EXISTS using sets.
SELECT DEPTNO FROM DEPT MINUS SELECT DEPTNO FROM EMP; DEPTNO
----------
40| DBMS | Difference keyword |
|---|---|
| Oracle | MINUS |
| SQL Server | EXCEPT |
EXCEPT, which arrived in 21c, and gives the same result as MINUS. On the exam, treat the Oracle difference as MINUS.To combine results vertically, the columns at the same position must pair up.
SELECT ENAME, SAL FROM EMP UNION SELECT DNAME FROM DEPT;SELECT ENAME, SAL FROM EMP UNION SELECT DNAME FROM DEPT
*
ERROR at line 1:
ORA-01789: query block has incorrect number of result columnsSELECT ENAME FROM EMP UNION SELECT DEPTNO FROM DEPT;SELECT ENAME FROM EMP UNION SELECT DEPTNO FROM DEPT
*
ERROR at line 1:
ORA-01790: expression must have same datatype as corresponding expression| Error | Cause |
|---|---|
| ORA-01789 | The SELECTs above and below have different numbers of columns |
| ORA-01790 | Columns at the same position have different data type families (character ↔ number) |
NULL or a constant. Write it like SELECT DEPTNO, NULL FROM DEPT.SELECT EMPNO AS ID, ENAME AS NAME FROM EMP WHERE DEPTNO = 10 UNION SELECT DEPTNO, DNAME FROM DEPT ORDER BY ID; ID NAME
---------- --------------
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS
7782 CLARK
7839 KING
7934 MILLERORDER BY can also refer only to the first SELECT's names. Using the second SELECT's alias or column name is an error.
SELECT ENAME FROM EMP WHERE DEPTNO = 10 UNION SELECT DNAME FROM DEPT WHERE DEPTNO = 10 ORDER BY DNAME;SELECT ENAME FROM EMP WHERE DEPTNO = 10 UNION SELECT DNAME FROM DEPT WHERE DEPTNO = 10 ORDER BY DNAME
*
ERROR at line 1:
ORA-00904: "DNAME": invalid identifierSELECT ENAME FROM EMP WHERE DEPTNO = 10 UNION SELECT DNAME FROM DEPT WHERE DEPTNO = 10 ORDER BY ENAME;ENAME
--------------
ACCOUNTING
CLARK
KING
MILLERORDER BY 2 sorts by the second column.ORDER BY sorts the final result after the set operation is complete. Writing it after an intermediate SELECT is a syntax error.
SELECT JOB FROM EMP WHERE DEPTNO = 10 ORDER BY JOB UNION SELECT JOB FROM EMP WHERE DEPTNO = 20;SELECT JOB FROM EMP WHERE DEPTNO = 10 ORDER BY JOB UNION SELECT JOB FROM EMP WHERE DEPTNO = 20
*
ERROR at line 1:
ORA-03048: SQL reserved word 'UNION' is not syntactically valid following '...DEPTNO = 10 ORDER BY JOB '⚠️ Pitfall: In a set-operation statement, write ORDER BY only once, after the last SELECT. The sort key is a column name or alias of the first SELECT, or a position number. A name that exists only in the last SELECT cannot be used.
Oracle's set operators have the same precedence. They are evaluated from top to bottom in order. The jobs in department 30 are CLERK, MANAGER, and SALESMAN.
SELECT JOB FROM EMP WHERE DEPTNO = 10 UNION SELECT JOB FROM EMP WHERE DEPTNO = 20 MINUS SELECT JOB FROM EMP WHERE DEPTNO = 30 ORDER BY JOB;JOB
---------
ANALYST
PRESIDENTIf you use parentheses to make MINUS run first, the result changes.
SELECT JOB FROM EMP WHERE DEPTNO = 10 UNION (SELECT JOB FROM EMP WHERE DEPTNO = 20 MINUS SELECT JOB FROM EMP WHERE DEPTNO = 30) ORDER BY JOB;JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENTTomorrow we cover ROLLUP and CUBE, which add subtotal and grand-total rows to GROUP BY.
Click a choice to reveal the answer and explanation.
Question 1
Department 10 of EMP has 3 employees and department 20 has 5, and the jobs found in both departments are CLERK and MANAGER. What is the result of the following SQL? ```sql SELECT COUNT(*) FROM (SELECT JOB FROM EMP WHERE DEPTNO = 10 UNION ALL SELECT JOB FROM EMP WHERE DEPTNO = 20); ```
Question 2
Which is the correct result of running the following SQL in Oracle? ```sql SELECT ENAME FROM EMP WHERE DEPTNO = 10 UNION SELECT DNAME FROM DEPT WHERE DEPTNO = 10 ORDER BY DNAME; ```
Question 3
DEPT has departments 10, 20, 30, and 40, and EMP has employees only in departments 10, 20, and 30. You want only department number 40, the department with no employees. Which operator goes in the blank? ```sql SELECT DEPTNO FROM DEPT ( blank ) SELECT DEPTNO FROM EMP; ```
Question 4
Which statement about set operators is NOT correct?
Question 5
The jobs in department 10 are CLERK, MANAGER, and PRESIDENT; in department 20, ANALYST, CLERK, and MANAGER; and in department 30, CLERK, MANAGER, and SALESMAN. What is the result of the following SQL? ```sql SELECT JOB FROM EMP WHERE DEPTNO = 10 UNION SELECT JOB FROM EMP WHERE DEPTNO = 20 MINUS SELECT JOB FROM EMP WHERE DEPTNO = 30 ORDER BY JOB; ```