0% found this document useful (0 votes)
159 views

DP9 Practice Activities - Answers

This document contains practice problems and solutions for SQL concepts including GROUP BY, HAVING, ROLLUP, CUBE, GROUPING SETS, UNION, UNION ALL, INTERSECT, and MINUS. It includes 6 problems on using GROUP BY and HAVING clauses, 3 problems using ROLLUP, CUBE and GROUPING SETS, and 6 problems using set operators to combine data from multiple tables. The goals are to summarize data, identify duplicates using different techniques, and combine records from two different tables.

Uploaded by

gery sumual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

DP9 Practice Activities - Answers

This document contains practice problems and solutions for SQL concepts including GROUP BY, HAVING, ROLLUP, CUBE, GROUPING SETS, UNION, UNION ALL, INTERSECT, and MINUS. It includes 6 problems on using GROUP BY and HAVING clauses, 3 problems using ROLLUP, CUBE and GROUPING SETS, and 6 problems using set operators to combine data from multiple tables. The goals are to summarize data, identify duplicates using different techniques, and combine records from two different tables.

Uploaded by

gery sumual
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Practice Activity for DP 9.1 – 9.

3
Gery J. Sumual (01123574291-53)

9.1 Using GROUP BY and HAVING Clauses


Vocabulary: HAVING clause, GROUP BY
Try It / Solve It:
1. a. T
b. F
c. F
d. T
e. F
2. a. SELECT manager_id
FROM employees
GROUP BY manager_id
HAVING AVG(salary) <16000;
b. SELECT cd_number, COUNT(title)
FROM d_cds
WHERE cd_number < 93
GROUP BY cd_number;
c. SELECT ID, MAX(ID), artist AS Artist
FROM d_songs
WHERE duration IN('3 min', '6 min', '10 min')
GROUP by ID, artist
HAVING ID < 50
d. SELECT loc_type, rental_fee AS Fee
FROM d_venues
WHERE id < 100
GROUP BY loc_type, rental_fee
ORDER BY 2;
3. SELECT MAX(song_id)
FROM d_track_listings
WHERE track IN ( 1, 2, 3);
4. a. T
b. F
c. F
5. SELECT ROUND(MAX(AVG(salary)), 2) AS max_avg_salary_of_each_dmt,
ROUND(MIN(AVG(salary)),2) AS min_avg_salary_of_each_dmt
FROM employees
GROUP BY department_id;
6. SELECT AVG(MAX(salary))
FROM employees
GROUP BY department_id;

9.2 Using ROLLUP and CUBE Operations and GROUPING SETS


Vocabulary: ROLLUP, CUBE, GROUPING SETS
Try It / Solve It:
1. SELECT manager_id, job_id, SUM(salary), GROUPING(job_id) AS subtotal_of_job_id
FROM employees
GROUP BY ROLLUP (manager_id, job_id)
ORDER BY manager_id, job_id;
2. SELECT manager_id, job_id, SUM(salary), GROUPING(manager_id) AS subtotal_of_mgr_id,
GROUPING(job_id) AS subtotal_of_job_id
FROM employees
GROUP BY CUBE (manager_id, job_id)
ORDER BY manager_id, subtotal_of_mgr_id, job_id;
3. SELECT department_id, manager_id, job_id, SUM(salary)
FROM employees
GROUP BY GROUPING SETS ((department_id, manager_id, job_id), (manager_id, job_id),
(department_id, manager_id));

9.3 Set Operators


Vocabulary: UNION, TO_CHAR/DATE/NUMBER(NULL) – matching the select list, UNION ALL, set
operators, MINUS, INTERSECT.
Try It / Solve It:
1. UNION, UNION ALL, INTERSECT, MINUS.
2. SELECT employee_id, job_id, hire_date, department_id
FROM employees
UNION
SELECT employee_id, job_id, start_date, department_id
FROM job_history
ORDER BY employee_id;
3. SELECT employee_id, job_id, hire_date, department_id
FROM employees
UNION ALL
SELECT employee_id, job_id, start_date, department_id
FROM job_history
ORDER BY employee_id;
The duplicate is on the employee with id 176, easier to spot using INTERSECT operator.
4. SELECT employee_id, job_id, hire_date, department_id
FROM employees
MINUS
SELECT employee_id, job_id, start_date, department_id
FROM job_history
ORDER BY employee_id;
5. SELECT employee_id, job_id, start_date, department_id
FROM job_history
MINUS
SELECT employee_id, job_id, hire_date, department_id
FROM employees
ORDER BY employee_id;
6. SELECT employee_id, job_id, NVL(salary,0) AS salary
FROM employees
UNION
SELECT employee_id, job_id, 0
FROM job_history
ORDER BY employee_id, salary;

You might also like