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 Tables

A table is used to store data in rows and columns inside a SQL Server database. In this section, we will learn how to create tables, define columns, select data types and insert records.

What is a Table?

A table is a database object that stores related information in the form of rows and columns.

Each column defines a particular type of information, while each row represents one complete record.

SQL Server
Students
------------------------------------------------
StudentId | StudentName | Age | Course
------------------------------------------------
1         | Rahul       | 22  | Java
2         | Priya       | 21  | .NET
3         | Amit        | 23  | Python

CREATE TABLE Syntax

The CREATE TABLE statement is used to create a new table.

SQL
CREATE TABLE TableName
(
    Column1 DataType,
    Column2 DataType,
    Column3 DataType
);

Create a Simple Table

First select the database in which you want to create the table.

SQL
USE StudentDB;

CREATE TABLE Students
(
    StudentId INT,
    StudentName VARCHAR(100),
    Age INT,
    Course VARCHAR(50)
);

Defining Columns and Data Types

Every column should have an appropriate data type according to the kind of data it will store.

Column Data Type Purpose
StudentId INT Stores student number.
StudentName VARCHAR(100) Stores student name.
Age INT Stores student's age.
Course VARCHAR(50) Stores course name.

Insert Data into the Table

After creating a table, the INSERT INTO statement can be used to add records.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    Course
)
VALUES
(
    1,
    'Rahul',
    22,
    'Java'
);

Insert Multiple Records

Multiple records can also be inserted using a single INSERT statement.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    Course
)
VALUES
(1, 'Rahul', 22, 'Java'),
(2, 'Priya', 21, '.NET'),
(3, 'Amit', 23, 'Python');

Display Table Data

The SELECT statement is used to retrieve records from the table.

SQL
SELECT *
FROM Students;

Output

StudentId StudentName Age Course
1 Rahul 22 Java
2 Priya 21 .NET
3 Amit 23 Python

Practical Example

The following example creates an Employee table with commonly used columns.

SQL
CREATE TABLE Employees
(
    EmployeeId INT,
    EmployeeName VARCHAR(100),
    Email VARCHAR(150),
    Age INT,
    Salary DECIMAL(10,2),
    JoiningDate DATE
);

Verify the Table

The following query can be used to view the columns and structure of a table.

SQL
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Students';

Table Creation Flow

SQL Server
Select Database
       ↓
CREATE TABLE
       ↓
Define Columns
       ↓
Choose Data Types
       ↓
Insert Records
       ↓
SELECT Data
Important

Always choose appropriate data types for columns. A good table structure makes data easier to store, retrieve and maintain.

Summary

In this section, we learned how to create a table using CREATE TABLE, define columns and data types, insert records and retrieve table data using SELECT.