Wednesday, December 18, 2024

SQL INSERT INTO Keyword

 The INSERT INTO statement in SQL is used to insert new records into a table. Here’s the basic syntax:

Basic Syntax:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
  • table_name: The name of the table into which you want to insert data.
  • column1, column2, column3, ...: The names of the columns in the table where the data will be inserted.
  • value1, value2, value3, ...: The values to be inserted into the respective columns.

Example 1: Inserting data into a table with specified columns:

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

Example 2: Inserting data into all columns (if all columns are provided in the table):

INSERT INTO employees
VALUES (1, 'Jane', 'Smith', 25, 'HR');

Inserting Multiple Records:

You can insert multiple rows in a single INSERT INTO statement:

INSERT INTO employees (first_name, last_name, age, department)
VALUES
('Alice', 'Brown', 28, 'Marketing'),
('Bob', 'Johnson', 35, 'Finance'),
('Charlie', 'Davis', 40, 'IT');

Inserting Data from Another Table:

You can also insert data into one table from another table:

INSERT INTO new_employees (first_name, last_name, age, department)
SELECT first_name, last_name, age, department
FROM employees
WHERE department = 'Sales';

Let me know if you need more details on any of these!

No comments:

Post a Comment