SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
In a relational database management system (DBMS), nested queries, often referred to as subqueries, are SQL queries that have another SQL query embedded within them. A subquery can return a single value, a single row, a single column, or a table.
Scalar Subquery:
Get the average salary from the employees
table.
SELECT AVG(salary) FROM employees;
Use the above subquery to find employees earning more than the average salary.
SELECT name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
Row Subquery:
Find employees with the same job_id
and department_id
as a specific employee (e.g., with employee_id = 100
).
SELECT name FROM employees WHERE (job_id, department_id) = (SELECT job_id, department_id FROM employees WHERE employee_id = 100);
Column Subquery: Get a list of departments that have employees.
SELECT DISTINCT department_id FROM employees WHERE department_id IN (SELECT department_id FROM departments);
Table Subquery: Find the employees who have the maximum salary in their respective departments.
SELECT e.name, e.salary, e.department_id FROM employees e WHERE (e.salary, e.department_id) IN (SELECT MAX(salary), department_id FROM employees GROUP BY department_id);
Subqueries can also be nested within other subqueries. For instance, to find the department name for employees who earn the maximum salary:
SELECT e.name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id WHERE e.salary IN (SELECT MAX(salary) FROM employees WHERE department_id IN (SELECT department_id FROM departments));
In summary, nested queries or subqueries are a powerful feature of SQL, allowing for complex operations and data retrieval. However, they should be used judiciously, keeping performance implications in mind.
Examples of nested queries in database management:
SELECT column1 FROM table1 WHERE column2 IN (SELECT column3 FROM table2 WHERE condition);
Nested subqueries in SQL explained:
SELECT column1 FROM table1 WHERE column2 = (SELECT column3 FROM table2 WHERE condition);
How to use nested queries in WHERE clause:
WHERE
clause for more complex conditions.SELECT column1 FROM table1 WHERE column2 = (SELECT MAX(column3) FROM table2);
Correlated subqueries in database management systems:
SELECT column1 FROM table1 t1 WHERE column2 = (SELECT MAX(column3) FROM table2 t2 WHERE t2.foreign_key = t1.primary_key);
Nested queries vs JOIN operations in SQL:
JOIN
operations can achieve similar results, choosing between them depends on the specific use case and performance considerations.-- Nested Query Example SELECT column1 FROM table1 WHERE column2 = (SELECT MAX(column3) FROM table2); -- JOIN Example SELECT table1.column1 FROM table1 JOIN table2 ON table1.column2 = table2.column3 WHERE table2.column3 = (SELECT MAX(column3) FROM table2);
Nested queries in SELECT and FROM clauses:
SELECT
and FROM
clauses, performing operations on the result set.-- Nested Query in SELECT SELECT column1, (SELECT MAX(column2) FROM table2) AS MaxValue FROM table1; -- Nested Query in FROM SELECT AVG(subquery_column) AS AverageValue FROM (SELECT column1 AS subquery_column FROM table1) AS subquery_table;
Common mistakes when using nested queries in SQL:
-- Incorrect Example (subquery returns multiple rows) SELECT column1 FROM table1 WHERE column2 = (SELECT column3 FROM table2 WHERE condition);