In this tutorial, we aim to explore the concept of correlated subqueries in SQL (Structured Query Language). Correlated subqueries are a powerful tool that allows us to filter data in our database based on a condition that relies on another part of the query.
By the end of this tutorial, you will be able to:
Before you start, you should have a basic understanding of:
- SQL syntax and commands
- Basic database concepts
- How to write simple SQL queries
A correlated subquery is a type of SQL subquery. Unlike a regular subquery (which runs once and returns a single value or a set of values), a correlated subquery runs once for each row processed by the outer query. Each subquery is executed sequentially and can use the results of the outer query.
Let's consider an example where we have a Students
table and a Courses
table. Each student can be enrolled in multiple courses. We want to find each student who is enrolled in the same courses as a particular student.
SELECT s1.StudentName
FROM Students s1
WHERE EXISTS (
SELECT 1
FROM Courses c1
WHERE c1.StudentID = s1.StudentID
AND c1.CourseID IN (
SELECT c2.CourseID
FROM Courses c2
WHERE c2.StudentID = 'student123'
)
);
Here, we'll find all employees whose salary is above the average salary in their department.
SELECT e1.EmployeeName
FROM Employees e1
WHERE e1.Salary > (
SELECT AVG(e2.Salary)
FROM Employees e2
WHERE e1.DepartmentID = e2.DepartmentID
);
In this example, the outer query scans the Employees
table row-by-row. For each row, the subquery calculates the average salary of the employees in the same department and checks if the employee's salary is greater than this average salary.
This query will find all customers who have made more purchases than the average customer.
SELECT c1.CustomerName
FROM Customers c1
WHERE (
SELECT COUNT(o1.OrderID)
FROM Orders o1
WHERE o1.CustomerID = c1.CustomerID
) > (
SELECT AVG(o2.OrderCount)
FROM (
SELECT COUNT(o3.OrderID) AS OrderCount
FROM Orders o3
GROUP BY o3.CustomerID
) o2
);
We have covered the concept of correlated subqueries, how to write them, and how to use them to process data row-by-row. Next, you might want to learn about joins, which are another way to combine data from multiple tables.
SELECT p1.ProductName
FROM Products p1
WHERE p1.Price > (
SELECT AVG(p2.Price)
FROM Products p2
WHERE p1.CategoryID = p2.CategoryID
);
SELECT e1.EmployeeName
FROM Employees e1
WHERE e1.YearsWithCompany < (
SELECT AVG(e2.YearsWithCompany)
FROM Employees e2
WHERE e1.DepartmentID = e2.DepartmentID
);
Remember, practice is key when learning SQL. Try to solve as many problems as you can using correlated subqueries to become more comfortable with the concept.