SQL INSERT INTO Statement

The INSERT INTO statement in SQL is used to add new rows of data into a table. It's a fundamental command for populating and updating your database.



INSERT INTO: Definition and Usage

You can add new rows to a table by specifying values for each column. If you don't provide a value for every column, the database will either use default values (if defined for those columns) or might use `NULL` for the missing values. If you have an auto-incrementing primary key, the database will typically handle assigning a new, unique ID.

Syntax

There are two main ways to write an INSERT INTO statement:

Syntax 1: Specifying Columns

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
      
Syntax 2: Inserting into All Columns

INSERT INTO table_name
VALUES (value1, value2, value3, ...);
      

In the second syntax, you don't list the column names, but you must provide values in the same order as the columns are defined in the table.

Example Database

Here's a sample 'Customers' table:

CustomerID CustomerName ContactName Address City PostalCode Country
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite 3B Seattle 98128 USA
90 Wilman Kala Matti Karttunen Keskuskatu 45 Helsinki 21240 Finland
91 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland

Examples

Inserting a New Row (Specifying Columns)

This adds a new customer, providing values for all columns. Note that `CustomerID` is usually auto-incrementing, so you shouldn't provide a value for it.

Syntax

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
      
Output
CustomerID CustomerName ContactName Address City PostalCode Country
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite 3B Seattle 98128 USA
90 Wilman Kala Matti Karttunen Keskuskatu 45 Helsinki 21240 Finland
91 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland
92 Cardinal Tom B. Erichsen Skagen 21 Stavanger 4006 Norway

Inserting into Specific Columns

Adding a new customer, but only specifying values for some columns. Missing values will likely be NULL or use defaults.

Syntax

INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Another Company', 'New City', 'New Country');
      
Output
CustomerID CustomerName ContactName Address City PostalCode Country
93 Another Company NULL NULL New City NULL New Country
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite 3B Seattle 98128 USA
90 Wilman Kala Matti Karttunen Keskuskatu 45 Helsinki 21240 Finland
91 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland
92 Cardinal Tom B. Erichsen Skagen 21 Stavanger 4006 Norway

Inserting Multiple Rows

Adding multiple rows in a single statement. Make sure each set of values is separated by a comma.

Syntax

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');
      
Output
CustomerID CustomerName ContactName Address City PostalCode Country
94 Cardinal Tom B. Erichsen Skagen 21 Stavanger 4006 Norway
95 Greasy Burger Per Olsen Gateveien 15 Sandnes 4306 Norway
96 Tasty Tee Finn Egan Streetroad 19B Liverpool L1 0AA UK
89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Suite 3B Seattle 98128 USA
90 Wilman Kala Matti Karttunen Keskuskatu 45 Helsinki 21240 Finland
91 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland
93 Another Company NULL NULL New City NULL New Country

**Note:** The `CustomerID` column is assumed to be auto-incrementing. The outputs show sample data; your results will depend on the data in your `Customers` table. Make sure that the number of values in the `VALUES` clause matches the number of columns specified, and that the data types are compatible.