codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

28 / 67 topics
28Subqueries Basics29Correlated Subqueries30Views
Tutorials/SQL & Databases/Subqueries Basics
🗄️SQL & Databases

Subqueries Basics

Updated 2026-04-20
4 min read

Subqueries Basics

Subqueries, also known as nested queries or inner queries, are a powerful feature of SQL that allows you to embed one query inside another. This technique enables complex data retrieval and manipulation by breaking down the problem into smaller, more manageable parts. In this tutorial, we will explore the fundamentals of subqueries, including their syntax, types, and best practices.

What is a Subquery?

A subquery is a SQL query that is embedded within another SQL query. The outer query, also known as the main query or parent query, uses the results of the inner query to filter or retrieve data. Subqueries can be used in various parts of a SQL statement, such as the SELECT, FROM, WHERE, and HAVING clauses.

Types of Subqueries

Subqueries can be categorized based on their position within the main query and the number of rows they return. Understanding these types is crucial for effective subquery usage.

1. Single-Row Subqueries

Single-row subqueries return a single row from the inner query. These are typically used with comparison operators like =, <, >, <=, >=, or <>.

Example:

SELECT employee_id, first_name, last_name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);

In this example, the subquery (SELECT MAX(salary) FROM employees) returns a single value, which is then used to filter employees whose salary matches the maximum salary.

2. Multiple-Row Subqueries

Multiple-row subqueries return more than one row from the inner query. These are often used with IN, ANY, or ALL operators.

Example:

SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1700);

Here, the subquery (SELECT department_id FROM departments WHERE location_id = 1700) returns multiple department IDs, and the main query retrieves employees belonging to these departments.

3. Correlated Subqueries

Correlated subqueries are those where the inner query is dependent on the outer query. The inner query executes once for each row of the outer query.

Example:

SELECT employee_id, first_name, last_name, salary
FROM employees e1
WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e2.department_id = e1.department_id);

In this example, the subquery (SELECT AVG(salary) FROM employees e2 WHERE e2.department_id = e1.department_id) calculates the average salary for each department and compares it with the employee's salary.

4. Scalar Subqueries

Scalar subqueries return a single value from the inner query. They are often used in the SELECT clause to retrieve additional information.

Example:

SELECT employee_id, first_name, last_name,
       (SELECT department_name FROM departments WHERE employees.department_id = departments.department_id) AS department_name
FROM employees;

Here, the subquery (SELECT department_name FROM departments WHERE employees.department_id = departments.department_id) retrieves the department name for each employee.

Subquery Placement

Subqueries can be placed in various parts of a SQL statement. Understanding where to place them is essential for their correct execution and performance.

1. In the WHERE Clause

Subqueries in the WHERE clause are used to filter rows based on conditions derived from another query.

Example:

SELECT employee_id, first_name, last_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE location_id = 1700);

2. In the FROM Clause

Subqueries in the FROM clause are used to create a derived table, which is then treated as a regular table in the main query.

Example:

SELECT AVG(salary) AS avg_salary
FROM (SELECT salary FROM employees WHERE department_id = 50) AS dept_50_salaries;

3. In the SELECT Clause

Subqueries in the SELECT clause are used to retrieve additional information for each row.

Example:

SELECT employee_id, first_name, last_name,
       (SELECT department_name FROM departments WHERE employees.department_id = departments.department_id) AS department_name
FROM employees;

4. In the HAVING Clause

Subqueries in the HAVING clause are used to filter groups based on conditions derived from another query.

Example:

SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > (SELECT AVG(salary) FROM employees);

Best Practices for Using Subqueries

  1. Use Correlated Subqueries Wisely: Correlated subqueries can be slow if not optimized properly, as they execute once for each row of the outer query.

  2. Avoid Deeply Nested Subqueries: Excessive nesting can make queries difficult to read and maintain. Consider breaking down complex queries into simpler parts or using temporary tables.

  3. Use EXISTS Instead of IN for Large Sets: When checking for the existence of a value in a large set, EXISTS is generally more efficient than IN.

  4. Optimize Subquery Performance: Ensure that subqueries are optimized by indexing relevant columns and avoiding unnecessary computations.

  5. Document Complex Queries: Clearly document complex queries with comments to explain the logic and purpose of each part.

Real-World Example

Let's consider a real-world scenario where we need to find employees who have a higher salary than the average salary in their department and are located in a specific city.

Tables:

  1. employees (employee_id, first_name, last_name, salary, department_id)
  2. departments (department_id, department_name, location_id)
  3. locations (location_id, city)

Query:

SELECT e.employee_id, e.first_name, e.last_name, d.department_name, l.city
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN locations l ON d.location_id = l.location_id
WHERE e.salary > (SELECT AVG(e2.salary) FROM employees e2 WHERE e2.department_id = e.department_id)
  AND l.city = 'New York';

Explanation:

  1. Join Operations: We join the employees, departments, and locations tables to retrieve all necessary information.
  2. Subquery in WHERE Clause: The subquery (SELECT AVG(e2.salary) FROM employees e2 WHERE e2.department_id = e.department_id) calculates the average salary for each department.
  3. Filter Conditions: We filter employees whose salary is greater than the department's average and are located in 'New York'.

Conclusion

Subqueries are a fundamental tool in SQL that enhance query flexibility and power. By understanding their types, placement, and best practices, you can write efficient and effective queries to retrieve complex data from your databases. Always strive to optimize subqueries for performance and maintainability.


This comprehensive guide should provide you with a solid foundation in subquery basics, enabling you to tackle more advanced SQL challenges and optimizations.


PreviousSelf JoinNext Correlated Subqueries

Recommended Gear

Self JoinCorrelated Subqueries