Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

Subqueries Introduction

Learn how to use a query inside another SQL query to retrieve and analyze related data.

What is a Subquery?

A Subquery is a query written inside another SQL query. The inner query is called the subquery, and the outer query is called the main query.

Subqueries are useful when the result of one query is required as input for another query.

Basic Structure of a Subquery


SELECT column_name
FROM table_name
WHERE column_name = (
    SELECT column_name
    FROM another_table
    WHERE condition
);
            

The inner query executes first and its result is then used by the outer query.

Simple Subquery Example

Suppose we have an Employees table:


EmployeeId    EmployeeName    Salary
-----------   -------------   -------
1             Rahul           30000
2             Priya           45000
3             Amit            50000
4             Sneha           40000
            

Now suppose we want to find employees whose salary is greater than the average salary.


SELECT EmployeeName, Salary
FROM Employees
WHERE Salary > (
    SELECT AVG(Salary)
    FROM Employees
);
            
How it works
  1. The inner query calculates the average salary.
  2. The outer query compares each employee's salary with that average.
  3. Employees whose salary is greater than the average are returned.

Subquery with WHERE

A subquery is commonly used with the WHERE clause to filter records based on the result of another query.


SELECT EmployeeName, Salary
FROM Employees
WHERE Salary > (
    SELECT Salary
    FROM Employees
    WHERE EmployeeName = 'Rahul'
);
            

In this example, the inner query finds Rahul's salary. The outer query then returns employees whose salary is greater than Rahul's salary.

Subquery with IN

The IN operator can be used when a subquery returns multiple values.


SELECT EmployeeName
FROM Employees
WHERE DepartmentId IN (
    SELECT DepartmentId
    FROM Departments
    WHERE Location = 'Pune'
);
            

The inner query returns department IDs for departments located in Pune. The outer query then returns employees belonging to those departments.

Types of Subqueries

Type Description
Single-Row Subquery Returns a single value or single row.
Multiple-Row Subquery Returns multiple rows or values.
Correlated Subquery Depends on a value from the outer query.
Subquery with DML Subqueries can be used with SELECT, INSERT, UPDATE and DELETE.

Important Points

  • A subquery is enclosed within parentheses.
  • The inner query is written inside the outer query.
  • A subquery can be used with WHERE, HAVING, FROM and other SQL clauses.
  • The type of operator used depends on whether the subquery returns one value or multiple values.
  • Subqueries are useful for solving complex SQL problems.

Summary

A subquery is a query inside another SQL query. It allows the result of one query to be used by another query. Subqueries are commonly used with WHERE and IN and are useful for filtering and analyzing related data.