SQL INSERT INTO Examples
Let’s look at some examples of INSERT INTO statement in SQL to understand it better.
Suppose there is a Student database and we want to add values.
ROLL_NO | NAME | ADDRESS | PHONE | AGE |
---|---|---|---|---|
1 | Ram | Delhi | xxxxxxxxxxxxxx | 18 |
2 | RAMESH | GURGAON | xxxxxxxxxxxxxx | 18 |
3 | SUJIT | ROHTAK | xxxxxxxxxxxxxx | 20 |
4 | SURESH | ROHTAK | xxxxxxxxxxxxxx | 18 |
3 | SUJIT | ROHTAK | xxxxxxxxxxxxxx | 20 |
2 | RAMESH | GURGAON | xxxxxxxxxxxxxx | 18 |
Inserting Only New Values Using INSERT INTO Example
If we want to insert only values then we use the following query:
Query:
INSERT INTO Student
VALUES ('5','HARSH','WEST BENGAL', 'XXXXXXXXXX','19');
Output:
The table Student will now look like this:
ROLL_NO | NAME | ADDRESS | PHONE | Age |
---|---|---|---|---|
1 | Ram | Delhi | XXXXXXXXXX | 18 |
2 | RAMESH | GURGAON | XXXXXXXXXX | 18 |
3 | SUJIT | ROHTAK | XXXXXXXXXX | 20 |
4 | SURESH | Delhi | XXXXXXXXXX | 18 |
3 | SUJIT | ROHTAK | XXXXXXXXXX | 20 |
2 | RAMESH | GURGAON | XXXXXXXXXX | 18 |
5 | HARSH | WEST BENGAL | XXXXXXXXXX | 19 |
Insert Values to Specified Columns Using INSERT INTO Example
If we want to insert values in the specified columns then we use the following query:
Query:
INSERT INTO Student (ROLL_NO, NAME, Age)
VALUES ('5','PRATIK','19');
Output:
The table Student will now look like this:
ROLL_NO | NAME | ADDRESS | PHONE | Age |
---|---|---|---|---|
1 | Ram | Delhi | XXXXXXXXXX | 18 |
2 | RAMESH | GURGAON | XXXXXXXXXX | 18 |
3 | SUJIT | ROHTAK | XXXXXXXXXX | 20 |
4 | SURESH | Delhi | XXXXXXXXXX | 18 |
3 | SUJIT | ROHTAK | XXXXXXXXXX | 20 |
2 | RAMESH | GURGAON | XXXXXXXXXX | 18 |
5 | PRATIK | null | null | 19 |
Notice that the columns for which the values are not provided are filled by null. Which are the default values for those columns?
SQL INSERT INTO Statement
The INSERT INTO statement in SQL is used to add new records to a table in a database. It is a fundamental command for data insertion and is used to insert new data into tables.