SQL

SQL - Insert

The INSERT INTO statement in SQL is used to add new rows of data to a table. Here’s a detailed tutorial on how to use the INSERT INTO statement effectively.

Basic Syntax

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

Steps to Insert Data
  1. Specify the table name: Indicate the table into which you want to insert data.
  2. List the columns: Specify the columns for which you want to insert values. If you are inserting values for all columns, you can omit the column names.
  3. Provide the values: Specify the values to be inserted in the corresponding columns.

Examples

1. Inserting Data into Specific Columns

Assume you have a table named employees with columns first_name, last_name, age, and department.


INSERT INTO employees (first_name, last_name, age, department)
VALUES ('John', 'Doe', 30, 'Engineering');

2. Inserting Data into All Columns

If you want to insert values into all columns, you can omit the column names:


INSERT INTO employees
VALUES ('Jane', 'Smith', 28, 'Marketing');

3. Inserting Multiple Rows

You can insert multiple rows in a single INSERT INTO statement by separating each set of values with a comma:


INSERT INTO employees (first_name, last_name, age, department)
VALUES 
('Alice', 'Johnson', 35, 'HR'),
('Bob', 'Brown', 40, 'Finance');