Unlocking the World of Business Intelligence with SQLBI

A subquery, also known as a nested query or inner query, is a query embedded within another query. The subquery provides a result that is used by the main (outer) query for further operations, such as filtering, aggregation, or comparison.
Subqueries can be used in various parts of a SQL query, including the WHERE
clause, FROM
clause, and SELECT
clause. They can return either a single value (scalar subquery), a single row, or multiple rows and columns.
A scalar subquery returns a single value. It can be used in a SELECT
clause or within a condition in the WHERE
clause.
Find the employee who has a salary higher than the average salary of all employees.
(SELECT AVG(salary) FROM employees)
calculates the average salary of all employees.A row subquery returns a single row with multiple columns. It is typically used in conditions like IN
, ANY
, or ALL
to compare multiple columns of the outer query.
Find all employees whose department matches that of a specific employee.
department_id
of the employee with employee_id = 101
.A table subquery returns multiple rows and columns. It is used in the FROM
clause or within IN
, EXISTS
, or JOIN
conditions.
Find the average salary by department for departments with more than 5 employees.
A correlated subquery is a subquery that references columns from the outer query. Unlike regular subqueries, which are independent, a correlated subquery is evaluated once for each row processed by the outer query.
Find all employees whose salary is greater than the average salary in their department.
department_id
to calculate the department's average salary.Subqueries can be placed in different parts of a SQL query depending on the situation:
In the WHERE
Clause:
In the FROM
Clause:
FROM
clause can be treated as a temporary table.In the SELECT
Clause:
SELECT
clause to return a value that is computed dynamically for each row.In the HAVING
Clause:
HAVING
clause to filter grouped data.EXISTS
and NOT EXISTS
Operators in SQLThe EXISTS
and NOT EXISTS
operators are used in SQL to test for the presence or absence of rows that meet the conditions specified by a subquery. These operators are used with subqueries in the WHERE
clause to filter the results of the outer query based on whether the inner subquery returns any rows.
These operators are particularly useful when you want to perform conditional queries that depend on the existence of records in another table, and they are optimized for such use cases.
EXISTS
OperatorThe EXISTS
operator checks whether a subquery returns any rows. It returns TRUE
if the subquery produces one or more rows, and FALSE
if no rows are returned.
EXISTS
is typically used in the WHERE
clause of a query to filter data based on the existence of related rows in another table.EXISTS
operator stops scanning once it finds the first matching row, making it more efficient than using IN
for large datasets.Consider two tables:
employees
: Contains employee details.departments
: Contains department details.We want to find all employees who work in a department that has at least one employee.
(SELECT 1 FROM departments d WHERE d.department_id = e.department_id)
checks whether there exists any department with the same department_id
as the employee's.1
in the subquery is just a placeholder value because EXISTS
only checks if the subquery returns any rows; it doesn't care about the actual data.If there are departments with employees, the query will return those employees.
NOT EXISTS
OperatorThe NOT EXISTS
operator is the opposite of EXISTS
. It checks whether the subquery returns no rows. It returns TRUE
if the subquery returns no rows, and FALSE
if the subquery returns one or more rows.
Suppose we want to find all departments that do not have any employees assigned to them.
(SELECT 1 FROM employees e WHERE e.department_id = d.department_id)
checks whether there exists any employee in the department.The query will return departments that have no employees.
Comments
Post a Comment