By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Subqueries in SQL are powerful tools that allow you to nest one query within another. They can be used in the SELECT and FROM clauses, serving different purposes. Scalar subqueries return a single value and are used in the SELECT clause, while derived tables (or inline views) return a table and are used in the FROM clause. Mastering subqueries is crucial for writing efficient and complex SQL queries. Incorrect usage can lead to performance issues and incorrect results, affecting data integrity and decision-making. For example, a poorly written subquery can slow down a database, impacting real-time analytics and reporting.
SELECT (SELECT MAX(salary) FROM employees) AS max_salary;
⚠️ Pitfall: Avoid using subqueries unnecessarily; they can slow down performance.
Use Scalar Subqueries in SELECT
SELECT employee_id, (SELECT department_name FROM departments WHERE department_id = e.department_id) AS department_name FROM employees e;
⚠️ Pitfall: Ensure the subquery returns only one row; otherwise, it will cause an error.
Create Derived Tables in FROM
SELECT dt.department_name, COUNT(e.employee_id) AS employee_count FROM (SELECT department_id, department_name FROM departments) dt JOIN employees e ON dt.department_id = e.department_id GROUP BY dt.department_name;
⚠️ Pitfall: Be cautious with large datasets; derived tables can be memory-intensive.
Implement Correlated Subqueries
SELECT employee_id, (SELECT department_name FROM departments d WHERE d.department_id = e.department_id) AS department_name FROM employees e;
⚠️ Pitfall: Correlated subqueries can be slow; use them sparingly.
Optimize with Non-correlated Subqueries
SELECT employee_id, (SELECT MAX(salary) FROM employees) AS max_salary FROM employees;
Experts view subqueries as modular components that can be optimized and reused. They focus on breaking down complex queries into simpler, manageable parts. Instead of writing monolithic queries, they think in terms of building blocks that can be assembled efficiently. This approach not only improves readability but also enhances performance and maintainability.
Exam trap: Questions may trick you with subqueries that return multiple rows.
The mistake: Overusing correlated subqueries.
Exam trap: Performance-related questions may exploit this.
The mistake: Not aliasing derived tables.
Exam trap: Questions may involve complex joins without aliases.
The mistake: Ignoring indexes when using subqueries.
Scenario 1: You need to find the department name for each employee.Question: Write a query to retrieve the department name for each employee.Solution: 1. Use a scalar subquery in the SELECT clause.2. Join the employees and departments tables.Answer:
employees
departments
Why it works: The scalar subquery retrieves the department name for each employee.
Scenario 2: You need to count the number of employees in each department.Question: Write a query to count employees per department.Solution: 1. Create a derived table for departments.2. Join with the employees table and group by department name.Answer:
Why it works: The derived table simplifies the join and grouping operations.
Scenario 3: You need to find the maximum salary in the company.Question: Write a query to find the maximum salary.Solution: 1. Use a non-correlated subquery in the SELECT clause.2. Retrieve the maximum salary from the employees table.Answer:
Why it works: The non-correlated subquery efficiently retrieves the maximum salary.
SELECT (subquery) AS alias FROM table;
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.