Based on the SQLD exam syllabusReport an error
GROUP BY ROLLUP(A, B) adds subtotals per A and a grand total row to the (A, B) groups. With n arguments, there are n + 1 grouping sets.ROLLUP(A, B) and ROLLUP(B, A) have different subtotal bases, so their results differ.CUBE(A, B) produces subtotals for every possible combination. With n arguments there are 2ⁿ grouping sets, and the argument order does not affect the set of result rows.Yesterday we attached a total row beneath the detail rows by hand with UNION ALL. If you need both per-department subtotals and an overall grand total, you have to write several SELECTs and join them. ROLLUP finishes that job in one GROUP BY line.
SELECT DEPTNO, SUM(SAL) FROM EMP GROUP BY ROLLUP(DEPTNO) ORDER BY DEPTNO; DEPTNO SUM(SAL)
---------- ----------
10 8750
20 10875
30 9400
29025GROUP BY DEPTNO.With two columns, one more level of subtotal appears. We attach department subtotals and a grand total to the totals by department and job.
SELECT DEPTNO, JOB, COUNT(*) AS CNT, SUM(SAL) AS SUM_SAL FROM EMP GROUP BY ROLLUP(DEPTNO, JOB) ORDER BY DEPTNO, JOB; DEPTNO JOB CNT SUM_SAL
---------- --------- ---------- ----------
10 CLERK 1 1300
10 MANAGER 1 2450
10 PRESIDENT 1 5000
10 3 8750
20 ANALYST 2 6000
20 CLERK 2 1900
20 MANAGER 1 2975
20 5 10875
30 CLERK 1 950
30 MANAGER 1 2850
30 SALESMAN 4 5600
30 6 9400
14 29025ROLLUP(DEPTNO, JOB) creates three grouping sets.
| Grouping set | Meaning | Rows | Columns that are NULL in the result |
|---|---|---|---|
| (DEPTNO, JOB) | By department and job | 9 | None |
| (DEPTNO) | Department subtotal | 3 | JOB |
| () | Grand total | 1 | DEPTNO, JOB |
JOB alone, is not created. ROLLUP treats the left column as the upper level of the hierarchy and produces only subtotals for that hierarchy.| Expression | Grouping sets created | Number of sets |
|---|---|---|
ROLLUP(A) | (A), () | 2 |
ROLLUP(A, B) | (A, B), (A), () | 3 |
ROLLUP(A, B, C) | (A, B, C), (A, B), (A), () | 4 |
ROLLUP with n arguments | n + 1 sets, dropping one at a time from the right | n + 1 |
In ROLLUP(JOB, DEPTNO), job is the upper level. The subtotal basis changes from department to job.
SELECT JOB, DEPTNO, SUM(SAL) AS SUM_SAL FROM EMP GROUP BY ROLLUP(JOB, DEPTNO) ORDER BY JOB, DEPTNO;JOB DEPTNO SUM_SAL
--------- ---------- ----------
ANALYST 20 6000
ANALYST 6000
CLERK 10 1300
CLERK 20 1900
CLERK 30 950
CLERK 4150
MANAGER 10 2450
MANAGER 20 2975
MANAGER 30 2850
MANAGER 8275
PRESIDENT 10 5000
PRESIDENT 5000
SALESMAN 30 5600
SALESMAN 5600
29025| Expression | Grouping sets | Rows |
|---|---|---|
ROLLUP(DEPTNO, JOB) | (DEPTNO, JOB) 9 + (DEPTNO) 3 + () 1 | 13 |
ROLLUP(JOB, DEPTNO) | (JOB, DEPTNO) 9 + (JOB) 5 + () 1 | 15 |
⚠️ Pitfall: A plain
GROUP BY DEPTNO, JOBgives the same groups even if you swap the column order (W4 Day 1).ROLLUP(DEPTNO, JOB)andROLLUP(JOB, DEPTNO)have different subtotal sets, so their results differ.
CUBE produces subtotals for every combination you can make from its arguments. Department subtotals and job subtotals come out together.
SELECT DEPTNO, JOB, SUM(SAL) AS SUM_SAL FROM EMP GROUP BY CUBE(DEPTNO, JOB) ORDER BY DEPTNO, JOB; DEPTNO JOB SUM_SAL
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
30 9400
ANALYST 6000
CLERK 4150
MANAGER 8275
PRESIDENT 5000
SALESMAN 5600
29025| Grouping set | Meaning | Rows |
|---|---|---|
| (DEPTNO, JOB) | By department and job | 9 |
| (DEPTNO) | Department subtotal | 3 |
| (JOB) | Job subtotal | 5 |
| () | Grand total | 1 |
CUBE(JOB, DEPTNO), the sets created are the same, so the set of rows is the same. Only the on-screen order depends on ORDER BY.| Expression | Number of sets | Example (3 arguments) |
|---|---|---|
ROLLUP | n + 1 | 4 |
CUBE | 2ⁿ | 8 |
CUBE(DEPTNO) and ROLLUP(DEPTNO) both create (DEPTNO) and ().A column placed outside ROLLUP is always included in every grouping set. GROUP BY DEPTNO, ROLLUP(JOB) has two sets, (DEPTNO, JOB) and (DEPTNO), and has no grand total.
SELECT DEPTNO, JOB, SUM(SAL) FROM EMP GROUP BY DEPTNO, ROLLUP(JOB) ORDER BY DEPTNO, JOB; DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
30 9400ROLLUP(DEPTNO, JOB) minus only the final grand total, so 12 rows.If you wrap columns in parentheses inside ROLLUP, those columns are treated as a single unit. They are not split apart in the middle.
SELECT DEPTNO, JOB, SUM(SAL) FROM EMP GROUP BY ROLLUP((DEPTNO, JOB)) ORDER BY DEPTNO, JOB; DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
29025| Expression | Grouping sets | Rows |
|---|---|---|
GROUP BY DEPTNO, JOB | (DEPTNO, JOB) | 9 |
GROUP BY DEPTNO, ROLLUP(JOB) | (DEPTNO, JOB), (DEPTNO) | 12 |
GROUP BY ROLLUP((DEPTNO, JOB)) | (DEPTNO, JOB), () | 10 |
GROUP BY ROLLUP(DEPTNO, JOB) | (DEPTNO, JOB), (DEPTNO), () | 13 |
GROUP BY CUBE(DEPTNO, JOB) | The four above + (JOB) | 18 |
WHERE filters rows before groups are made. ROLLUP's subtotals and grand total are also computed only from the remaining rows.
SELECT DEPTNO, JOB, SUM(SAL) FROM EMP WHERE DEPTNO = 10 GROUP BY ROLLUP(DEPTNO, JOB) ORDER BY DEPTNO, JOB; DEPTNO JOB SUM(SAL)
---------- --------- ----------
10 CLERK 1300
10 MANAGER 2450
10 PRESIDENT 5000
10 8750
8750Every ROLLUP and CUBE example in this note has ORDER BY attached. Without ORDER BY, in the practice container the detail rows came out mixed up, followed by the subtotals and grand total. This order is a result that can change with the execution plan, and it is not an order that Oracle guarantees.
ORDER BY DEPTNO, JOB. In ascending order NULL goes last, so the subtotal comes below the detail rows.Next time we cover GROUPING SETS, which pick only the grouping sets you want, and the GROUPING function, which distinguishes subtotal rows.
ROLLUP(DEPTNO, JOB).().(DEPTNO, JOB), (DEPTNO), and ().GROUP BY A, ROLLUP(B) where only some columns go into ROLLUP, producing subtotals without a grand total.ROLLUP((A, B)).Click a choice to reveal the answer and explanation.
Question 1
How many grouping sets does GROUP BY ROLLUP(A, B, C) create?
Question 2
In EMP there are 9 (department, job) combinations, 3 departments, and 5 jobs. How many rows does the following SQL return? ```sql SELECT DEPTNO, JOB, SUM(SAL) FROM EMP GROUP BY ROLLUP(JOB, DEPTNO); ```
Question 3
Using the same EMP, how many rows does the following SQL return? ```sql SELECT DEPTNO, JOB, SUM(SAL) FROM EMP GROUP BY CUBE(DEPTNO, JOB); ```
Question 4
In the result of SELECT DEPTNO, JOB, SUM(SAL) FROM EMP GROUP BY ROLLUP(DEPTNO, JOB);, what does the row where DEPTNO is 10 and JOB is NULL mean? (JOB in EMP has no NULLs.)
Question 5
In EMP there are 9 (department, job) combinations and 3 departments. How many rows does the following SQL return? ```sql SELECT DEPTNO, JOB, SUM(SAL) FROM EMP GROUP BY DEPTNO, ROLLUP(JOB); ```