Fatskills
Practice. Master. Repeat.
Study Guide: Database-Systems Subqueries Subqueries in SELECT and FROM Scalar Subqueries Derived Tables
Source: https://www.fatskills.com/databases/chapter/database-systems-subqueries-subqueries-in-select-and-from-scalar-subqueries-derived-tables

Database-Systems Subqueries Subqueries in SELECT and FROM Scalar Subqueries Derived Tables

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

What This Is and Why It Matters

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.

Core Knowledge (What You Must Internalize)

  • Subquery: A query nested inside another query. (Why this matters: It allows for more complex and dynamic data retrieval.)
  • Scalar Subquery: Returns a single value. (Why this matters: Useful for calculations and comparisons in the SELECT clause.)
  • Derived Table: Returns a table that can be used in the FROM clause. (Why this matters: Helps in breaking down complex queries into manageable parts.)
  • Correlated Subquery: Depends on the outer query for its values. (Why this matters: Allows for row-by-row processing, useful in complex conditions.)
  • Non-correlated Subquery: Independent of the outer query. (Why this matters: Can be executed once and reused, improving performance.)
  • Key Principle: Subqueries should be used judiciously to avoid performance bottlenecks. (Why this matters: Efficient queries are essential for database performance.)

Step‑by‑Step Deep Dive

  1. Understand the Basics of Subqueries
  2. Action: Identify where a subquery can be used.
  3. Principle: Subqueries can be placed in SELECT, FROM, WHERE, and other clauses.
  4. Example: SELECT (SELECT MAX(salary) FROM employees) AS max_salary;
  5. ⚠️ Pitfall: Avoid using subqueries unnecessarily; they can slow down performance.

  6. Use Scalar Subqueries in SELECT

  7. Action: Write a scalar subquery that returns a single value.
  8. Principle: Scalar subqueries are used for calculations or comparisons.
  9. Example: SELECT employee_id, (SELECT department_name FROM departments WHERE department_id = e.department_id) AS department_name FROM employees e;
  10. ⚠️ Pitfall: Ensure the subquery returns only one row; otherwise, it will cause an error.

  11. Create Derived Tables in FROM

  12. Action: Define a derived table using a subquery.
  13. Principle: Derived tables act as temporary tables within the query.
  14. Example: 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;
  15. ⚠️ Pitfall: Be cautious with large datasets; derived tables can be memory-intensive.

  16. Implement Correlated Subqueries

  17. Action: Write a correlated subquery that depends on the outer query.
  18. Principle: Correlated subqueries are evaluated for each row of the outer query.
  19. Example: SELECT employee_id, (SELECT department_name FROM departments d WHERE d.department_id = e.department_id) AS department_name FROM employees e;
  20. ⚠️ Pitfall: Correlated subqueries can be slow; use them sparingly.

  21. Optimize with Non-correlated Subqueries

  22. Action: Use a non-correlated subquery for better performance.
  23. Principle: Non-correlated subqueries are executed once and reused.
  24. Example: SELECT employee_id, (SELECT MAX(salary) FROM employees) AS max_salary FROM employees;
  25. ⚠️ Pitfall: Verify that the subquery logic is correct; incorrect logic can lead to wrong results.

How Experts Think About This Topic

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.

Common Mistakes (Even Smart People Make)

  1. The mistake: Using a subquery that returns multiple rows in a scalar context.
  2. Why it's wrong: This causes a runtime error.
  3. How to avoid: Always check that scalar subqueries return a single value.
  4. Exam trap: Questions may trick you with subqueries that return multiple rows.

  5. The mistake: Overusing correlated subqueries.

  6. Why it's wrong: This can significantly slow down query performance.
  7. How to avoid: Use joins or non-correlated subqueries where possible.
  8. Exam trap: Performance-related questions may exploit this.

  9. The mistake: Not aliasing derived tables.

  10. Why it's wrong: This can lead to ambiguous column references.
  11. How to avoid: Always alias derived tables for clarity.
  12. Exam trap: Questions may involve complex joins without aliases.

  13. The mistake: Ignoring indexes when using subqueries.

  14. Why it's wrong: This can degrade performance.
  15. How to avoid: Check and use indexes to optimize subquery performance.
  16. Exam trap: Performance optimization questions may test this.

Practice with Real Scenarios

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:


SELECT employee_id, (SELECT department_name FROM departments d WHERE d.department_id = e.department_id) AS department_name FROM employees e;

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:


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;

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:


SELECT (SELECT MAX(salary) FROM employees) AS max_salary;

Why it works: The non-correlated subquery efficiently retrieves the maximum salary.

Quick Reference Card

  • Core Rule: Subqueries can be used in SELECT and FROM clauses for complex data retrieval.
  • Key Formula: SELECT (subquery) AS alias FROM table;
  • Critical Facts:
  • Scalar subqueries return a single value.
  • Derived tables act as temporary tables.
  • Correlated subqueries depend on the outer query.
  • Dangerous Pitfall: Using subqueries that return multiple rows in a scalar context.
  • Mnemonic: "Scalar for single, derived for table, correlated for row-by-row."

If You're Stuck (Exam or Real Life)

  • Check: The structure of your subquery.
  • Reason: From first principles, breaking down the query into simpler parts.
  • Estimate: The expected output to verify your query logic.
  • Find: The answer by referring to SQL documentation or querying smaller datasets.

Related Topics

  • Joins: Understand how to use joins effectively to combine data from multiple tables.
  • Indexes: Learn about indexing to optimize query performance.
  • Query Optimization: Study techniques for optimizing SQL queries for better performance.


ADVERTISEMENT