SQL Tutorial

SQL INSERT STATEMENT

SQL INSERT STATEMENT
SQL INSERT STATEMENT

SQL INSERT STATEMENT

The SQL INSERT statement is used to add data to a table. It allows users to insert either a single record or multiple records into a table.

SQL INSERT STATEMENT

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Ways to Insert Data into a Table:

  1. Using SQL INSERT INTO statement
    • By specifying column names
    • Without specifying column names
  2. Using SQL INSERT INTO SELECT statement

1) Inserting Data Directly into a Table

The INSERT INTO statement is used to add records to a table. There are two methods for inserting values:

Method 1: Without Specifying Column Names

In this method, column names are not specified. Only the values to be inserted are provided.

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

Method 2: With Specified Column Names

In this method, the column names and their corresponding values are specified.

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

Example:

Consider a table STUDENTS and insert five records into it.

INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (1, 'RAHUL', 23, 'MUMBAI');

INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (2, 'PRIYA', 21, 'BANGALORE');

INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (3, 'KARAN', 22, 'HYDERABAD');

INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (4, 'SIMRAN', 20, 'PUNE');

INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (5, 'ARJUN', 24, 'CHENNAI');

Final Table Output:

ROLL_NONAMEAGECITY
1RAHUL23MUMBAI
2PRIYA21BANGALORE
3KARAN22HYDERABAD
4SIMRAN20PUNE
5ARJUN24CHENNAI

Another way to add a record to the CUSTOMERS table:

INSERT INTO CUSTOMERS
VALUES (6, 'ANITA', 25, 'KOCHI');

Updated Table Output:

ROLL_NONAMEAGECITY
1RAHUL23MUMBAI
2PRIYA21BANGALORE
3KARAN22HYDERABAD
4SIMRAN20PUNE
5ARJUN24CHENNAI
6ANITA25KOCHI

2) Inserting Data Through SELECT Statement

The INSERT INTO SELECT statement is used to copy data from one table to another.

SQL Syntax:

INSERT INTO table_name
[(column1, column2, .... column)]
SELECT column1, column2, .... Column N
FROM another_table [WHERE condition];

Important Notes:

  • Ensure that the data types of the columns match the values being inserted.
  • Follow any integrity constraints, such as unique and not null constraints, defined for the table.

Example:

Suppose we have an OLD_STUDENTS table containing existing student records and want to copy some of them into NEW_STUDENTS.

INSERT INTO NEW_STUDENTS (ROLL_NO, NAME, AGE, CITY)
SELECT ROLL_NO, NAME, AGE, CITY
FROM OLD_STUDENTS
WHERE AGE > 22;

This query will insert only students older than 22 from OLD_STUDENTS into NEW_STUDENTS.

Download New Real Time Projects :- Click here

Conclusion

The SQL INSERT statement is an essential command for adding records to a table. Whether inserting single or multiple records, using INSERT INTO with or without column names, or copying data with INSERT INTO SELECT, these methods are useful for handling database operations.

Keywords: sql insert statement with multiple values, sql insert statement postgresql, sql insert statement examples, sql insert statement from select, sql insert statement generator, sql insert statement from excel, sql insert statement with datetime, sql insert statement with auto increment field, sql insert statement multiple rows, sql insert statement example, insert into table sql multiple rows, insert into table sql from another table

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us