For working professionals
For fresh graduates
More
SQL Tutorial: Learn Structured…
1. SQL Tutorial
2. The Essential Guide To Understanding SQL In DBMS
3. SQL Commands
4. SQL Data Types
5. SQL Aliases
6. SQL INSERT INTO With Examples
Now Reading
7. Master SQL Update
8. SQL Delete Statement: A Complete Overview with Examples
9. SQL Delete Statement Example
10. SQL WHERE Clause
11. SQL AND Operator
12. SQL NOT Operator: A Comprehensive Guide
13. SQL Like
14. SQL Between Operator: A Complete Overview with Examples
15. Difference Between SQL and MySQL: Get to Know Your DBMS
16. MySQL Workbench
17. A Comprehensive Guide to MySQL Workbench Installation and Configuration
18. Mastering SQL: Your Comprehensive Guide to Becoming an SQL Developer
19. SQL CREATE TABLE With Examples
20. How To Add Columns In SQL: A Step-By-Step Guide
21. Drop Column in SQL: Everything You Need to Know
22. Index in SQL
23. Constraints in SQL: A Complete Guide with Examples
24. Schema in SQL
25. Entity Relationship Diagram (ERD) - A Complete Overview
26. Foreign Key in SQL with Examples
27. An Ultimate Guide to Understand all About Composite Keys in SQL
28. Normalization in SQL
29. Better Data Management: The Efficiency of TRUNCATE in SQL
30. Difference Between DELETE and TRUNCATE in SQL
31. SQL ORDER BY
32. SQL Not Equal Operator
33. SQL Intersect Operator: A Comprehensive Guide
34. SQL Union: Explained with Examples
35. SQL Case Statement Explained with Examples
36. Unleashing the CONCAT Function In SQL: String Manipulation Made Easy
37. Understanding and Mastering COALESCE in SQL
38. NVL in SQL
39. Understanding SQL Date Formats and Functions
40. DateDiff in SQL: A Complete Guide in 2024
41. SQL Wildcards
42. SQL DISTINCT: A Comprehensive Guide
43. LIMIT in SQL: A Comprehensive Tutorial
44. SQL Aggregate Functions
45. GROUP BY in SQL
46. SQL HAVING
47. EXISTS in SQL
48. SQL Joins
49. Inner Join in SQL
50. Left Outer Join in SQL
51. Full Outer Join in SQL
52. Cross Join in SQL
53. Self Join SQL
54. Left Join in SQL
55. Mastering SQL Substring
56. Understanding the ROW_NUMBER() Function in SQL
57. Cursor in SQL
58. Triggers In SQL
59. Stored Procedures in SQL
60. RANK Function in SQL
61. REPLACE in SQL
62. How to Delete Duplicate Rows in SQL
63. Transact-SQL
64. INSTR in SQL
65. PostgreSQL vs MySQL: Explore Key Differences
66. Mastering SQL Server Management Studio (SSMS): A Comprehensive Guide
67. Auto-Increment in SQL
68. Unveiling the Power of SQL with Python
69. SQL Vs NoSQL: Key Differences Explained
70. Advanced SQL
71. SQL Subquery
72. Second Highest Salary in SQL
73. Database Integrity Constraints: Everything You Need to Know
74. Primary Key In SQL: A Complete Guide in 2024
75. A Comprehensive Guide on View in SQL
76. Understanding PostgreSQL: A Complete Tutorial
77. SQL Injection Attack
78. MySQL database
79. What is SQLite
80. SQLite
81. ALTER Command in SQL
The SQL INSERT INTO statement is a fundamental component of database management. It allows you to add new data rows seamlessly to your tables.
Ever since its initiation, it has held a significant place in Relational Database Management Systems (RDBMS). With nearly all RDBMS offering this SQL query, its significance cannot be overstated in database operations.
Let's examine the SQL INSERT INTO statement or command in more detail. I'll provide specific examples to walk you through the command.
SQL INSERT INTO allows you to add new rows of data to your database tables effortlessly.
In this guide, I will explain the syntax of the SQL INSERT INTO statement, discuss various scenarios where it can be applied, and illustrate its usage with clear examples.
By reading this article to the end, you will have a good idea of how SQL INSERT INTO adds data to your database tables without any hitches.
The SQL INSERT INTO statement is a powerful SQL command in database management, allowing you to add one or more records to a table effortlessly.
It's important to ensure that the values you're inserting match the data type and constraints of the respective columns in the table. If any mismatch occurs, the INSERT INTO statement will generate an error.
The syntax of the SQL INSERT INTO statement is given as:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
● Here, "table_name" represents the table where you want to insert the new row(s),
● "column1", "column2", etc., are the columns where the values are to be inserted.
● Similarly, "value1", "value2", etc., are the actual values you want to insert into the corresponding columns.
Let’s say you have a table storing information about customers, and you want to add a new customer's details to it. This is where the INSERT INTO statement comes into play. Let’s say the "Customers" table has columns like "CustomerID", "FirstName", "LastName", and "Email".
CustomerID | FirstName | LastName | |
102 | Jane | Smith | |
103 | David | Johnson |
You can use the INSERT INTO statement to insert a new record into this table, specifying the values for each column.
INSERT INTO Customers (CustomerID, FirstName, LastName, Email)
VALUES (101, 'John', 'Doe', 'john.doe@example.com');
You now have:
CustomerID | FirstName | LastName | |
101 | John | Doe | |
102 | Jane | Smith | |
103 | David | Johnson |
In this example, we're inserting a new customer with CustomerID 101, first name "John", last name "Doe", and email address "john.doe@example.com" into the "Customers" table.
Here are three ways you can use the SQL INSERT INTO statement
The SQL INSERT INTO statement allows you to insert new data rows directly into your database tables. There are two main ways to insert values into a table using INSERT INTO.
Firstly, you can simply provide the values you want to insert without specifying the column names where the data will be inserted:
INSERT INTO table_name
VALUES (value1, value2, value3...);
Alternatively, you can specify both the column names and values you want to insert:
INSERT INTO table_name (column1, column2, column3...)
VALUES (value1, value2, value3...);
Let's consider a table named "Employees" with columns for EmployeeID, Name, Age, and Department.
EmployeeID | Name | Age | Department |
102 | Jane Doe | 35 | Sales |
103 | Alice Johnson | 28 | HR |
104 | Bob Brown | 40 | Operations |
105 | Emma White | 30 | Marketing |
We can use INSERT INTO to add new employees to the table:
INSERT INTO Employees (EmployeeID, Name, Age, Department)
VALUES (101, 'John Smith', 30, 'Marketing');
This SQL statement adds a new employee with the specified EmployeeID, Name, Age, and Department to the Employees table.
EmployeeID | Name | Age | Department |
101 | John Smith | 30 | Marketing |
102 | Jane Doe | 35 | Sales |
103 | Alice Johnson | 28 | HR |
104 | Bob Brown | 40 | Operations |
105 | Emma White | 30 | Marketing |
Multiple Records
Similarly, you can insert multiple records or perform SQL INSERT multiple rows by repeating the INSERT INTO statement with different values, like adding more employees to the Employees table:
INSERT INTO Employees (EmployeeID, Name, Age, Department)
VALUES (106, 'Joseph', 31, 'Sales'),
(107, 'Mary', 21, 'HR');
This inserts two new records for employees Jane Doe and Alice Johnson into the Employees table. You now have:
EmployeeID | Name | Age | Department |
101 | John Smith | 30 | Marketing |
102 | Jane Doe | 35 | Sales |
103 | Alice Johnson | 28 | HR |
104 | Bob Brown | 40 | Operations |
105 | Emma White | 30 | Marketing |
106 | Joseph | 31 | Sales |
107 | Mary | 21 | HR |
Another powerful way to utilize the SQL INSERT statement is by inserting data from one table into another using the SELECT statement. INSERT with SELECT in SQL allows you to copy data from one table and insert it into another table seamlessly. So SQL INSERT from another table is possible.
Let's explain the syntax and provide an example to help you understand this approach better.
SQL INSERT INTO SELECT Syntax:
INSERT INTO target_table_name [(column1, column2, .... column)]
SELECT column1, column2, .... Column N
FROM source_table_name [WHERE condition];
The syntax is broken down into the following components:
● INSERT INTO table_name: Specifies the target table where you want to insert the data.
● (column1, column2, .... columnN): Optional. Specifies the columns in the target table where you want to insert the data. If omitted, data will be inserted into all columns in the target table.
● SELECT column1, column2, .... columnN: Specifies the columns you want to select data from in the source table.
● FROM table_name: Specifies the source table from which you want to select data.
● [WHERE condition]: Optional. Specifies any conditions that the selected rows must meet in the source table.
For example, let's say you have a table named "Customers" with columns for CustomerID, Name, Age, and City, and you want to create a new table named "VIP_Customers" by selecting specific customers from the "Customers" table based on certain criteria.
Original table "Customers":
CustomerID | Name | Age | City |
1 | John Smith | 35 | New York |
2 | Alice Johnson | 28 | Los Angeles |
3 | Bob Brown | 40 | Chicago |
4 | Emma White | 30 | Houston |
5 | James Taylor | 45 | Miami |
You may accomplish this with the INSERT INTO SELECT statement:
INSERT INTO VIP_Customers (CustomerID, Name, Age, City)
SELECT CustomerID, Name, Age, City
FROM Customers
WHERE Age > 30;
Target table "VIP_Customers" after inserting data using SELECT statement:
CustomerID | Name | Age | City |
1 | John Smith | 35 | New York |
3 | Bob Brown | 40 | Chicago |
5 | James Taylor | 45 | Miami |
In this example, the SELECT statement retrieves data from the "Customers" table where the Age column is greater than 30, and inserts it into the "VIP_Customers" table. This allows you to create a new table containing only the customers who meet the specified criteria.
Efficient use of the SQL INSERT INTO statement can greatly streamline the process of adding data to your database. Here are some tips to make the most out of it:
Instead of executing individual INSERT statements for each record, consider using batch inserts to add multiple rows at once. This can significantly reduce the overhead associated with multiple database transactions.
For instance, using the Employee Table we used before, rather than:
INSERT INTO Employees (Name, Age, Department) VALUES (108, 'John', 30, 'Marketing');
INSERT INTO Employees (Name, Age, Department) VALUES (109, 'Jane', 35, 'Sales');
You can merge all into one statement:
INSERT INTO Employees (Name, Age, Department)
VALUES (108, 'John', 30, 'Marketing'),
(109, 'Jane', 35, 'Sales');
If your table has columns with default values defined, you can omit them from your INSERT statement. This can simplify your queries and reduce the amount of data you need to specify explicitly. For example, To insert into an Orders Table use the following command.:
INSERT INTO Orders (OrderID, CustomerID, OrderDate)
VALUES (1, 101, DEFAULT);
In this example, if OrderDate has a default value defined, you don't need to specify it explicitly.
Only include the columns in your INSERT statement that you need to populate. Omitting unnecessary columns reduces the amount of data transferred and processed, improving performance. For instance, Let’s say you want to INSERT into a student’s table:
INSERT INTO Students (Name, Age) VALUES ('Alice', 25);
If the Students table has other columns like Grade, and you're not inserting values into it, there's no need to include it in the INSERT statement.
When working with the SQL INSERT INTO statement, there are a few common pitfalls that you'll want to avoid to ensure your data is inserted correctly and efficiently. Let's delve into some of these potential stumbling blocks:
One of the most frequent errors is forgetting to specify all the columns when inserting data into a table. If you omit a column, the database will either assign a default value (if specified) or insert NULL.
For instance, if you have a table with columns for first_name, last_name, and email, make sure your INSERT INTO statement includes values for all these columns.
-- Incorrect
INSERT INTO users (thefirst_name, thelast_name) VALUES ('Jake', 'Doe');
-- Correct
INSERT INTO users (thefirst_name, thelast_name, email) VALUES ('Jake', 'Doe', 'john@example.com');
Make sure that the data types of the values you're inserting match the data types of the columns in your table. If there's a mismatch, it can lead to errors or unexpected behavior. For example, inserting a string into a numeric column or vice versa can result in a conversion error.
-- Incorrect
INSERT INTO employees (employee_id, age) VALUES ('ABC', 30);
-- Correct
INSERT INTO employees (employee_id, age) VALUES (101, 30);
If you're inserting data into a table with a primary key, be cautious not to insert duplicate primary key values. Attempting to do so will result in a primary key violation error. This commonly occurs when you're importing data from an external source or performing batch inserts.
-- Incorrect
INSERT INTO customers (customer_id, name) VALUES (101, 'John Doe');
-- Correct (assuming 101 is not already in use)
INSERT INTO customers (customer_id, name) VALUES (102, 'John Doe');
In summary, knowing how to use the SQL INSERT INTO command is crucial for managing your database well. It allows you to add new data to your tables easily and without hassle.
Throughout this guide, we've discussed how to use INSERT INTO, shown examples, and discussed how it can be used in different situations. Just remember to be careful and avoid common mistakes like forgetting to include all the necessary details or trying to add the same information twice.
Following these tips can help you become more skilled at handling your data with SQL. So, keep practicing and enjoy making your databases work smoothly!
The INSERT command in SQL is used to add new records or rows of data into a table in a database. It helps you Insert into SQL tables
To write an SQL insert script, you specify the table name and the values you want to insert into the table, following the syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
You can insert items into a table in SQL using the INSERT INTO statement followed by the table name and the values you want to insert.
The syntax of INSERT in MySQL is similar to other SQL databases:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
To insert a NULL value in SQL, you can simply specify NULL in place of the value for the respective column in the INSERT INTO statement.
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, NULL, ...);
There is no difference between INSERT and INSERT INTO in SQL. They both serve the same purpose of adding new records to a table.
In SQL Plus, you use the same INSERT INTO statement as in regular SQL to add data to a table.
Examples of INSERT statements include adding new customers to a customer table, inserting product information into a product table, or adding employee records to an employee table.
Yes, you can use INSERT INTO with a SELECT statement to insert data selected from one table into another table.
There's no fixed limit to the number of records you can insert in SQL. It depends on factors like database configuration and available system resources.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.