How to Insert Data into a Table in SQL?

Inserting data into a table in SQL is a fundamental operation in database management.

After creating a table and defining its structure, the next step is to populate it with data.

In this article, we will guide you through the process of inserting data into a table using SQL.

Table of Contents

  1. Introduction
  2. SQL INSERT INTO Statement
  3. Inserting Data into a Table
  4. Inserting Data into Specific Columns
  5. Inserting Multiple Rows
  6. Conclusion

Introduction

In SQL, tables serve as containers for data, and inserting data into these tables is essential for creating, updating, and managing database records.

The INSERT INTO statement is used to add data into a table, specifying the target table and the values to be inserted.

SQL INSERT INTO Statement

The basic syntax for the INSERT INTO statement is as follows:

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

Inserting Data into a Table

Let's say we have a table named "Employees" with columns for "EmployeeID," "FirstName," "LastName," and "Salary."

To insert a new employee record, you would use the following SQL statement:

INSERT INTO Employees (EmployeeID, FirstName, LastName, Salary)
VALUES (1, 'John', 'Doe', 50000.00);

In this example:

Inserting Data into Specific Columns

You can choose to insert data into specific columns, leaving others empty.

For instance, to insert a new employee without specifying the "Salary," you can do the following:

INSERT INTO Employees (EmployeeID, FirstName, LastName)
VALUES (2, 'Jane', 'Smith');

In this case, only the "EmployeeID," "FirstName," and "LastName" columns are populated, and "Salary" will be empty.

Inserting Multiple Rows

You can insert multiple rows into a table with a single INSERT INTO statement. To insert data for multiple employees, you can write:

INSERT INTO Employees (EmployeeID, FirstName, LastName, Salary)
VALUES
    (3, 'Robert', 'Johnson', 60000.00),
    (4, 'Mary', 'Brown', 55000.00),
    (5, 'David', 'Lee', 62000.00);

In this example, we insert data for three employees in a single statement.

Conclusion

Inserting data into a table is a fundamental operation in SQL. Whether you're adding new records to a table or updating existing ones, the INSERT INTO statement is a vital tool for managing the data within your database.

By following the syntax and providing the appropriate values, you can efficiently populate your tables with relevant data, making your database a valuable resource for applications and decision-making.