SQL Insert

SQL Insert

SQL INSERT statement is a SQL query. It is used to insert a single or multiple records in a table.

There are two ways to insert data in a table:

1. Specify both the column names and the values to be inserted:

2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the “INSERT INTO” syntax would be as follows:

Example

Course Video

NOTE: Practice below practice questions on MSSQL SERVER, it will not execute on site editor.

1. Write A Query to Create Student TABLE with columns StudentID, FirstName, LastName, Age, City AND INSERT 3 RECORDS IN THAT TABLE BY USING COLUMNS.

2. Write SQL Query To insert 2 records into the first two columns of Student table.

3. WRITE QUERY TO INSERT 5 RECORDS IN THAT TABLE WITHOUT MENTIONING COLUMNS.

4. Write a SQL Query To insert Multiple Records (5 records) into a table with a single SQL statement.

5. Create table Workspace with 3 columns Workspace_Id, Workspace_Name, Workspace_Number and insert 10 records in the table.

6. Compose a SQL statement to insert a new record into the Students table, including details for a student with StudentID as 1, FirstName ‘John’, and LastName ‘Doe’.

Output 

7. Construct a SQL query to insert multiple student records into the Students table, each featuring unique StudentID, FirstName, and LastName combinations. Specifically, the query should include entries for students with

StudentID 2, FirstName ‘Jane’, and LastName ‘Smith’;

StudentID 3, FirstName ‘Mike’, and LastName ‘Johnson’;

as well as StudentID 4, FirstName ‘Emily’, and LastName ‘Davis’.

8. Compose a SQL statement to add a new student record to the Students table, providing values only for the FirstName and LastName columns, with ‘Alice’ and ‘Brown’ as the respective values.

9. Write a SQL query to insert 3 additional records into the students table without specifying column names.

10. Create a table to store information about various pieces of jewelry.
Design a table named Jewelry with the following columns:

1. Design a table named Jewelry with the following columns:

JewelryID (integer), JewelryName (string), MetalType (string), Gemstone (string), Price (decimal).

2. After creating the Jewelry table, insert 5 records into it with the following details:

  • For each record, generate a unique JewelryID.
  • Populate JewelryName with names like ‘Diamond Ring’, ‘Gold Necklace’, ‘Sapphire Earrings’, etc.
  • Populate MetalType with types like ‘Gold’, ‘Silver’, ‘Platinum’, etc.
  • Populate Gemstone with gemstones like ‘Diamond’, ‘Sapphire’, ‘Emerald’, etc.
  • Populate Price with prices ranging from 100 to 1000.
Output 
Frequently Asked Questions

Still have a question?

Let's talk

The SQL INSERT statement is used to add new rows of data to a table. It’s one of the most common operations in database management.

The basic syntax is:

sql

Copy code

INSERT INTO table_name (column1, column2, …) VALUES (value1, value2, …);

For example:

sql

Copy code

INSERT INTO Employees (name, department) VALUES (‘John Doe’, ‘HR’);

You can add multiple rows by listing multiple sets of values:

sql

Copy code

INSERT INTO table_name (column1, column2) VALUES  

(‘Value1’, ‘Value2’), 

(‘Value3’, ‘Value4’), 

(‘Value5’, ‘Value6’);

You can add multiple rows by listing multiple sets of values:

sql

Copy code

INSERT INTO table_name (column1, column2) VALUES  

(‘Value1’, ‘Value2’), 

(‘Value3’, ‘Value4’), 

(‘Value5’, ‘Value6’);

You can add multiple rows by listing multiple sets of values:

sql

Copy code

INSERT INTO table_name (column1, column2) VALUES  

(‘Value1’, ‘Value2’), 

(‘Value3’, ‘Value4’), 

(‘Value5’, ‘Value6’);

  • Use column names explicitly to avoid errors if the table structure changes.
  • Validate data before insertion to maintain data integrity.
  • Use transactions for bulk inserts to ensure atomicity.

An INSERT INTO SELECT statement inserts data from another table:
sql
Copy code
INSERT INTO table_name (column1, column2)
SELECT column1, column2 FROM another_table WHERE condition;

  • Use batch inserts to reduce the number of queries.
  • Disable indexes during bulk inserts and re-enable them afterward.
  • Avoid using triggers during bulk data loading for better speed.

Yes, you can insert NULL values into a column if it is allowed (i.e., not defined as NOT NULL).

If columns have default values, they will use those defaults. Otherwise, the query will throw an error if a column is defined as NOT NULL.

Yes, our website offers a Free SQL INSERT Tutorial for beginners and advanced users.

  • Mismatched column and value counts.
  • Inserting data into non-existent columns.

Violating unique or primary key constraints.