Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
SQLite INSERT INTO SELECT
Next article icon

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_NO NAME ADDRESS PHONE Age
1 Ram Delhi 9455123451 18
2 Ramesh GURGAON 9562431543 18
3 Sujit ROHTAK 9156253131 20
4 Suresh Delhi 9156768971 18
3 Sujit ROHTAK 9156253131 20
2 Ramesh GURGAON 9562431543 18
5 HARSH – – 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
SQLite INSERT INTO SELECT
author
kartik
Improve
Article Tags :
  • Databases
  • SQL

Similar Reads

  • PL/SQL INSERT INTO
    PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension to SQL. It allows us to write complex queries and scripts that include procedural logic, control structures, and error handling. The INSERT INTO statement in PL/SQL is essential for adding new rows of data to tab
    6 min read
  • SQLite INSERT INTO SELECT
    SQLite is a lightweight and server-less relational database management system. It requires very minimal configuration which has proven to be very helpful for developers to integrate it into any applications with ease. Due to its server-less architecture, we can use SQLite in various mobile applicati
    4 min read
  • PL/SQL INSERT INTO SELECT
    In PL/SQL, the INSERT INTO SELECT statement is used to insert data into a table by selecting data from one or more tables. This is a powerful feature for populating tables with data from existing tables or views, making it useful for data migration, reporting, and backup processes. In this guide, we
    5 min read
  • MySQL INSERT INTO Statement
    In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database. Understanding how to use the INSERT INTO statement is key for managing and
    6 min read
  • SQL Query to Insert Multiple Rows
    In SQL, the INSERT statement is used to add new records to a database table. When you need to insert multiple rows in a single query, the INSERT statement becomes efficient. In this article, We will learn different methods such as using basic INSERT statements, utilizing INSERT INTO ... SELECT for b
    4 min read
  • SQL INSERT INTO Statement
    The SQL INSERT INTO statement is one of the most commonly used commands for adding new data into a table in a database. Whether you're working with customer data, products, or user details, mastering this command is crucial for efficient database management. Let’s break down how this command works,
    6 min read
  • SQL INSERT INTO SELECT Statement
    In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
    6 min read
  • PostgreSQL - INSERT
    PostgreSQL INSERT statement is one of the fundamental SQL commands used to add new rows to a specified table within a PostgreSQL database. This command allows users to insert data efficiently, whether for a single record or multiple records at once. With the PostgreSQL INSERT INTO clause, we can spe
    5 min read
  • SQL Server INSERT Multiple Rows
    SQL Server is a relational Database Management System(RDBMS). It offers various features for creating, and managing databases with its efficient tools. It can handle both small-scale and large-scale industry database applications. INSERT Statement in SQL ServerThe Insert statement is a command of th
    4 min read
  • MySQL INSERT IGNORE
    In MySQL, managing data insertion errors is crucial for maintaining data integrity and ensuring smooth database operations. The INSERT IGNORE statement provides a practical solution by allowing records to be inserted without interruption even if some rows would cause errors like duplicate key violat
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences