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

Creating a View

Learn how to create a SQL Server View using the CREATE VIEW statement with simple and practical examples.

Introduction

A SQL View is created using the CREATE VIEW statement.

A View contains a SELECT query that defines the data that will be displayed when the View is queried.

Once created, the View can be used like a virtual table with a normal SELECT statement.

CREATE VIEW Syntax

CREATE VIEW ViewName
AS
SELECT column1, column2, ...
FROM TableName
WHERE condition;
Explanation
  • CREATE VIEW is used to create a new View.
  • ViewName is the name given to the View.
  • AS separates the View name from the SELECT query.
  • The SELECT query defines the data returned by the View.

Example Table

Consider the following Employees table:

EmployeeId EmployeeName Department Salary
1 Rahul IT 45000
2 Priya HR 40000
3 Amit IT 55000
4 Sneha Finance 50000
5 Neha IT 60000

Example 1: Create a Simple View

Suppose we want a View that displays employee name and department only.

CREATE VIEW EmployeeDepartmentView
AS
SELECT
    EmployeeName,
    Department
FROM Employees;
Execute the View
SELECT *
FROM EmployeeDepartmentView;

Example 2: View with WHERE Clause

A View can also contain a WHERE condition. For example, we can create a View that displays only IT department employees.

CREATE VIEW ITEmployeesView
AS
SELECT
    EmployeeId,
    EmployeeName,
    Department,
    Salary
FROM Employees
WHERE Department = 'IT';
Retrieve Data
SELECT *
FROM ITEmployeesView;

Example 3: Selecting Specific Columns

A View does not need to contain every column from the original table.

CREATE VIEW EmployeeSalaryView
AS
SELECT
    EmployeeName,
    Salary
FROM Employees;

Now the View exposes only the employee name and salary.

SELECT *
FROM EmployeeSalaryView;

Example 4: View Using Multiple Tables

A View can also be created using a JOIN between multiple tables.

CREATE VIEW EmployeeDepartmentDetails
AS
SELECT
    e.EmployeeId,
    e.EmployeeName,
    d.DepartmentName
FROM Employees e
INNER JOIN Departments d
    ON e.DepartmentId = d.DepartmentId;

This View combines employee information with department information.

Checking the Created View

After creating a View, we can retrieve its data using a SELECT statement.

SELECT *
FROM EmployeeDepartmentView;

SQL Server executes the query defined by the View and returns the result.

Viewing Existing Views

SQL Server provides system catalog information that can be used to find Views in a database.

SELECT
    name
FROM sys.views;

This query displays the names of Views available in the current database.

Important Points

  • Use CREATE VIEW to create a View.
  • The View definition contains a SELECT query.
  • A View can select specific columns instead of all columns.
  • A View can contain filtering conditions using WHERE.
  • A View can be based on multiple tables using JOINs.
  • After creating a View, it can be queried using SELECT.

Summary

The CREATE VIEW statement is used to create a SQL Server View. A View is based on a SELECT query and can contain selected columns, filtering conditions and data from multiple tables. Once created, it can be queried like a virtual table.