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

INSERT Operation

The INSERT statement is used to add new records into a SQL Server table.

INSERT is one of the most commonly used SQL commands. Whenever we want to store a new student, employee, customer, product or any other record in a table, we use the INSERT statement.

What is INSERT?

The INSERT statement adds one or more new rows to an existing table.

The values supplied in the INSERT statement are stored in the corresponding columns of the table.

INSERT Syntax

SQL
INSERT INTO table_name
(
    column1,
    column2,
    column3
)
VALUES
(
    value1,
    value2,
    value3
);

Here, the column names specify where the values should be stored, while the VALUES clause provides the actual data.

Example Table

We will use the following Students table for our examples.

StudentId StudentName Age City
101 Rahul 22 Pune
102 Priya 23 Mumbai
103 Amit 21 Nashik

Insert a Single Record

We can insert a single student record using the following INSERT statement.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    City
)
VALUES
(
    104,
    'Neha',
    22,
    'Pune'
);

Output

After executing the INSERT statement, the new record is added to the Students table.

StudentId StudentName Age City
101 Rahul 22 Pune
102 Priya 23 Mumbai
103 Amit 21 Nashik
104 Neha 22 Pune

INSERT Without Specifying Column Names

If values are supplied for every column in the exact table-column order, column names can be omitted.

SQL
INSERT INTO Students
VALUES
(
    105,
    'Kiran',
    24,
    'Mumbai'
);
Important: When column names are omitted, the values must follow the exact order of the table columns.

Insert Multiple Records

SQL Server allows multiple rows to be inserted using a single INSERT statement.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    City
)
VALUES
(106, 'Sneha', 22, 'Pune'),
(107, 'Aakash', 23, 'Mumbai'),
(108, 'Pooja', 21, 'Nashik');

Insert Data into Specific Columns

We do not always need to provide values for every column. We can specify only the columns for which we want to insert values, provided the remaining columns allow omitted values.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age
)
VALUES
(
    109,
    'Riya',
    22
);

String Values in INSERT

Character and string values are generally written inside single quotation marks.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    City
)
VALUES
(
    110,
    'Rohan',
    24,
    'Pune'
);

Numeric values such as StudentId and Age are written without quotation marks, while text values such as StudentName and City are written inside single quotes.

INSERT with NULL

If a column allows NULL values, NULL can be inserted when no value is available.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    City
)
VALUES
(
    111,
    'Vikas',
    NULL,
    'Pune'
);

Practical Example

Consider a Student Management System. When a new student takes admission, the application can store the student's information in the Students table.

SQL
INSERT INTO Students
(
    StudentId,
    StudentName,
    Age,
    City
)
VALUES
(
    112,
    'Snehal',
    23,
    'Pune'
);

After execution, a new row containing Snehal's information is added to the Students table.

Important Points

  • INSERT is used to add new records.
  • The number of values should match the specified columns.
  • String values are written inside single quotes.
  • Multiple rows can be inserted using one INSERT statement.
  • Column names should be specified whenever possible for clarity and maintainability.

Summary

In this section, we learned how the INSERT statement is used to add records to a SQL Server table. We covered inserting a single record, inserting multiple records, inserting into specific columns, using string values and inserting NULL values.