Tables are the backbone of any database system and they have the ability to store more than 30 types of data. Tables provide a systematic way of storing data to keep your database organized. The SQL insert command is an essential part of SQL and if users don’t execute it properly, it is impossible to store data in database tables.

What Is SQL Insert?

The “INSERT INTO” command is a part of the Data Manipulation Language (DML), a sublanguage of SQL that enables modification and retrieval of information from database objects. This command enables sus to insert rows into tables.

Using this command, you can insert values into all columns or selected columns of a table. This insertion can be executed in an existing table—or a table you create using the “CREATE TABLE” command.

Let’s gain insight into the syntax of the SQL insert command.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Syntax of the SQL Insert INTO Command

There are two syntaxes for the “INSERT INTO” command. The first syntax is as follows:

syntax_for_specified_columns-SQL_Insert

  • The “INSERT INTO” statement lets the database system know that you wish to insert rows into the table that  the table_name parameter specifies 
  • Specify the table columns you want to insert values into inside brackets. Use commas to separate the columns
  • The “VALUES” statement lets the database system know that the values to be inserted are being specified
  • Each column’s respective values are then specified in the same order as the columns inside brackets—using commas to separate these values

If you wish to insert values into all of the table’s columns, it is unnecessary to specify the columns in the command. You can use the following syntax for this purpose:

syntax_for_entire_table

  • Here, it is important to ensure that all specified values are in the correct order, corresponding to their respective columns in the table

The values being inserted should be data types that match what their respective columns during table creation defined. 

Let’s try to populate an entire table using these concepts.

Inserting Values Into All the Columns of a Table

  • The first thing we’ll do is create our own table using the “CREATE TABLE” command

We’ll create a table named “Employee” using the following query:

Create_table-SQL_insert.

As you can see, “EmployeeID” is the primary key, and “Name” has the NULL CONSTRAINT defined on it, so neither of these attributes can be left blank during insertion. Additionally, “EmployeeID” cannot have the same value for multiple rows.

“Name” and “City” will contain character string data types, so during insertion, the values will be enclosed in single inverted commas. The values won't be accepted otherwise. 

  • Let’s insert values into our table using the following query:

insert_into_command-SQL_Insert.

If any of the rules are not followed, the system will display an error message explaining what the problem is. For example, if we try to insert another record with the “EmployeeID” column as one, it will result in the following:

Error_primary_key

  • Let’s insert more valid values into our “Employee” table

Insert_into_command

  • To see our table, we’ll use the following query:

SELECT * FROM Employee;

This will result in the following:

Select_command

Sometimes, we don’t have the information about all of the attributes and want to insert values for only a few columns. Let’s see how this can be achieved.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Inserting Values Into Specific Columns

In our “Employee” table, EmployeeID is the primary key, and “Name” has a NULL CONSTRAINT, so we need to enter explicit values for each column’s row. The “City” and “Salary” columns can contain NULL values.

  • For example, if if you only have ID and name information for a specific employee, you can use the following query to insert these values:

Insert_into_specific_columns-SQL_Insert

When we don’t insert an exact value for a NULL CONSTRAINT attribute like “Name,” we see the following error:

error_null_constraint

As this error message demonstrates, we have to enter explicit values for every record in these columns with constraints.

  • Let’s insert additional rows in our table using the following query:

/insert_into_selected

  • To see the table, we’ll use the SELECT command.

created_table_null_values-SQL_Insert

If you don’t enter an explicit value for any of the records in a column without constraints, they are represented as NULL values, as the default value for any column without constraints is “NULL”.

Sometimes, we require the values of one table to be copied to another table. Let’s see how this can be done.

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

Populating a Table Using Another Table

You can populate a table using another table with the “INSERT INTO SELECT” command. The syntax of this command is as follows:

INSERT INTO destination_table (column_1, column_2,...column_n)

SELECT column_1, column_2,...column_n

FROM source_table

WHERE [condition];

  • The destination_table parameter specifies the table we want to insert the values in, and the source_table parameter specifies the table the values are being inserted from
  • Here, we need to ensure that we only copy data types from columns that come from the same source and destination tables
  • The columns that have a primary key or NOT NULL constraints in the source table also need to be present in the destination table and copied
  • This command contains an optional “WHERE” clause 

From the source table, you can specify the condition according to which rows should be selected in the “condition” parameter of this clause. If this is not present in the query, all of the columns’ rows are inserted into the destination table.

For example, if we want to insert values from our “Employee” table “ into a “Salary” table that has only three columns (EmployeeID, Name, and Salary), we’ll use the following query:

insert_into_select_command-SQL_insert

To view our “Salary” table, we’ll use the SELECT command as follows:

output_table_insert_into_select

This shows that all values from the Employee table’s specified columns have been inserted into the Salary table. 

You can also insert records from all columns of a table into another table. The syntax of this command is as follows:

INSERT INTO destination_table

SELECT * FROM source_table

WHERE [condition];

For example, if we duplicate our Employee table and name it “EmployeeCopy,” and this copy includes the same columns from the original, we can use the following query to insert the values of all columns from the original table to the new table:.

INSERT INTO EmployeeCopy

SELECT * FROM Employee;

To view our new table, we’ll use the SELECT command.

inserting_all_columns_output.

Now that we know the different ways to insert rows into a table, let’s see how to delete these rows.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Deleting Rows From a Table

Sometimes, we need to delete some (or all) of the rows from a table. This can be done with the help of the “DELETE” command, which is a part of the Data Manipulation Language. The syntax of this command is as follows:

DELETE  FROM table_name

WHERE [condition];

This command utilizes the “WHERE” clause. You can specify the condition according to which rows should be deleted in the “condition” parameter of this clause. If this is not present in the query, all the table’s rows will be deleted.

For example, if we want to delete all employee records with salaries less than or equal to 25000 from our “Employee” table, we’ll use the following query:

Delete_command_with_where_clause-SQL_Insert

This will result in the following table:

Delete_with_where_output

As this shows, two rows have been deleted.

If we want to delete all rows from the table, we’ll use the following query:

DELETE FROM Employee;

All rows from this table are deleted, but the table can still be used to insert new rows.

Next Steps

In conclusion, it is crucial to insert the right types of values into columns, as this is the data you’re going to be working with. Any mistakes in insertion could potentially create some errors.

Now that you know how to populate tables, the next step in your SQL journey would be to start querying data to retrieve useful information. If you liked this article and want to get certified, check out Simplilearn’s Business Analyst Master’s Program, which covers SQL in great depth.

Do you have any questions for us? Leave a comment, and we’ll have our experts in the field answer them for you.

Our Business And Leadership Courses Duration And Fees

Business And Leadership Courses typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Advanced Certification in UI UX Design

Cohort Starts: 4 Dec, 2024

5 months$ 2,500
UI UX Bootcamp

Cohort Starts: 6 Dec, 2024

5 months$ 4,500
Product Management Professional Program

Cohort Starts: 13 Dec, 2024

8 Months$ 5,000
Post Graduate Program in Business Analysis

Cohort Starts: 18 Dec, 2024

6 months$ 2,995
Business Analyst11 Months$ 1,449