Yandex

INSERT INTO Statement
Adding Data to SQL Tables



Introduction

Tables are like empty registers — they need entries to become useful. That’s where the INSERT INTO statement steps in. It lets you add rows of meaningful data into your SQL tables — student records, teacher assignments, exam scores — all start here.

Syntax of INSERT INTO

Standard Syntax

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

Short Syntax (Only when inserting all columns in order)

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

1. Basic INSERT Example

Assume we already created a students table:

CREATE TABLE students (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  class VARCHAR(10),
  age INT,
  city VARCHAR(30)
);

Now let’s insert data for a student named Arjun:

INSERT INTO students (roll_no, name, class, age, city)
VALUES (1, 'Arjun Deshmukh', '10A', 15, 'Pune');

2. Inserting Multiple Records

Rather than inserting one record at a time, you can insert many in a single statement:

INSERT INTO students (roll_no, name, class, age, city)
VALUES
(2, 'Priya Reddy', '9B', 14, 'Hyderabad'),
(3, 'Karan Mehta', '10A', 15, 'Ahmedabad'),
(4, 'Neha Iyer', '8C', 13, 'Chennai');

3. Viewing Inserted Data

SELECT * FROM students;
roll_no | name            | class | age | city
--------+-----------------+-------+-----+-----------
1       | Arjun Deshmukh  | 10A   | 15  | Pune
2       | Priya Reddy     | 9B    | 14  | Hyderabad
3       | Karan Mehta     | 10A   | 15  | Ahmedabad
4       | Neha Iyer       | 8C    | 13  | Chennai

4. Inserting Only Specific Columns

If you don't have data for all columns, you can skip them — but you must exclude them in the column list as well:

INSERT INTO students (roll_no, name, class)
VALUES (5, 'Devanshi Shah', '9A');

5. What If You Miss Required Columns?

Let’s try inserting without specifying roll_no (which is PRIMARY KEY):

INSERT INTO students (name, class, age, city)
VALUES ('Ishaan Pandey', '10B', 15, 'Lucknow');
ERROR: Column 'roll_no' cannot be null

This teaches an important lesson — columns marked NOT NULL or PRIMARY KEY must always be provided.

6. INSERT with DEFAULT Values

Suppose we create a student_profile table with a default city:

CREATE TABLE student_profile (
  roll_no INT PRIMARY KEY,
  name VARCHAR(50),
  city VARCHAR(30) DEFAULT 'Delhi'
);

Now insert without specifying the city:

INSERT INTO student_profile (roll_no, name)
VALUES (1, 'Mehul Arora');
roll_no | name         | city
--------+--------------+-------
1       | Mehul Arora  | Delhi

Best Practices for INSERT INTO

  • Always specify column names for clarity and future safety.
  • Validate data types — inserting text into a number column will cause errors.
  • Insert in batches if you have many records.

Summary

The INSERT INTO statement breathes life into your tables. It’s how raw data makes its way into a structured format. Whether it’s a new student enrolment or exam result, everything starts here.

What’s Next?

Next up, we’ll explore the SELECT statement — your primary tool for fetching data and making sense of the entries you've just inserted.

QUIZ

Question 1:Which of the following statements correctly inserts a new student into the `students` table?

Question 2:The INSERT INTO statement can only be used if you specify values for all columns in order.

Question 3:Which of the following are valid uses of the INSERT INTO statement?

Question 4:What does this SQL command do?
INSERT INTO students (roll_no, name, class) VALUES (110, 'Lakshmi', '6A');

Question 5:You can insert multiple rows using a single INSERT INTO statement.

Question 6:Which of the following mistakes can lead to errors when using INSERT INTO?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M