A Subquery is a query nested inside another query. It's used to retrieve data that will be used in the main query. Subqueries can be placed in SELECT, INSERT, UPDATE, or DELETE statements and can help in filtering or creating dynamic queries.
π Pro Tip: Subqueries can be used when the result of one query depends on another query's result. It's like nesting queries inside queries for better control.
There are different types of subqueries based on where and how they are used:
A Single-row subquery is used when a subquery returns only one value. Itβs typically used with comparison operators like =, >, <, etc.
Example: Find the employee with the highest salary:
SELECT employee_name FROM employees WHERE salary = (SELECT MAX(salary) FROM employees);
This query retrieves the name of the employee who has the highest salary from the employees
table.
A Multi-row subquery returns more than one value. It is typically used with operators like IN, NOT IN, ANY, or ALL.
Example: Find all employees who have a salary greater than any employee in the "Sales" department:
SELECT employee_name FROM employees WHERE salary > ANY (SELECT salary FROM employees WHERE department = 'Sales');
Here, the subquery returns the salaries of employees in the "Sales" department. The outer query then checks for employees with a salary greater than any of those values.
A Correlated subquery is one where the subquery refers to a column in the outer query. Itβs evaluated once for each row processed by the outer query.
Example: Find employees who earn more than the average salary in their department:
SELECT employee_name FROM employees e WHERE salary > (SELECT AVG(salary) FROM employees WHERE department = e.department);
In this example, the subquery is correlated with the outer query because it refers to the department
column of the outer query's current row. It calculates the average salary for each department and compares it with the employee's salary.
A subquery can also be used in the SELECT clause to return calculated results for each row.
Example: Get each employee's department and the highest salary in that department:
SELECT employee_name, department, (SELECT MAX(salary) FROM employees e2 WHERE e2.department = e1.department) AS highest_salary FROM employees e1;
This query will return the employee names, their departments, and the highest salary in each department.
You can use the EXISTS operator in a subquery to check whether a subquery returns any rows.
Example: Find employees who have made any sale:
SELECT employee_name FROM employees e WHERE EXISTS (SELECT 1 FROM sales s WHERE s.employee_id = e.employee_id);
In this case, the outer query retrieves employees who have made at least one sale, based on whether any rows are returned by the subquery.
π Interactive Task: Try creating your own subqueries using the examples above. Explore how they work in different SQL operations.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!