INSERT

INSERT is a SQL command used to add data into a database table. It allows the user to specify values for all or specific columns within the table. The values are inserted into the table in the order they are provided.

Example

INSERT INTO Customers (customer_id, name, city)
VALUES (1, 'John Doe', 'New York');

Output

Query OK, 1 row affected (0.01 sec)

Explanation

The above SQL command INSERT INTO is used to insert new rows of data into an existing table. In this case, a new customer with customer_id 1, name ‘John Doe’ and city ‘New York’ is inserted into the ‘Customers’ table.

Example

INSERT INTO employees (employee_id, first_name, last_name, email)
VALUES (1, 'John', 'Doe', 'johndoe@example.com');

Output

INSERT 0 1

Explanation

In the above INSERT statement, a new row is inserted into the employees table. The values provided in the statement are ‘1’ for employee_id, ‘John’ for first_name, ‘Doe’ for last_name, and ‘johndoe@example.com’ for email. The output INSERT 0 1 confirms the statement was executed successfully, and 1 row was inserted.

Example

INSERT INTO Employee
(Id, Name, Age, Department)
VALUES
(1, 'John Doe', 32, 'Marketing');

Output

This operation doesn’t generate any textual output suitable for display.

Explanation

In the provided example, an SQL command is used to insert a new record into a table. The INSERT INTO statement specifies the table Employee and the fields: Id, Name, Age, and Department. The VALUES keyword followed by a list in parentheses (1, 'John Doe', 32, 'Marketing') specifies the data to be inserted into the corresponding columns for a new row in the table. As it’s an operation that changes the data, it does not produce any output for display, but if executed successfully, a new row with specified values would be added to the Employee table.

Example

INSERT INTO employees (employee_id, first_name, last_name, email, hire_date, job_id, salary, department_id)
VALUES (207, 'John', 'Doe', 'jdoe@test.com', TO_DATE('2022-01-01','YYYY-MM-DD'), 'IT_PROG', 60000, 60);

Output

1 row inserted.

Explanation

In the above example, a new record is inserted into the employees table. The INSERT INTO clause specifies the table, and the list of columns in parentheses indicates where the data will go. The VALUES clause lists the data to be inserted, which correspond to the listed columns. Each piece of data is separated by a comma within the parentheses.

The TO_DATE function is used to format the string ‘2022-01-01’ into a date.

The output 1 row inserted. indicates that the operation was successful and a new record was added to the table.

Example

CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name TEXT,
Age INT,
Address CHAR(50),
Salary REAL
);
INSERT INTO Employees (ID, Name, Age, Address, Salary)
VALUES (1, 'Tom', 25, '123 Street', 50000.0);

Output

Query OK, 1 row affected (0.00 sec)

Explanation

The above SQL code creates a new table ‘Employees’ with five columns including ID, Name, Age, Address, and Salary. Then, it inserts one new row of data into this table. The data consists of an ID of 1, a Name of ‘Tom’, an Age of 25, an Address of ‘123 Street’ and a Salary of 50000.0. The output shows that the operation was successful and one row was affected.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.