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

Subquery with SELECT, INSERT, UPDATE & DELETE

Learn how subqueries can be used with SELECT, INSERT, UPDATE and DELETE statements.

Introduction

A subquery can be used not only with SELECT statements, but also with INSERT, UPDATE and DELETE statements.

The subquery provides the required data or condition that is used by the main SQL statement.

1. SELECT with Subquery

A subquery can be used inside a SELECT statement to filter records based on the result of another query.


SELECT EmployeeName, Salary
FROM Employees
WHERE Salary >
(
    SELECT AVG(Salary)
    FROM Employees
);
            

The inner query calculates the average salary and the outer query returns employees whose salary is greater than the average.

2. INSERT with Subquery

A subquery can be used with INSERT to insert the result of a SELECT query into another table.

Suppose we have an archive table:


CREATE TABLE EmployeeArchive
(
    EmployeeId INT,
    EmployeeName VARCHAR(100),
    Salary DECIMAL(10,2)
);
            

We can insert selected employees using a subquery:


INSERT INTO EmployeeArchive
    (EmployeeId, EmployeeName, Salary)
SELECT EmployeeId, EmployeeName, Salary
FROM Employees
WHERE DepartmentId IN
(
    SELECT DepartmentId
    FROM Departments
    WHERE Location = 'Pune'
);
            

The inner query finds departments located in Pune. The SELECT query then gets employees belonging to those departments and inserts them into EmployeeArchive.

3. UPDATE with Subquery

A subquery can be used in an UPDATE statement to determine which records should be updated.

Example: Increase the salary of employees who belong to the IT department.


UPDATE Employees
SET Salary = Salary + 5000
WHERE DepartmentId =
(
    SELECT DepartmentId
    FROM Departments
    WHERE DepartmentName = 'IT'
);
            

The subquery finds the department ID of the IT department. The UPDATE statement then increases the salary of employees belonging to that department.

4. DELETE with Subquery

A subquery can also be used with DELETE to identify the records that should be removed.

Example: Delete employees who belong to a department located in Mumbai.


DELETE FROM Employees
WHERE DepartmentId IN
(
    SELECT DepartmentId
    FROM Departments
    WHERE Location = 'Mumbai'
);
            

The inner query finds the departments located in Mumbai. The DELETE statement removes employees belonging to those departments.

Subquery with SQL Statements

Statement Purpose Example Use
SELECT Retrieve records based on another query. Find employees above average salary.
INSERT Insert data obtained from another query. Copy selected employees to an archive.
UPDATE Update records selected using a subquery. Update employees of a particular department.
DELETE Delete records identified by a subquery. Delete employees from a particular location.

Important Points

  • A subquery can be used with SELECT, INSERT, UPDATE and DELETE.
  • Always verify the result of the subquery before performing UPDATE or DELETE operations.
  • INSERT ... SELECT is useful for copying selected records between tables.
  • UPDATE with a subquery can be used to modify records based on related data.
  • DELETE with a subquery can remove records based on conditions from another table.
  • Be especially careful with UPDATE and DELETE because they can modify or remove multiple records.

Summary

Subqueries can be used with SELECT, INSERT, UPDATE and DELETE statements. They allow one query to provide data or conditions for another SQL operation and are useful when working with related data across tables.