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
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.
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.
INSERT INTO Students
VALUES
(
105,
'Kiran',
24,
'Mumbai'
);
Insert Multiple Records
SQL Server allows multiple rows to be inserted using a single INSERT statement.
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.
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.
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.
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.
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.