SQL INSERT INTO
Example Table
We will use the following table named employees for our examples:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
Employees Table
| employee_id | employee_name | department | salary |
|---|---|---|---|
| 1 | John Doe | HR | 50000.00 |
| 2 | Jane Smith | Finance | 60000.00 |
| 3 | Emily Davis | IT | 70000.00 |
| 4 | Michael Brown | Finance | 80000.00 |
| 5 | Jessica Wilson | IT | 90000.00 |
Using SQL INSERT INTO
The SQL INSERT INTO statement is used to add new rows to a table. You can insert data into all columns or specific columns of a table.
Example 1: Insert into All Columns
To insert a new employee into all columns of the employees table, use the following query:
INSERT INTO employees (employee_id, employee_name, department, salary)
VALUES (6, 'Alice Johnson', 'Marketing', 55000.00);
Result:
| employee_id | 6 |
|---|---|
| employee_name | Alice Johnson |
| department | Marketing |
| salary | 55000.00 |
Example 2: Insert into Specific Columns
To insert a new employee into specific columns of the employees table, use the following query:
INSERT INTO employees (employee_id, employee_name)
VALUES (7, 'Robert King');
Result:
| employee_id | 7 |
|---|---|
| employee_name | Robert King |