Insert Into SQL – How To Insert Into A Table Query [Example Statement]
Maybe your like
If you want to add data to your SQL table, then you can use the INSERT statement.
In this article, I will show you how to use the INSERT statement to add a single row, multiple rows, and to copy rows from one SQL table to another.
Basic INSERT syntax
Here is the basic syntax for adding rows to a table in SQL:
INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc);The first line of code uses the INSERT statement followed by the name of the table you want to add the data to. After the table name, you should specify the column names.
The second line of code is where you will add the values for the rows. It is important that the number of values matches with the number of columns specified or else you will get an error message.
How to add a row to a SQL table
In this example we have a table called dogs with the columns of id, name and gender. We want to add one dog called AXEL.
This is what the code looks like to add AXEL to the table:
INSERT INTO dogs(id, name, gender) VALUES (1, 'AXEL', 'M');This is what the table looks like.

What happens if the number of values does not match the columns?
As mentioned earlier, the number of columns has to match with the number of values.
If I alter the code to remove a value, then I would get an error message.
INSERT INTO dogs(id, name, gender) VALUES (1, 'AXEL');
Since we specified 3 columns, we need to provide three values for each row added to the table.
What happens if you ignore column constraints?
When you create SQL tables, you will add column constraints which serve as rules for the column.
In our dogs table, the name and gender columns have a constraint of NOT NULL. This rule means that a value cannot be absent from the row.
When I try to add NULL for the gender, I come back with an error message.
INSERT INTO dogs(id, name, gender) VALUES (1, 'AXEL', NULL);
Any constraints that you specified in the creation of your SQL table need to be respected when adding rows.
How to add multiple rows to a table in SQL
If you want to add multiple rows to a table all at once, then you can use this syntax:
INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc), (value1, value2, value3, etc), (value1, value2, value3, etc);It is important to remember the commas between each of the rows or else you will get an error message.

This is what the code would look like to add eight dogs to the table all at once:
INSERT INTO dogs(id, name, gender) VALUES (1, 'AXEL', 'M'), (2, 'Annie', 'F'), (3, 'Ace', 'M'), (4, 'Zelda', 'F'), (5, 'Diesel', 'M'), (6, 'Tilly', 'F'), (7, 'Leroy', 'M'), (8, 'Olivia', 'F');This is what the table looks like now:

How to copy rows from one table and insert them into another table
You can use the SELECT and INSERT statements to copy rows from one SQL table to another.
This is the basic syntax:
INSERT INTO table_name1 (columns) SELECT columns FROM table_name2;In this example, I have created a cats table with three rows in it with the same column names as the dogs table.

We can add all of the cats data into the dogs table using the following code:
INSERT INTO dogs SELECT * FROM cats;This is what the new dogs table looks like with the additional cats:

Conclusion
If you want to add data to your SQL table, then you can use the INSERT statement.
Here is the basic syntax for adding rows to your SQL table:
INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc);The second line of code is where you will add the values for the rows. It is important that the number of values matches with the number of columns specified or else you will get an error message.
When you try to ignore column constraints in adding rows to the table, then you will receive an error message.
If you want to add multiple rows to a table all at once, then you can use this syntax:
INSERT INTO table_name (column1, column2, column3,etc) VALUES (value1, value2, value3, etc), (value1, value2, value3, etc), (value1, value2, value3, etc);You can use the SELECT and INSERT statement to copy rows from one SQL table to another.
This is the basic syntax:
INSERT INTO table_name1 (columns) SELECT columns FROM table_name2;I hope you enjoyed this article and best of luck on your SQL journey.
Tag » Add Element In Sql Server
-
SQL INSERT - Insert One Or More Rows Into A Table - Zentut
-
SQL INSERT INTO Statement - W3Schools
-
SQL Server INSERT: Adding A Row Into A Table By Practical ...
-
INSERT (Transact-SQL) - SQL Server - Microsoft Docs
-
Insert (XML DML) - SQL Server - Microsoft Docs
-
Add New Items To A Project - SQL Server Management Studio (SSMS)
-
Methods To Insert Data Into SQL Server
-
SQL Server: INSERT Statement - TechOnTheNet
-
SQL INSERT INTO
-
Insert Data Into Tables In SQL Server - TutorialsTeacher
-
SQL - INSERT Query - Tutorialspoint
-
INSERT INTO SQL Server Command
-
MySQL 8.0 Reference Manual :: 13.2.6 INSERT Statement
-
SQL | INSERT INTO Statement - GeeksforGeeks
-
SQL INSERT INTO TABLE Statement - Devart Blog
-
Lệnh INSERT Trong SQL Server
-
SQL Loop Through List Of Integers And Insert Line Into Table For Each
-
How To Insert Values Into SQL Server Table Using Python
-
MySQL INSERT INTO Query: How To Add Row In Table (Example)