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

CTE Introduction

Learn about Common Table Expressions (CTE) in SQL Server, their syntax, uses, advantages, and practical examples.

What is a CTE?

CTE stands for Common Table Expression. It is a temporary named result set that can be referenced within a single SQL statement.

A CTE is defined using the WITH keyword and makes complex SQL queries easier to read and maintain.

CTEs are commonly used with SELECT, INSERT, UPDATE and DELETE statements.

Why Use CTE?

CTEs are useful when a SQL query becomes complex and needs to be divided into smaller logical parts.

  • Makes complex queries easier to understand.
  • Improves query readability.
  • Helps organize query logic.
  • Useful for recursive queries.
  • Can simplify queries involving joins and aggregations.

CTE Syntax

WITH CTE_Name AS
(
    SELECT column1, column2
    FROM TableName
    WHERE condition
)
SELECT *
FROM CTE_Name;

Syntax Explanation

Part Explanation
WITH Starts the Common Table Expression.
CTE_Name Name given to the CTE.
AS Defines the query that produces the CTE result.
SELECT Retrieves the required data for the CTE.
FROM Specifies the source table.

Simple CTE Example

Consider an Employees table containing employee information.

WITH HighSalaryEmployees AS
(
    SELECT EmployeeId,
           EmployeeName,
           Department,
           Salary
    FROM Employees
    WHERE Salary > 50000
)
SELECT *
FROM HighSalaryEmployees;

How Does a CTE Work?

  1. The WITH keyword starts the CTE.
  2. A name is assigned to the CTE.
  3. The query inside the parentheses creates the result set.
  4. The main query references the CTE using its name.
  5. The CTE is available only for the statement that follows it.

CTE with WHERE Clause

A CTE can contain filtering conditions using the WHERE clause.

WITH PuneEmployees AS
(
    SELECT EmployeeId,
           EmployeeName,
           City
    FROM Employees
    WHERE City = 'Pune'
)
SELECT *
FROM PuneEmployees;

CTE with Query Operations

The CTE result can be used by the main query for further filtering, sorting, joining, or aggregation.

WITH EmployeeData AS
(
    SELECT EmployeeId,
           EmployeeName,
           Salary
    FROM Employees
)
SELECT *
FROM EmployeeData
WHERE Salary > 60000
ORDER BY Salary DESC;

Advantages of CTE

  • Improves SQL query readability.
  • Makes complex queries easier to maintain.
  • Helps divide complex query logic into logical sections.
  • Supports recursive queries.
  • Can be used with multiple SQL statement types.

Important Points

  • CTE starts with the WITH keyword.
  • A CTE has a name.
  • The CTE definition is enclosed inside parentheses.
  • The statement following the CTE uses the CTE result.
  • CTEs are temporary and are not permanent database objects.

Summary

A Common Table Expression (CTE) is a temporary named result set created using the WITH keyword.

CTEs are useful for making complex SQL queries more readable and maintainable. They are also useful for joins, filtering, aggregation and recursive operations.