SQL Tutorial



INSERT INTO QUERY


INSERT INTO Statement in SQL

The INSERT INTO statement is used to add new rows of data into an existing table. It is one of the most fundamental commands in SQL, and is part of Data Manipulation Language (DML).

πŸ“ Basic Syntax

Here's the basic syntax of the INSERT INTO statement:

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

Explanation: This syntax is used when you want to insert data into specific columns of a table.

πŸ’‘ Example 1: Insert a Single Record

Let's assume you have a table called Employees with columns EmployeeID, EmployeeName, and Department.

-- Inserting a new employee record
INSERT INTO Employees (EmployeeID, EmployeeName, Department)
VALUES (1, 'John Doe', 'HR');
  

This statement inserts one record with the values (1, 'John Doe', 'HR') into the Employees table.

πŸ’‘ Example 2: Insert Multiple Records

You can also insert multiple rows of data into the table at once. Here's an example:

-- Inserting multiple employee records
INSERT INTO Employees (EmployeeID, EmployeeName, Department)
VALUES 
  (2, 'Jane Smith', 'Finance'),
  (3, 'Emily Davis', 'IT'),
  (4, 'Michael Brown', 'Marketing');
  

This will insert three records into the Employees table in a single statement.

πŸš€ Try It Out!

Let’s simulate an interactive example where you can experiment with inserting data into a table.

Imagine you are managing a Books table. The columns are:

  • BookID: Unique identifier for the book
  • BookTitle: Title of the book
  • Author: Author of the book
  • Price: Price of the book

Now, your task is to insert a new book record with the following details:

  • BookID: 101
  • BookTitle: "The Great Adventure"
  • Author: "John Green"
  • Price: 19.99

Write the INSERT INTO statement below:

πŸ’‘ What to Remember:

  • Order of columns: Make sure the order of columns in the INSERT INTO statement matches the order of values in the VALUES clause.
  • Multiple records: You can insert multiple records in a single INSERT INTO statement by separating each set of values with commas.
  • Null Values: If a column allows NULL values, you can omit the value in the VALUES clause.

Conclusion

The INSERT INTO statement is essential for adding new data to your database. You can insert single or multiple records with a simple syntax. Make sure to follow the correct column order and data types when inserting records!

Quick Tip:

Use the VALUES clause with caution. Always ensure that the number of values matches the number of columns you're inserting into!


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review