Microsoft SQL Server Certification Course
- 6k Enrolled Learners
- Weekend
- Live Class
Structured Query Language or most commonly known as SQL, is one of the most popular languages used in relational databases. This language is used to handle databases and manipulate data with the help of queries. One such query is the Insert Query. So, in this article on Insert Query SQL, you will understand the INSERT INTO statement in the following sequence:
The SQL INSERT INTO statement is used to add new tuples into a table, for a database. With the help of this SQL query, you can insert data either into a specific column or for all columns in general. Also, you can insert data to a specific table from another table for a single row or multiple rows. So, now that you what is INSERT query in SQL, let us move forward and see the syntax of this query.
There are two ways through which you can implement the INSERT query.
INSERT INTO Tablename (Column1, Column2, Column3, ...,ColumnN) VALUES (Value1, Value2, Value3, ...);
INSERT INTO Tablename VALUES (Value1, Value2, Value3, ...);
Note: When you use the second method, you have to make sure that the values are mentioned in the same order as that of column names.
So, now, that you know the syntax of the INSERT statement, next in this article on Insert query SQL, let us see an example.
Learn more about SQL Server and its framework from the SQL Server Certification.
Consider the following table with the table name as SampleData:
ID | Name | Age | PhoneNumber | Salary |
1 | Sanjay | 23 | 9876543210 | 30000 |
2 | Rhea | 30 | 9977742234 | 150000 |
3 | Vipul | 32 | 9898989898 | 175000 |
4 | Simran | 28 | 9955555433 | 65000 |
5 | Akshay | 34 | 9646434437 | 200000 |
Now, let us say you want to insert a row in this table. Then, you can use either of the above syntaxes in the following way:
#With column names and values INSERT INTO SampleData(ID, Name, Age, PhoneNumber, Salary) VALUES ('6', 'Rohit','25', '9924388761', '35000'); #With values only INSERT INTO SampleData VALUES ('6', 'Rohit','25', '9924388761', '35000');
Once you execute the query, you will see the below output:
ID | Name | Age | PhoneNumber | Salary |
1 | Sanjay | 23 | 9876543210 | 30000 |
2 | Rhea | 30 | 9977742234 | 150000 |
3 | Vipul | 32 | 9898989898 | 175000 |
4 | Simran | 28 | 9955555433 | 65000 |
5 | Akshay | 34 | 9646434437 | 200000 |
6 | Rohit | 25 | 9924388761 | 35000 |
Well, this was about inserting a new record to a table. But, there may be a few other scenarios, where you would want to use SQL. The scenarios can be as follows:
The answer to these questions is by using the SELECT statement with the INSERT statement. So, next in this article on Insert Query SQL, let us understand how to use the SELECT statement in INSERT INTO.
The SELECT query is used along with the INSERT INTO statement to select the data from another table. The following are the various ways to use the SELECT statement with the INSERT query in SQL:
You can insert a specific set of rows from a table to another table by using the SELECT statement with the WHERE clause.
INSERT INTO Table1 SELECT * FROM Table2 WHERE condition;
Here, you are trying to insert values from Table2 to Table1 based on a condition.
Consider an example, where you have to insert a few rows from our above table(SampleData) to a new table (New_Data) based on the condition Age > 30
INSERT INTO New_Data SELECT * FROM SampleData WHERE Age > 30;
Output:
ID | Name | Age | PhoneNumber | Salary |
3 | Vipul | 32 | 9898989898 | 175000 |
5 | Akshay | 34 | 9646434437 | 200000 |
You can insert all the columns from a table to another table by using the asterisk(*) with INSERT INTO query.
INSERT INTO Table1 SELECT * FROM Table2;
Here, you are trying to insert values all columns from Table2 to Table1.
Consider an example, where you have to insert all columns from our above table(SampleData) to a new table (ExampleData). Also, consider that ExampleData already has the following data present:
ID | Name | Age | PhoneNumber | Salary |
7 | Suhas | 23 | 9876543239 | 42000 |
8 | Meena | 31 | 9765412345 | 192000 |
Now, execute the following query to insert all the columns and rows from SampleData to the above table.
INSERT INTO ExampleData SELECT * FROM SampleData;
Output:
ID | Name | Age | PhoneNumber | Salary |
7 | Suhas | 23 | 9876543239 | 42000 |
8 | Meena | 31 | 9765412345 | 192000 |
1 | Sanjay | 23 | 9876543210 | 30000 |
2 | Rhea | 30 | 9977742234 | 150000 |
3 | Vipul | 32 | 9898989898 | 175000 |
4 | Simran | 28 | 9955555433 | 65000 |
5 | Akshay | 34 | 9646434437 | 200000 |
You can insert a specific set of columns from a table to another table by using the SELECT statement.
INSERT INTO Table1(Column_Names) SELECT Column_Names FROM Table2;
Here, you are trying to insert specific columns from Table2 to Table1.
Consider an example, where you have to insert columns(ID, Name ) from the table(ExampleData) to the table(SampleData).
INSERT INTO SampleData(ID,Name) SELECT ID, Name, FROM ExampleData;
ID | Name | Age | PhoneNumber | Salary |
1 | Sanjay | 23 | 9876543210 | 30000 |
2 | Rhea | 30 | 9977742234 | 150000 |
3 | Vipul | 32 | 9898989898 | 175000 |
4 | Simran | 28 | 9955555433 | 65000 |
5 | Akshay | 34 | 9646434437 | 200000 |
7 | Suhas | null | null | null |
8 | Meena | null | null | null |
With this, we come to an end to this article on Insert Query SQL. I hope you understood how to use INSERT INTO query in SQL. We have seen the various ways to use the INSERT query. If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.
Got a question for us? Please mention it in the comments section of ”Insert Query SQL” and I will get back to you.
edureka.co