MODULO SQL & Database Design Master SeriesLAB

Unlocking the Power of Subqueries: Mastering Scalar, Correlated, and Nested Queries

Introduction to Subqueries

A subquery is a query nested inside another query. The inner query is used to get a set of rows, and the outer query is used to get the desired result from the inner query. Subqueries can be used to perform complex operations, such as filtering data based on conditions that cannot be achieved using a simple WHERE clause.

6.1 Types of Subqueries

There are several types of subqueries, each with its own strengths and use cases. In this section, we will explore three main types of subqueries: Scalar, Correlated, and Nested subqueries.

6.1.1 Scalar Subqueries

A scalar subquery is a subquery that returns a single value. It can be used in the SELECT, WHERE, or HAVING clause of a query.

Characteristics of Scalar Subqueries:

  • Returns a single value
  • Can be used in the SELECT, WHERE, or HAVING clause
  • Can be used with comparison operators (=, <, >, etc.)

Example of a Scalar Subquery:

SELECT *
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

In this example, the subquery (SELECT AVG(salary) FROM employees) returns the average salary of all employees. The outer query then selects all employees who have a salary greater than the average salary.

6.1.2 Correlated Subqueries

A correlated subquery is a subquery that references the outer query. It is used to compare a value in the outer query with a set of values in the inner query.

Characteristics of Correlated Subqueries:

  • References the outer query
  • Can be used in the WHERE or HAVING clause
  • Can be used with comparison operators (=, <, >, etc.)

Example of a Correlated Subquery:

SELECT *
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE department = e.department);

In this example, the subquery (SELECT AVG(salary) FROM employees WHERE department = e.department) returns the average salary of employees in the same department as the current employee being processed in the outer query. The outer query then selects all employees who have a salary greater than the average salary in their department.

6.1.3 Nested Subqueries

A nested subquery is a subquery that is nested inside another subquery. It can be used to perform complex operations, such as filtering data based on conditions that cannot be achieved using a simple WHERE clause.

Characteristics of Nested Subqueries:

  • Can be used to perform complex operations
  • Can be used in the WHERE or HAVING clause
  • Can be used with comparison operators (=, <, >, etc.)

Example of a Nested Subquery:

SELECT *
FROM employees
WHERE department IN (
  SELECT department
  FROM (
    SELECT department, AVG(salary) AS avg_salary
    FROM employees
    GROUP BY department
  ) AS subquery
  WHERE avg_salary > 50000
);

In this example, the innermost subquery (SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department) returns the average salary of each department. The middle subquery (SELECT department FROM (...) WHERE avg_salary > 50000) returns the departments that have an average salary greater than 50000. The outer query then selects all employees who are in one of these departments.

Best Practices for Using Subqueries

  • Use subqueries judiciously, as they can impact performance
  • Avoid using correlated subqueries when possible, as they can be slow
  • Use indexes to improve the performance of subqueries
  • Consider rewriting subqueries as joins or Common Table Expressions (CTEs) for better performance

Common Use Cases for Subqueries

  • Filtering data based on conditions that cannot be achieved using a simple WHERE clause
  • Performing complex aggregations, such as calculating the average salary of each department
  • Retrieving data from multiple tables, such as retrieving all employees who are in a specific department

Conclusion

Subqueries are a powerful tool in SQL that can be used to perform complex operations. By understanding the different types of subqueries, including scalar, correlated, and nested subqueries, you can write more efficient and effective queries. Remember to use subqueries judiciously and consider rewriting them as joins or CTEs for better performance.

⚔️ YOUR MISSION

Mission: Write a query that retrieves all employees who have a salary greater than the average salary of their department. Use a correlated subquery to achieve this.

Constraints:

  • Use the employees table
  • Assume the department column is in the employees table
  • Assume the salary column is in the employees table
SQL practice editor
Read the exercise prompt
SELECT *
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE department = e.department);
WAITING FOR SIGNAL...
BACKNEXT STAGE