SQL: INSERT Statements

adam lee
2 min readMay 8, 2024

An INSERT statement in SQL is used to add new rows to a table in a database. It allows you to specify the column names and values for the rows you want to insert. Listed below is an example of a basic INSERT statement:

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

In this example, “table_name” is the name of the table you want to insert the data into, and “column1,” “column2,” and “column3” are the names of the columns in the table. “Value1,” “value2,” and “value3” are the values you want to insert into those columns.

Lets say we had a Pokemon table with the following columns:

  • id : The unique identifier of the pokemon
  • name: The name of the pokemon
  • type: The type of pokemon
  • level: An integer representation of the pokemon

Our INSERT statement would look like something below:

INSERT INTO pokemon (id, name, type, level)
VALUES (1, 'Pikachu', 'Electric', 15);

You can also use an INSERT statement to insert data into specific columns in a table, a generic example is listed below:

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

In this case, the INSERT statement would insert a new row into the table with values for “column1” and “column3,” but not “column2.”

For example if we wanted to insert specific data into a pokemon table, the SQL execution would look like this:

INSERT INTO pokemon (id, name)
VALUES (2, 'Bulbasaur');

You can also use an INSERT statement to insert data from another table into a new table. This is known as an “insert select” statement. A generic example is listed below:

INSERT INTO new_table (column1, column2, column3)
SELECT column1, column2, column3
FROM old_table;

This INSERT statement would insert all the rows from “old_table” into “new_table,” with the same column names and values.

For example, If I wanted to import pokemon from an old table into my current table, the SQL execution would look something like this:

INSERT INTO pokemon (id, name, type, level)
SELECT id, name, type, level
FROM old_pokemon;

It’s important to note that the data tyhpe in the source and target tables must match for each column. Otherwise, the query might fail or data might be incorrectly inserted. Keep in mind that unique or primary key contraints could also violate the INSERT option.

There are many other options and variations you can use with INSERT statements, such as inserting default values, inserting multiple rows at once, and inserting data from a query. By using INSERT statements, you can easily add new data to your database tables.

--

--

adam lee

I'm a martial artist and software engineer. I enjoy writing about Martial Arts, Personal Development, Technology, and Travel.