SQL Subqueries: Nested and Correlated Subqueries

Subqueries-in-SQL

SQL (Structured Query Language) is a powerful tool that allows us to interact with databases, manipulate data, and retrieve information.

One of the many powerful features of SQL is the ability to use subqueries. In this article, we will explore two types of subqueries: nested and correlated subqueries.

We will dive into each type, discuss their usage, and provide relevant examples and scenarios.

Table of Contents

Nested Subqueries

Definition and Usage:

A nested subquery, also known as an inner query or subselect, is a query embedded within another query. Nested subqueries are a powerful way to combine multiple SQL operations in a single query.

They can be used in SELECT, WHERE, and HAVING clauses and can be found in various parts of an SQL statement.

Examples and Scenarios:

a. Example 1: Using a Nested Subquery in a SELECT Clause

Imagine we have two tables: “employees” and “salaries”. We want to find the average salary of all employees and display it alongside each employee’s name.

SELECT e.name, (SELECT AVG(s.salary) FROM salaries s) as avg_salary
FROM employees e;

Here, the nested subquery calculates the average salary of all employees, which is then displayed next to each employee’s name.

b. Example 2: Using a Nested Subquery in a WHERE Clause

Suppose we want to find all employees whose salary is greater than the average salary. We can use a nested subquery within a WHERE clause:

SELECT e.name, e.salary
FROM employees e
WHERE e.salary > (SELECT AVG(s.salary) FROM salaries s);

Correlated Subqueries ๐Ÿ“Œ

Definition and Usage:

A correlated subquery is a type of subquery that depends on the outer query for its execution. Unlike nested subqueries, correlated subqueries cannot be executed independently as they reference columns from the outer query.

They are often used to filter results based on the relationship between the outer and inner queries.

Examples and Scenarios:

a. Example 1: Using a Correlated Subquery to Find Employees with Salaries Above Their Department’s Average

Suppose we have two tables: “employees” and “departments”. We want to find employees whose salary is higher than the average salary within their department.

SELECT e.name, e.salary, d.name as department_name
FROM employees e, departments d
WHERE e.department_id = d.id
AND e.salary > (
    SELECT AVG(s.salary)
    FROM employees s
    WHERE s.department_id = e.department_id
);

In this example, the correlated subquery calculates the average salary of each department and compares it to the salary of each employee.

b. Example 2: Using a Correlated Subquery to Rank Products by Sales

Imagine we have a table called “sales” that contains information about products and their total sales. We want to rank the products by their sales.

SELECT s1.product_name, s1.total_sales, (
    SELECT COUNT(*) + 1
    FROM sales s2
    WHERE s2.total_sales > s1.total_sales
) as rank
FROM sales s1
ORDER BY s1.total_sales DESC;

In this scenario, the correlated subquery counts the number of products with higher sales and assigns a rank accordingly.

Conclusion

Nested and correlated subqueries are powerful SQL tools that enable complex operations within a single query.

Understanding when and how to use them effectively can greatly enhance your SQL skills and help you tackle more advanced data manipulation tasks.


Thank you for reading our blog, we hope you found the information provided helpful and informative. We invite you to follow and share this blog with your colleagues and friends if you found it useful.

Share your thoughts and ideas in the comments below. To get in touch with us, please send an email to dataspaceconsulting@gmail.com or contactus@dataspacein.com.

You can also visit our website โ€“ DataspaceAI

Leave a Reply