Open In App

SQL | INSERT INTO Query

Last Updated : 18 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL, managing data effectively involves the ability to insert new rows into a table. TheINSERT INTO statement is used to add data to a database table. This statement provides two methods for inserting rows: inserting only values and inserting values with column names. Each method has its own syntax and practical use cases.

In this article, we will explain both methods in detail, provide examples, and show how the output table looks after each insertion.

1. Inserting Rows with Values Only

The first method of using the INSERT INTO statement is to specify only the values of the data to be inserted without mentioning the column names. This method is straightforward but lacks flexibility in specifying which columns receive the values. Inserting values this way is straightforward but does not allow us to specify which columns receive the values, making it ideal for quick and simple data insertion. The syntax is as follows:

Syntax:

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


Key Terms

  • table_name: name of the table.
  • value1, value2,.. : value of first column, second column,... for the new record

Example of Inserting Rows with Values Only

The Student table is designed to store information about students enrolled in a school or university. It contains five columns: ROLL_NO, NAME, ADDRESS, PHONE, and Age. Each row in the table represents a unique student with easy access to each student's details and facilitates operations such as querying, updating, and deleting records.

table1

To add a new student named HARSHfrom WEST BENGALwith a PHONE number 8759770477 and AGE19, we would use the following SQL query:

Query:

INSERT INTO Student VALUES ('5','HARSH','WEST BENGAL','8759770477','19');

Output

Insert-into-example1
INSERT INTO Example 1

2. Inserting Rows with Specified Columns and Values

The second method of using theINSERT INTO statement involves specifying both the columns that need data and their corresponding values. This approach provides more control over which columns receive the data and is useful for inserting into partial tables where not all columns are included. The syntax is as follows:

Syntax:

INSERT INTO table_name (column1, column2, column3,..)
VALUES ( value1, value2, value3,..);
table_name: name of the table.

Key Terms

  • table_name: The name of the table where the new row will be inserted.
  • column1, column2, ...: The columns in the table that we want to fill with data.
  • value1, value2, value3, ...: The values corresponding to the specified columns in the order they appear in the table definition.

Example of Inserting Rows with Specified Columns and Values

Continuing with the Student table example, if we want to insert a new student with only the ROLL_NO, NAME, and AGE, and leave out ADDRESS and PHONE, we would use the following SQL query:

Query:

INSERT INTO Student (ROLL_NO, NAME, Age) VALUES ('5','HARSH','19');

Output

ROLL_NONAMEADDRESSPHONEAge
1RamDelhi945512345118
2RameshGURGAON956243154318
3SujitROHTAK915625313120
4SureshDelhi915676897118
3SujitROHTAK915625313120
2RameshGURGAON956243154318
5HARSH--19

Conclusion

Using the INSERT INTO statement effectively is essential for managing and manipulating data in SQL. Whether we are adding new rows with only values or specifying columns along with values, understanding these methods allows us to work efficiently with SQL tables. By using these methods, we can maintain data integrity, avoid errors, and keep your database organized. This article provides a clear, structured introduction to using theINSERT INTO statement, detailed explanations of both methods, practical examples with outputs, and a conclusion that reinforces the importance of these methods in SQL


Next Article
Article Tags :

Similar Reads