SQL INSERT INTO statement: insert data

The INSERT INTO statement in SQL is used to add new rows of data into a table in the database.

There are two main ways of using the INSERT INTO statement:

  • Inserting data into all columns of the table

In this case, you do not need to specify the column names where the data will be inserted. Instead, you just provide the values in the same order as the columns appear in the table.

INSERT INTO table_name 
VALUES (value1, value2, value3, ...);
  • Inserting data into specific columns

Here, you specify both the column names and the corresponding values.

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

Example:

Consider a Customers table:

CustomerIDNameContactNumber
1John1234567890
2Jane2345678901

If you want to insert a new row into the Customers table, you would use the INSERT INTO statement as follows:

INSERT INTO Customers (CustomerID, Name, ContactNumber) 
VALUES (3, 'Alice', '3456789012');

After the execution of the above SQL statement, the Customers table would look like this:

CustomerIDNameContactNumber
1John1234567890
2Jane2345678901
3Alice3456789012

This query inserts a new row into the Customers table.

Note:

  • Ensure that the order of the values corresponds with the order of the columns specified.
  • The data types of the values must match the data types of the columns. For example, strings must be enclosed in single quotes (' ').
  • Not all databases support the INSERT INTO statement with the same syntax, so it's always a good idea to check the specific SQL dialect documentation.

As always, the exact syntax may vary between different SQL dialects, so be sure to consult the documentation for the SQL dialect you're using.

  1. Inserting Data into a Table in SQL:

    • Description: Inserts new records or rows into a table.
    • Code Example:
      INSERT INTO TableName (Column1, Column2)
      VALUES (Value1, Value2);
      
  2. Using VALUES Clause in INSERT INTO:

    • Description: VALUES clause specifies the values to be inserted into each column.
    • Code Example:
      INSERT INTO Students (StudentID, StudentName, Age)
      VALUES (1, 'John Doe', 25);
      
  3. SQL INSERT INTO with SELECT Statement:

    • Description: Populates a table with data from the result set of a SELECT query.
    • Code Example:
      INSERT INTO NewTable (Column1, Column2)
      SELECT OldColumn1, OldColumn2
      FROM ExistingTable
      WHERE Condition;
      
  4. Specifying Column Names in INSERT INTO:

    • Description: Explicitly names the columns to which data is inserted, ensuring correct mapping.
    • Code Example:
      INSERT INTO TableName (Column1, Column2)
      VALUES (Value1, Value2);
      
  5. Inserting Multiple Rows in a Single Statement:

    • Description: Inserts multiple rows with a single INSERT INTO statement.
    • Code Example:
      INSERT INTO TableName (Column1, Column2)
      VALUES (Value1, Value2),
             (Value3, Value4),
             (Value5, Value6);
      
  6. Inserting Data with DEFAULT Values in SQL:

    • Description: Allows inserting default values into columns if they are not explicitly provided.
    • Code Example:
      INSERT INTO TableName (Column1, Column2, Column3)
      VALUES (Value1, DEFAULT, Value3);
      
  7. Handling NULL Values in INSERT INTO:

    • Description: NULL values can be inserted into columns by specifying NULL or omitting the column in the VALUES clause.
    • Code Example:
      INSERT INTO TableName (Column1, Column2, Column3)
      VALUES (Value1, NULL, Value3);
      
  8. SQL INSERT INTO Example:

    • Example:
      INSERT INTO Employees (EmployeeID, FirstName, LastName, Salary)
      VALUES (1, 'Alice', 'Smith', 50000),
             (2, 'Bob', 'Johnson', 60000),
             (3, 'Charlie', 'Davis', 55000);