
SQL INSERT STATEMENT
SQL INSERT STATEMENT
The SQL INSERT statement is a powerful command in SQL that allows users to add data into a table. This statement enables inserting either a single record or multiple records simultaneously.
Ways to Insert Data into a Table:
- Using SQL INSERT INTO statement
- By specifying column names
- Without specifying column names
- Using SQL INSERT INTO SELECT statement
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here
1) Inserting Data Directly into a Table
The INSERT INTO statement is used to insert records into a table. There are two methods to insert values:
Method 1: Without Specifying Column Names
In this method, column names do not need to be specified; only their values are required.
INSERT INTO table_name
VALUES (value1, value2, value3....);
Method 2: With Specified Column Names
In this method, both the column names and the corresponding values must be mentioned.
INSERT INTO table_name (column1, column2, column3....)
VALUES (value1, value2, value3.....);
Example:
Let’s 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_NO | NAME | AGE | CITY |
---|---|---|---|
1 | RAHUL | 23 | MUMBAI |
2 | PRIYA | 21 | BANGALORE |
3 | KARAN | 22 | HYDERABAD |
4 | SIMRAN | 20 | PUNE |
5 | ARJUN | 24 | CHENNAI |
Another way to add records to a table CUSTOMERS:
INSERT INTO CUSTOMERS
VALUES (6, 'ANITA', 25, 'KOCHI');
Updated Table Output:
ROLL_NO | NAME | AGE | CITY |
---|---|---|---|
1 | RAHUL | 23 | MUMBAI |
2 | PRIYA | 21 | BANGALORE |
3 | KARAN | 22 | HYDERABAD |
4 | SIMRAN | 20 | PUNE |
5 | ARJUN | 24 | CHENNAI |
6 | ANITA | 25 | KOCHI |
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 the data types of the columns match the values being inserted.
- Follow any integrity constraints (e.g., unique, not null) defined for the table.
Example:
Suppose we have an OLD_STUDENTS table with existing student records, and we 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
Complete Advance AI topics:- CLICK HERE
Conclusion
The SQL INSERT statement is an essential tool for adding records to a table efficiently. Whether inserting single or multiple records, using INSERT INTO with or without column names, or copying data with INSERT INTO SELECT, mastering these techniques helps in handling database operations effectively.
For more SQL tutorials, visit UpdateGadh.
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
Post Comment