⬅ Previous Topic
CREATE TABLE StatementNext Topic ⮕
UPDATE Statement in SQL⬅ Previous Topic
CREATE TABLE StatementNext Topic ⮕
UPDATE Statement in SQLTables 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.
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
INSERT INTO table_name
VALUES (value1, value2, ...);
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');
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');
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
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');
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.
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
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.
Next up, we’ll explore the SELECT statement — your primary tool for fetching data and making sense of the entries you've just inserted.
INSERT INTO students (roll_no, name, class) VALUES (110, 'Lakshmi', '6A');
⬅ Previous Topic
CREATE TABLE StatementNext Topic ⮕
UPDATE Statement in SQLYou 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.