SQL INSERT Statement

What is SQL INSERT?

The INSERT statement in SQL is used to add new records (or rows) into a database table. You can insert values into all columns of the table, or just into specific columns.

Syntax of INSERT

Here is the basic syntax for using the INSERT statement:

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

Where:

  • table_name is the name of the table where the data will be inserted.
  • column1, column2, column3, ... are the names of the columns in the table where the values will be inserted.
  • value1, value2, value3, ... are the values to be inserted into the specified columns.

Example of SQL INSERT

Consider a table named Students with the following columns: ID, Name, and Age. Here's how you would insert a new record into this table:

INSERT INTO Students (ID, Name, Age)
VALUES (1, 'Shaikh Arshiya Naaz', 22);

This command inserts a new row into the Students table where the ID is 1, the Name is 'John Doe', and the Age is 22.

Inserting Multiple Rows

You can insert multiple rows at once by providing multiple sets of values, separated by commas:

INSERT INTO Students (ID, Name, Age)
VALUES 
(2, 'Syeda Anam Fatema ', 23),
(3, 'Muntajeeb Ahmed Moosa', 21);

This statement inserts two rows into the Students table.

Inserting Data into Selected Columns

If you only want to insert values into specific columns of a table, you can omit the others. For example:

INSERT INTO Students (Name, Age)
VALUES ('Syed Mohammad Maaz', 21);

In this case, the ID column would either use its default value or auto-increment (if defined as such in the table schema).

SQL INSERT in Popular Databases

SQL INSERT is used across most relational database systems, including:

  • MySQL
  • PostgreSQL
  • Microsoft SQL Server
  • Oracle Database
  • SQLite