SQL INSERT INTO Statement

## SQL Basic

SQL HOME SQL Intro SQL Syntax SQL SELECT SQL SELECT DISTINCT SQL WHERE SQL AND & OR SQL ORDER BY SQL INSERT INTO SQL UPDATE SQL DELETE SQL Injection

## SQL Advanced

SQL SELECT TOP SQL LIKE SQL Wildcards SQL IN SQL BETWEEN SQL Aliases SQL Joins SQL INNER JOIN SQL LEFT JOIN SQL RIGHT JOIN SQL FULL JOIN SQL UNION SQL SELECT INTO SQL INSERT INTO SELECT SQL CREATE DB SQL CREATE TABLE SQL Constraints SQL NOT NULL SQL UNIQUE SQL PRIMARY KEY SQL FOREIGN KEY SQL CHECK SQL DEFAULT SQL CREATE INDEX SQL DROP SQL ALTER SQL Auto Increment SQL Views SQL Dates SQL NULL Values SQL NULL Functions SQL General Data Types SQL DB Data Types

## SQL Functions

SQL Functions SQL AVG() SQL COUNT() SQL FIRST() SQL LAST() SQL MAX() SQL MIN() SQL SUM() SQL GROUP BY SQL HAVING SQL UCASE() SQL LCASE() SQL MID() SQL LEN() SQL ROUND() SQL NOW() SQL FORMAT()
SQL Quick Ref SQL Hosting SQL Summary

## SQL Quiz

SQL Quiz

# SQL INSERT INTO Statement

« Previous
Next Chapter »

The INSERT INTO statement is used to insert new records in a table.

## The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

### SQL INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two forms.

The first form does not specify the column names where the data will be inserted, only their values:

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

The second form specifies both the column names and the values to be inserted:

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

## Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:

CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country
--- | --- | --- | --- | --- | --- | ---
87 | Wartian Herkku | Pirkko Koskitalo | Torikatu 38 | Oulu | 90110 | Finland
88 | Wellington Importadora | Paula Parente | Rua do Mercado, 12 | Resende | 08737-363 | Brazil
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

## INSERT INTO Example

Assume we wish to insert a new row in the "Customers" table.

We can use the following SQL statement:

## Example

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
Try it yourself »

The selection from the "Customers" table will now look like this:

CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country
--- | --- | --- | --- | --- | --- | ---
87 | Wartian Herkku | Pirkko Koskitalo | Torikatu 38 | Oulu | 90110 | Finland
88 | Wellington Importadora | Paula Parente | Rua do Mercado, 12 | Resende | 08737-363 | Brazil
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

| Did you notice that we did not insert any number into the CustomerID field? The CustomerID column is automatically updated with a unique number for each record in the table.

## Insert Data Only in Specified Columns

It is also possible to only insert data in specific columns.

The following SQL statement will insert a new row, but only insert data in the "CustomerName", "City", and "Country" columns (and the CustomerID field will of course also be updated automatically):

## Example

INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
Try it yourself »

The selection from the "Customers" table will now look like this:

CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country
--- | --- | --- | --- | --- | --- | ---
87 | Wartian Herkku | Pirkko Koskitalo | Torikatu 38 | Oulu | 90110 | Finland
88 | Wellington Importadora | Paula Parente | Rua do Mercado, 12 | Resende | 08737-363 | Brazil
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 | null | null | Stavanger | null | Norway

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE