SQL | INSERT INTO Query
Last Updated :
11 Sep, 2023
The INSERT INTO statement of SQL is used to insert a new row in a table. There are two ways of using INSERT INTO statement for inserting rows:
- Only values:Â First method is to specify only the value of data to be inserted without the column names.
Syntax:
INSERT INTO table_name VALUES (value1, value2, value3,...);
table_name: name of the table.
value1, value2,.. : value of first column, second column,... for the new record
- Column names and values both: In the second method we will specify both the columns which we want to fill and their corresponding values as shown below:
Syntax:
INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2, value3,..);
table_name: name of the table.
column1: name of first column, second column ...
value1, value2, value3 : value of first column, second column,... for the new record
Queries:
Method 1 example:
INSERT INTO Student VALUES ('5','HARSH','WEST BENGAL','8759770477','19');
Output:
The table Student will now look like:
ROLL_NO |
NAME |
ADDRESS |
PHONE |
Age |
1 |
Ram |
Delhi |
9455123451 |
18 |
2 |
RAMESH |
GURGAON |
9562431543 |
18 |
3 |
SUJIT |
ROHTAK |
9156253131 |
20 |
4 |
SURESH |
Delhi |
9156768971 |
18 |
3 |
SUJIT |
ROHTAK |
9156253131 |
20 |
2 |
RAMESH |
GURGAON |
9562431543 |
18 |
5 |
HARSH |
WEST BENGAL |
8759770477 |
19 |
Method 2 (Inserting values in only specified columns):
INSERT INTO Student (ROLL_NO, NAME, Age) VALUES ('5','HARSH','19');
Output:
The table Student will now look like:
ROLL_NO |
NAME |
ADDRESS |
PHONE |
Age |
1 |
Ram |
Delhi |
9455123451 |
18 |
2 |
RAMESH |
GURGAON |
9562431543 |
18 |
3 |
SUJIT |
ROHTAK |
9156253131 |
20 |
4 |
SURESH |
Delhi |
9156768971 |
18 |
3 |
SUJIT |
ROHTAK |
9156253131 |
20 |
2 |
RAMESH |
GURGAON |
9562431543 |
18 |
5 |
HARSH |
WEST BENGAL |
8759770477 |
19 |
Share your thoughts in the comments
Please Login to comment...