Database Programming With SQL 10-4: Correlated Subqueries Practice Activities
Database Programming With SQL 10-4: Correlated Subqueries Practice Activities
Database Programming With SQL 10-4: Correlated Subqueries Practice Activities
Try It / Solve It
2. Write a query that lists the highest earners for each department. Include the last_name,
department_id, and the salary for each employee.
SELECT e.last_name, e.department_id, e.salary
FROM employees e
WHERE e.salary = (SELECT MAX(ee.salary) FROM employees ee WHERE e.department_id = ee.department_id)
3. Examine the following select statement and finish it so that it will return the last_name,
department_id, and salary of employees who have at least one person reporting to them. So we
are effectively looking for managers only. In the partially written SELECT statement, the WHERE
clause will work as it is. It is simply testing for the existence of a row in the subquery.
SELECT outer.last_name, outer.department_id, outer.salary
FROM employees outer
SELECT (enter columns here) WHERE outer.employee_id IN (SELECT DISTINCT inner.manager_id
FROM (enter table name here) outer FROM employees inner
WHERE 'x' IN (SELECT 'x' WHERE inner.manager_id = outer.employee_id)
ORDER BY outer.department_id
FROM (enter table name here) inner
WHERE inner(enter column name here) = inner(enter column name here)
Finish off the statement by sorting the rows on the department_id column.
4. Using a WITH clause, write a SELECT statement to list the job_title of those jobs whose maximum
salary is more than half the maximum salary of the entire company. Name your subquery
MAX_CALC_SAL. Name the columns in the result JOB_TITLE and JOB_TOTAL, and sort the
result on JOB_TOTAL in descending order.
Hint: Examine the jobs table. You will need to join JOBS and EMPLOYEES to display the
job_title. WITH MAX_CALC_SAL AS
(SELECT j.job_id , j.job_title, MAX(e.salary) AS job_actual_max FROM employees e
RIGHT OUTER JOIN jobs j ON e.job_id = j.job_id GROUP BY j.job_id,j.job_title)