- INSERT INTO statement in SQL is used to insert new records in a table.
- Either we can insert a new record for all columns or can insert values only for particular columns using INSERT INTO statement in SQL. Syntax of SQL INSERT INTO statement is given below.
SQL Syntax for INSERT INTO statement:
|Syntax to INSERT values for all columns||INSERT INTO
VALUES (value1, value2, value3, etc);
|Syntax to INSERT values only to selected columns||INSERT INTO
table_name (column1, column2, column3, etc)
VALUES (value1,value2,value3, etc);
Example for how to use SQL INSERT INTO statement:
Please consider the following table with few records as given below.
Table name (for example): student
Column names in this table: Student_ID, Student_name, City and Age
Available records: 4 rows
- To insert a record, i.e. Values for all columns in this table, please execute below query. This query will insert values to all columns available in this table. Please note that the order of the columns and values are matching.
INSERT INTO student (Student_ID, Student_name, City, Age)
VALUES (5, ‘Johnson’, ‘Mumbai’, 29);
SQL query Output:
- To insert a record, i.e. Values to only particular columns in this table, please execute below query. This query will insert values to only particular columns in this table that we mentioned in the query list. Please note that the order of the columns and values are matching.
INSERT INTO student (Student_ID, City)
VALUES (6, ‘Kolkatta’);
SQL query Output:
Please note that as we did not insert values for Student_name and Age columns, it is left as blank in the table. You can insert values for any particular column as you like using the above syntax.
- While inserting character or strings in a table in SQL, please use single quotes as mentioned in above queries.
- No single quotes are required for numerical values. If we give single quotes for numerical values, then it will be considered as a character or string.