SQL Tutorial
The INSERT INTO
statement in SQL is used to add new rows of data into a table in the database.
There are two main ways of using the INSERT INTO
statement:
In this case, you do not need to specify the column names where the data will be inserted. Instead, you just provide the values in the same order as the columns appear in the table.
INSERT INTO table_name VALUES (value1, value2, value3, ...);
Here, you specify both the column names and the corresponding values.
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Example:
Consider a Customers
table:
CustomerID | Name | ContactNumber |
---|---|---|
1 | John | 1234567890 |
2 | Jane | 2345678901 |
If you want to insert a new row into the Customers
table, you would use the INSERT INTO
statement as follows:
INSERT INTO Customers (CustomerID, Name, ContactNumber) VALUES (3, 'Alice', '3456789012');
After the execution of the above SQL statement, the Customers
table would look like this:
CustomerID | Name | ContactNumber |
---|---|---|
1 | John | 1234567890 |
2 | Jane | 2345678901 |
3 | Alice | 3456789012 |
This query inserts a new row into the Customers
table.
Note:
INSERT INTO
statement with the same syntax, so it's always a good idea to check the specific SQL dialect documentation.As always, the exact syntax may vary between different SQL dialects, so be sure to consult the documentation for the SQL dialect you're using.
Inserting Data into a Table in SQL:
INSERT INTO TableName (Column1, Column2) VALUES (Value1, Value2);
Using VALUES Clause in INSERT INTO:
INSERT INTO Students (StudentID, StudentName, Age) VALUES (1, 'John Doe', 25);
SQL INSERT INTO with SELECT Statement:
INSERT INTO NewTable (Column1, Column2) SELECT OldColumn1, OldColumn2 FROM ExistingTable WHERE Condition;
Specifying Column Names in INSERT INTO:
INSERT INTO TableName (Column1, Column2) VALUES (Value1, Value2);
Inserting Multiple Rows in a Single Statement:
INSERT INTO TableName (Column1, Column2) VALUES (Value1, Value2), (Value3, Value4), (Value5, Value6);
Inserting Data with DEFAULT Values in SQL:
INSERT INTO TableName (Column1, Column2, Column3) VALUES (Value1, DEFAULT, Value3);
Handling NULL Values in INSERT INTO:
INSERT INTO TableName (Column1, Column2, Column3) VALUES (Value1, NULL, Value3);
SQL INSERT INTO Example:
INSERT INTO Employees (EmployeeID, FirstName, LastName, Salary) VALUES (1, 'Alice', 'Smith', 50000), (2, 'Bob', 'Johnson', 60000), (3, 'Charlie', 'Davis', 55000);