EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Data Science Data Science Tutorials SQL Tutorial SQL Insert Query

SQL Insert Query

Priya Pedamkar
Article byPriya Pedamkar

Updated March 16, 2023

SQL Insert Query

Introduction on SQL Insert Query

In SQL, the ‘Insert’ command is used to add new records into the table in a database. An ‘Insert’ statement can be used to insert single row records or multiple rows of records, depending on the requirement. Insert command can also be used along with the select command when there is a need to make a copy of records from one table and insert those records into another table in the database.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

We can add new rows of data to the existing table by using the INSERT queries in SQL. Below is the syntax used for inserting values to a table with the specific columns we want certain values to be inserted.

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

Here we want to insert data for columns: column1, column2, column3, and so on, and the values to be inserted are: value1, value2, value3, etc.

Also, if we want to insert values to all the columns of a table, there is no need to specify the column names in the INSERT query. However, we should be careful in maintaining the order of the columns, which should be the same as the columns in the table. The following syntax can achieve this:

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

It is also possible to insert data into one table from another. For example, we can insert the data from another table by using the Select statement inside the Insert query.

If we want to populate the data of all the columns from the second to the first table, we can use the below syntax.

INSERT INTO table_name_1 SELECT * FROM table_name_2;

We can insert data from the second table to the first table and the columns specified will be inserted to the first table with the values from the second table below.

INSERT INTO table_name_1 (column1, column2,column3....columnN) 
SELECT (column1, column2, column3.....columnN) FROM table_name_2;

How does Insert Query work in SQL?

Insert operation results in addition to one or more rows to the existing rows in a table. Thus, insert queries can be used to insert specific column values or values to all the columns in a table. Also, with the usage of the SELECT statement in the INSERT query, we can add or populate the data or add values from another table to a particular table. But we should be careful in taking into consideration the table structure in such cases where there might be any discrepancy in the columns of the two tables in question.

How to Use Insert Query in SQL?

The insert query must contain the keyword INSERT. Also, the columns and values must be in proper order. In the case that the columns are of characters or strings, the values to be inserted need to be specified within quotes. If the columns of the table to which the data is to be inserted are not defined in the query, then the values will be inserted to all the columns in that table.

Examples of SQL Insert Query

Let us see how the insert queries work.

For example, we have the below EMPLOYEE table.

SQL Insert Query-1.1

Now, we want to add values to the above table, and it can be done as below:

INSERT INTO EMPLOYEE (ID, NAME, AGE, SALARY) VALUES (‘7899’, ‘Raj’, ‘34’, ‘35890.00’);

After running the above query, we will get the below result:

Select * from EMPLOYEE;

INSERT INTO EMPLOYEE -1.2

As we are inserting the values to all the columns in the table, we can use the below query for the same operation.

INSERT INTO EMPLOYEE  VALUES (‘7899’, ‘Raj’, ‘34’, ‘35890.00’);

If we want to insert values only to specific columns, it can be achieved by the below query.

INSERT INTO EMPLOYEE (ID, NAME) VALUES (‘7899’, ‘Raj’);

After running the above query, we will get the below result:

Select * from EMPLOYEE;

INSERT INTO EMPLOYEE (ID, NAME)-1.3

Here we can see that the column for which no value is inserted has a null value.

Let us see how we can insert data from a second table, ‘DETAILS’, to the existing table ‘EMPLOYEE’. Below is the table ‘DETAILS’.

SQL Insert Query-1.4

In order to insert the data from the table Details to EMPLOYEE, we can use the below query.

INSERT INTO EMPLOYEE SELECT * FROM DETAILS;

Here the data from the table DETAILS will be added to the EMPLOYEE table as below.

Select * from EMPLOYEE;

SQL Insert Query-1.5

Similarly, the data for specific columns from the table DETAILS can be populated to the EMPLOYEE table accordingly.

Conclusion

Insert query in SQL can be used to add new rows of data to the existing table. The insert statements can be used to add values for specific or all the columns of a table. We should be careful in maintaining the order of the columns, data types while inserting the values to the columns.

Recommended Articles

This is a guide to SQL Insert Query. Here we discuss the basic concept and how Insert Query Works in SQL and Examples of SQL Insert Query. You may also look at the following articles to learn more –

  1. MySQL Users
  2. MySQL REINDEX
  3. MySQL REPLACE
  4. MySQL Admin Tool
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW