MySQL INSERT INTO Statement

The MySQL INSERT INTO statement adds a new record (row) to a database table. If you want to store data in a MySQL table, you must use the INSERT INTO statement. For example, if you have a students table and want to add a new student’s information, you can use the INSERT INTO command.

The INSERT INTO command is one of the most commonly used SQL commands. This command allows you to add new data such as student records, employee details, customer information, and product data to a database table.

Syntax of MySQL INSERT INTO Statement


There are two ways to write the INSERT INTO statement in MySQL:

Syntax 1: INSERT INTO with Column Names

This syntax allows you to specify the names of the columns into which you want to insert data.

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

In this syntax:

  • table_name is the name of the table where you want to insert data.
  • column1, column2, column3, … are the columns that will receive the values.
  • VALUES contains the data to be inserted.
  • Each value corresponds to the specified column in the same order.

Example 1:

Suppose you have a students table:

INSERT INTO students (id, name, age, city)
VALUES (101, 'John', 20, 'New York');

The values are inserted into the corresponding columns as shown below:

Column NameValue Inserted
id101
nameJohn
age20
cityNew York

Resulting Table

idnameagecity
101John20New York

How MySQL Maps the Values

Specified ColumnCorresponding Value
id101
name‘John’
age20
city‘New York’

Since the columns and values are listed in the same order, MySQL inserts:

  • 101 into the id column.
  • ‘John’ into the name column.
  • 20 into the age column.
  • ‘New York’ into the city column.

As a result, MySQL adds one new row to the students table.

Syntax 2: INSERT INTO Without Column Names

If you provide values for all columns in the table, you can omit the column names.

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

In this syntax:

  • MySQL inserts values according to the order of columns defined in the table.
  • You must provide a value for every column.
  • The order of values must exactly match the order of the columns.

Example 2: INSERT INTO Without Specifying Column Names

INSERT INTO students
VALUES (102, 'David', 22, 'Chicago');

In this example, we have not specified the column names. Therefore, MySQL inserts the values according to the order of columns defined in the students table.

How MySQL Maps the Values

Table ColumnValue Inserted
id102
nameDavid
age22
cityChicago

MySQL inserts the values in the following order:

  • 102 is inserted into the id column.
  • ‘David’ is inserted into the name column.
  • 22 is inserted into the age column.
  • ‘Chicago’ is inserted into the city column.

Since the values are provided in the same order as the table columns, the record is inserted successfully.

Resulting Table

idnameagecity
102David22Chicago

Note that when you omit column names, you must provide values for all columns and follow the exact order of columns in the table. However, you should follow syntax 1 because of the following reasons:

  • Easy to read and understand.
  • Reduces errors when the table structure changes.
  • Allows you to insert data into selected columns only.

Example 3: Insert Values into Specific Columns

The INSERT INTO statement in MySQL also allows you to specify only the columns for which you want to insert values. You do not need to insert values into every column of a table.

Any columns that are omitted automatically receive their default value (if one exists) or NULL if the column allows NULL values. This feature is useful when you do not have data for every column or when some columns are optional. Look at the below example query.

INSERT INTO students(id, name)
VALUES(104, 'Michael');

How MySQL Maps the Values

Specified ColumnValue Inserted
id104
nameMichael

Resulting Table

idnameagecity
104MichaelNULLNULL

In this example:

  • The id column stores the value 104.
  • The name column stores the string value ‘Michael’.
  • Since we have not provided values for the age and city columns, MySQL stores NULL for both columns.
  • Here, NULL means no value or unknown value.

How to Insert Multiple Rows at Once?


MySQL allows you to insert multiple records into a table using a single INSERT INTO statement. Instead of executing several separate queries, you can insert many rows at the same time. The following SQL query inserts three new records in the “students” table:

INSERT INTO students(id, name, age, city)
VALUES
(105, 'Emma', 19, 'Dallas'),
(106, 'Sophia', 20, 'Houston'),
(107, 'James', 21, 'Seattle');

In the above query, each set of values is enclosed in parentheses and separated by commas. You must end the statement with a semicolon (;).

How MySQL Maps the Values

First Row:

ColumnValue
id105
nameEmma
age19
cityDallas

Second Row:

ColumnValue
id106
nameSophia
age20
cityHouston

Third Row:

ColumnValue
id107
nameJames
age21
citySeattle

Resulting Table

idnameagecity
105Emma19Dallas
106Sophia20Houston
107James21Seattle

In this example:

  • The first set of values (105, ‘Emma’, 19, ‘Dallas’) creates the first row.
  • The second set of values (106, ‘Sophia’, 20, ‘Houston’) creates the second row.
  • The third set of values (107, ‘James’, 21, ‘Seattle’) creates the third row.

MySQL processes all three sets of values and inserts three records into the students table with a single query. This is the most efficient way to insert multiple records into a MySQL table using a single query.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.