Insert into - SQL

In SQL, the INSERT INTO statement is used to add new records (rows) into a table. When using INSERT INTO, you specify the table name followed by the column names (optional) and the values to be inserted.

SQL
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
  • table_name : The name of the table where you want to insert the data.
  • (column1, column2, ...) Optional. Specifies the columns into which you want to insert data. If omitted, values will be inserted into all columns in the order they are defined in the table.
  • VALUES Keyword indicating the beginning of the list of values to be inserted.
  • (value1, value2, ...) The actual values to be inserted into the specified columns. The number of values provided must match the number of columns specified (or the number of columns in the table if column names are not provided).

Example without specifying column names

Example
INSERT INTO employees
VALUES (1, 'John', 'Doe', '2024-02-06');

This SQL statement inserts a new record into the employees table with values for all columns in the order they are defined in the table.

Example with specifying column names:

Example
INSERT INTO employees (employee_id, first_name, last_name, hire_date)
VALUES (1, 'John', 'Doe', '2024-02-06');

This SQL statement inserts a new record into the employees table, explicitly specifying the columns into which values should be inserted.

It's important to ensure that the data being inserted conforms to any constraints defined on the table, such as data types, nullability, and any unique or foreign key constraints.