SQL Basics
Querying Data
Filtering Data
Joining Tables
SQL Functions
Modifying Data
Defining Data
SQL INSERT
In this tutorial, you will learn how to insert new rows into your database table using the SQL INSERT statement.
Syntax
The basic syntax of the INSERT statement to add new rows can be given with:
INSERT INTO table_name (column1,column2,...)
VALUES (value1,value2,...)
Examples
To understand the INSERT statement in a better way, let’s look at the following customers and orders tables in our tutorial database:
Now, let’s check out some examples that demonstrate how it actually works.
We have the following task be to solve using SQL statements
The following Query will returns all the rows from customers table..
INSERT INTO customers
VALUES (DEFAULT,'Anna','Nixon','UK',NULL)
After executing the above query, you’ll get the result set something like this:
INSERT INTO customers
(first_name, last_name)
VALUES ('Max','Lang')
As you can see the output contains everything the whole customers tables including all rows and columns.
SELECT * helps you to examine the content of table that you are not familiar with. But be careful using it specially with big tables because database will retrieve everything which costs a lot of data movement across the network and might slow down the application
Select specific Columns from Table
Let’s say we are only interested in getting only specific columns from a table, then we could use the following syntax of the SELECT statement:
SELECT
column1,
column2,
columnN
FROM table_name
Insead of using asterisk (*) after SELECT, we will be now listing the name of columns of a table whose you want to retrive.
We have the following task to be solve using SQL statements
In the task we dont reuqire all the data, we need only specific columns. The following SQL statement selcts only first_name and country from table customers.
SELECT
first_name,
country
FROM customers
After executing the above query, you’ll get the result set something like this:
As you can see the result contains only the columns that we specified after SELECT.