MySQL INSERT INTO Statement (original) (raw)

Last Updated : 23 Jul, 2025

In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database.

Understanding how to use the **INSERT INTO statement is key for managing and ensuring the integrity and functionality of the database.

INSERT INTO Statement

The INSERT INTO statement in MySQL is a Data Manipulation Language (**DML**)** command that allows users to add new records (rows) into a specified table. It follows a concise syntax to specify the table name and the values to be inserted into the respective columns.

Let's look at **Various ways of using the INSERT INTO statement, exploring how it can be used to add data efficiently to our table.

  1. **Insert record in a single row
  2. **Insert record in multiple rows

**Syntax:

There are two ways to write an INSERT INTO statement in MySQL.

1. The general syntax for inserting a single record into a MySQL table using the SQL INSERT INTO command is as follows:

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

2. Use the following statement to insert multiple records with a single command:

INSERT INTO table_name (column1, column2, ..., columnN)
VALUES
(value1_1, value1_2, ..., value1_N),
(value2_1, value2_2, ..., value2_N),
...,
(valueM_1, valueM_2, ..., valueM_N);

Demo MySQL Database

Write the following command to create an empty table on which we will perform the INSERT INTO.

CREATE TABLE employees (
name VARCHAR(50),
age INT,
department VARCHAR(50)
);

INSERT Statement Examples

Let's look at the examples of the INSERT INTO statement, to understand the workings of INSERT INTO statement, and cover its different use cases.

**Example 1: MySQL INSERT INTO Table Single Row

To insert a single record into a table, specify the table name and provide values for each column in the order they appear in the table.

**Syntax:

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

**Query:

-- Insert single record into the employees table
INSERT INTO employees (name, age, department)
VALUES ('John Doe', 30, 'IT');

-- Select all records from the employees table
select * from employees;

**Output:

name age department
John Doe 30 IT

**Explanation: This syntax inserts a single row of data into the '**employees' table. You specify the **table name and the columns into which you want to **insert data, followed by the corresponding values. In this example, we insert the name '**John Doe', age '**30', and department 'IT' into the respective columns.

Example 2: MySQL INSERT INTO table Multiple Rows

MySQL allows **inserting multiple rows of data in a **single query. It is reducing the overhead of executing **multiple queries. The syntax is similar to inserting a single row but with multiple sets of values enclosed within parentheses and separated by commas

**Syntax:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES
(value1_1, value1_2, value1_3, ...),
(value2_1, value2_2, value2_3, ...),
(value3_1, value3_2, value3_3, ...);

**Query:

-- Insert multiple records into the employees table
INSERT INTO employees (name, age, department)
VALUES
('Jane Smith', 28, 'HR'),
('Michael Johnson', 35, 'Finance'),
('Emily Brown', 32, 'Marketing');

-- Select all records from the employees table
SELECT * FROM employees;

**Output:

name age department
Jane Smith 28 HR
Michael Johnson 35 Finance
Emily Brown 32 Marketing

**Explanation: This syntax allows you to insert multiple rows of data into the '**employees' table in a **single query. Each set of values represents a separate row to be inserted. In this example, we insert three new employees into the table with their respective details.

**Example 3: MySQL INSERT INTO SELECT

Sometimes, We may need to **insert data into a table based on the results of a **SELECT query. MySQL facilitates this through the INSERT INTO SELECT statement. This statement selects data from one table and inserts it into another

**Syntax:

INSERT INTO table_name (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM another_table
WHERE condition;

**Query:

-- Create the employees table
CREATE TABLE employees (
name VARCHAR(50),
age INT,
department VARCHAR(50)
);

-- Insert multiple records into the employees table
INSERT INTO employees (name, age, department)
VALUES
('Jane Smith', 28, 'HR'),
('Michael Johnson', 35, 'Finance'),
('Emily Brown', 32, 'Marketing');

-- Create the employees_backup table
CREATE TABLE employees_backup (
name VARCHAR(50),
age INT,
department VARCHAR(50)
);

-- Insert records into the employees_backup table where age > 30
INSERT INTO employees_backup (name, age, department)
SELECT name, age, department
FROM employees
WHERE age > 30;

-- Select all records from the employees table
SELECT * FROM employees_backup;

**Output:

name age department
Michael Johnson 35 Finance
Emily Brown 32 Marketing

**Explanation: This syntax inserts data into the '**employees_backup' table based on the results of a **SELECT query. In this example, we select employees from the 'employees' table where the age is greater than 30 and insert their details into the 'employees_backup' table.

Example 4: MySQL INSERT INTO DATE

Inserting **date values into a MySQL database is a common requirement. MySQL provides several date and time functions to handle date-related operations efficiently. To **insert the current date into a date column, we can use the NOW() function with MySQL Insert Date.

**Syntax:

INSERT INTO table_name (date_column)
VALUES (NOW());

**Query:

INSERT INTO orders (order_date)
VALUES (NOW());

**Output:

2024-02-20 12:00:00

**Explanation: To insert the current date into a date column, you can use the **NOW() function within the **INSERT INTO statement. This example inserts the current date into the 'order_date' column of the 'orders' table.

Conclusion

In conclusion, **INNER JOIN in MySQL is essential for combining rows from multiple tables based on a common column, ensuring only matching rows are included in the results. This article has explained the basic syntax and usage, showing examples with **GROUP BY, **HAVING, **WHERE, and **USING clauses. Mastering **INNER JOIN allows you to perform complex queries and manage database information efficiently, making it a crucial skill for effective database management.