DB-Lab 8
DB-Lab 8
Systems -Lab
LAB 8
Correlated Subquery
Lab Objective:
Outcome:
This lab will cover the usage of correlated subqueries in SQL, which are subqueries that rely
on the outer query for each row. Compared to independent subqueries, correlated subqueries
are reviewed for each row processed by the outer query.
Correlated Subquery:
A correlated subquery is a subquery that references columns from the outer query. This dependency
causes the subquery to be executed repeatedly, once for each row processed by the outer query.
Correlated subqueries are commonly used for queries that need to filter data based on row-by-row
comparisons.
Sample Table
dept_name VARCHAR(50));
CREATE TABLE employees ( emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
(2, 'Marketing'),
(3, 'IT');
1. Write a query to find employees whose salary is above the average salary of their respective
department.
2. This query will use a correlated subquery to calculate the average salary per department.
1. Write a correlated subquery to display Invoice Id, Billing city billing state and total
sales in descending order. Hint: One Sale= unitprice* quantity
2. Write a correlated subquery to display invoice information having latest date.
3. Write correlated subquery to display Customers(customer Id, full name, email address)
who placed orders greater than their average order amount.
4. Write query to list all albums with track longer than the average track length in database.