0% found this document useful (0 votes)
242 views3 pages

DP10 Practice Activities - Answers

1. This document contains practice problems and solutions for fundamentals of subqueries including single-row, multiple-row, correlated, and non-correlated subqueries. 2. Examples of different types of subqueries are provided such as finding records where a value is greater than the maximum value of a subquery, retrieving records matching values from a multiple-row subquery, and correlated subqueries where the outer query drives the execution of the inner query. 3. Solutions to each practice problem are included to demonstrate proper use of subqueries.

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)
242 views3 pages

DP10 Practice Activities - Answers

1. This document contains practice problems and solutions for fundamentals of subqueries including single-row, multiple-row, correlated, and non-correlated subqueries. 2. Examples of different types of subqueries are provided such as finding records where a value is greater than the maximum value of a subquery, retrieving records matching values from a multiple-row subquery, and correlated subqueries where the outer query drives the execution of the inner query. 3. Solutions to each practice problem are included to demonstrate proper use of subqueries.

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/ 3

Practice Activity for DP 10.1 – 10.

3
Gery J. Sumual (01123574291-53)

10.1 Fundamentals of Subqueries


Vocabulary: outer query, multiple-rows subquery, subquery, pairwise multiple-columns subquery
, single-row query, non-pairwise multiple-columns subquery, inner query.
Try It / Solve It:
1. To retrieve the information needed to execute the superquery
2. A query inside another query, used to get the necessary information before the other query
can be done.
3. SELECT song_id, event_id
FROM d_play_list_items
WHERE event_id = (SELECT event_id FROM d_play_list_items WHERE song_id = 45);
4. SELECT*
FROM d_events
WHERE cost > (SELECT cost FROM d_events WHERE id = 100);
5. SELECT track
FROM d_track_listings
WHERE cd_number = (SELECT cd_number FROM d_cds WHERE LOWER(title) = 'party music
for all occasions');
Or using a multiple-row subquery:
SELECT track
FROM d_track_listings
WHERE song_id IN (SELECT song_id FROM d_track_listings WHERE cd_number = (SELECT
cd_number FROM d_cds WHERE LOWER(title) = 'party music for all occasions'));
6. SELECT*
FROM d_events
WHERE theme_code = (SELECT code FROM d_themes WHERE description = 'Tropical');
7. SELECT first_name||' '||last_name AS name, salary
FROM f_staffs
WHERE salary > (SELECT salary FROM f_staffs WHERE id = 12);
8. SELECT first_name||' '||last_name AS name, staff_type
FROM f_staffs
WHERE staff_type <> (SELECT staff_type FROM f_staffs WHERE
CONCAT(first_name,last_name) = 'BobMiller');
9. SELECT*
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE
department_name = 'IT');
10. SELECT department_name
FROM departments
WHERE location_id = (SELECT location_id FROM locations WHERE city = 'Seattle');
11. a. T
b. T
c. F
10.2 Single-Row Subqueries
Try It / Solve It:
1. SELECT*
FROM employees
WHERE salary > (SELECT salary FROM employees WHERE 'Lorentz' IN (first_name,
last_name)) AND department_id = (SELECT department_id FROM employees WHERE 'Abel'
IN (first_name, last_name));
2. SELECT*
FROM employees
WHERE job_id = (SELECT job_id FROM employees WHERE 'Rajs' IN (first_name, last_name))
AND hire_date > (SELECT hire_date FROM employees WHERE 'Davies' IN (first_name,
last_name));
3. SELECT*
FROM d_events
WHERE theme_code = (SELECT theme_code FROM d_events WHERE id = 100);
4. SELECT staff_type, MAX(salary)
FROM f_staffs
GROUP BY staff_type
HAVING MAX(salary) < (SELECT MIN(salary) FROM f_staffs WHERE LOWER(staff_type) =
'cook');
5. SELECT department_id, TO_CHAR(ROUND(AVG(salary), 2), 'L99999.99') AS average_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > (SELECT salary FROM employees WHERE last_name = 'Ernst');
6. SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id <> 50);
10.3 Multiple-Row Subqueries
Try It / Solve It:
1. If it is a single row subquery, then the query will unavoidably return a null too. Whereas in
the case of multiple-row subquery, it depends on the value of the rows returned and the
comparison operator in use: When one of the rows returned a null, it is inevitable for the
query to return the same if the comparison operator or its equivalent contains ALL, for
example >ALL, <=ALL, NOT IN (equivalent of =!ALL), etc. For the case of ANY and IN it will
only compare with any values other than NULL and return them if it’s a match.
2. SELECT*
FROM d_songs
WHERE type_code IN (SELECT code FROM d_types WHERE LOWER(description) IN ('jazz',
'pop'));
3. SELECT last_name
FROM employees
WHERE salary IN (SELECT MIN(salary) FROM employees GROUP BY department_id);
4. SELECT*
FROM f_staffs
WHERE salary IN (SELECT MIN(salary) FROM f_staffs);
5. a. <ALL
b. <ALL
c. IN or =ANY
d. >ALL
6. a. F
b. F
c. F
d. T
e. F
7. SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) < (SELECT MIN(salary) FROM employees WHERE department_id = 50);
8. a. F
b. T
c. F
d. T
9. SELECT last_name, first_name, department_id, manager_id
FROM employees
WHERE (department_id, manager_id) = (SELECT department_id, manager_id FROM
employees WHERE employee_id = 141) AND employee_id <> 141;
10. SELECT last_name, first_name, department_id, manager_id
FROM employees
WHERE (department_id) = (SELECT department_id FROM employees WHERE employee_id =
141) AND (manager_id) = (SELECT manager_id FROM employees WHERE employee_id =
141) AND employee_id <> 141;
10.4 Correlated Subqueries
Try It / Solve It:
1. When a correlated subqueries got detected the outer query executed first, and for every
time it returned a row, the row passed into the subquery to be executed and its result to be
used to qualify or disqualify that row, in other words, the outer query drives the execution
of the subquery and use its result for qualification. The difference with non-correlated
subqueries is the separation of outer and inner query execution, which in this case, the inner
query got executed first without being dependent to the outer query that get executed after
while using the result it got from the inner query.
2. SELECT last_name, department_id, salary
FROM employees epe
WHERE salary = (SELECT MAX(salary) FROM employees WHERE department_id =
epe.department_id);
3. SELECT last_name, department_id, salary
FROM employees outer
WHERE 'x' IN (SELECT 'x'
FROM employees inner
WHERE inner.manager_id = outer.employee_id)
ORDER BY department_id;
4. WITH max_calc_sal AS (SELECT MAX(salary) FROM employees)
SELECT job_title, MAX(salary) AS job_total
FROM employees JOIN jobs USING (job_id)
GROUP BY job_title
HAVING MAX(salary) > (SELECT* FROM max_calc_sal)/2
ORDER BY job_title DESC;

You might also like