A multi-row subquery is a subquery that returns more than one row of data. These types of subqueries are often used in the WHERE clause when you want to filter data based on a set of values rather than just a single value. Multi-row subqueries work well with operators like IN, ANY, and ALL to compare the outer query's data with multiple results from the inner query.
A multi-row subquery returns more than one row of results. Unlike a single-row subquery, which only returns one value, a multi-row subquery can return a list of values that the outer query can use in comparisons. These subqueries are typically used with comparison operators like IN, ANY, and ALL, which allow multiple values to be compared against the outer query's data.
The syntax for a multi-row subquery generally looks like this:
SELECT column1, column2, ... FROM table1 WHERE column1 IN (SELECT column1 FROM table2 WHERE condition);
Suppose you have two tables: Employees and Departments. You want to find employees who work in certain departments. In this case, you can use a multi-row subquery with the IN operator.
SELECT EmployeeName FROM Employees WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE DepartmentName IN ('Sales', 'Marketing'));
This query works as follows:
The ANY operator can be used to compare a value with any of the values returned by a multi-row subquery. For example, suppose you want to find products with a price greater than any of the prices in a list of products from a specific category.
SELECT ProductName, Price FROM Products WHERE Price > ANY (SELECT Price FROM Products WHERE Category = 'Electronics');
In this example:
The ALL operator can be used to compare a value with all of the values returned by a multi-row subquery. For instance, you might want to find employees whose salary is higher than all the salaries in a specific department.
SELECT EmployeeName, Salary FROM Employees WHERE Salary > ALL (SELECT Salary FROM Employees WHERE DepartmentID = 3);
This query works as follows:
Multi-row subqueries can be used in various parts of SQL queries, including the WHERE, HAVING, and FROM clauses. Here are a few examples:
SELECT DepartmentID, COUNT(*) AS EmployeeCount FROM Employees GROUP BY DepartmentID HAVING COUNT(*) > ALL (SELECT COUNT(*) FROM Employees WHERE DepartmentID = DepartmentID GROUP BY DepartmentID);
In this example, the subquery calculates the number of employees for each department, and the outer query filters departments with more employees than the department with the maximum number of employees.
SELECT DepartmentID, COUNT(*) AS EmployeeCount FROM (SELECT DepartmentID FROM Employees WHERE Salary > 50000) AS HighPaidEmployees GROUP BY DepartmentID;
In this query, the subquery selects employees with a salary greater than 50,000, and the outer query counts the number of employees in each department who meet this condition.
Multi-row subqueries allow you to perform complex queries by comparing a single value in the outer query with multiple values returned by the subquery. They are a powerful tool for filtering and aggregating data across different tables or conditions. Understanding when and how to use IN, ANY, and ALL with multi-row subqueries is crucial for writing efficient SQL queries.