⬅ Previous Topic
UPDATE Statement in SQLNext Topic ⮕
SELECT Statement in SQL⬅ Previous Topic
UPDATE Statement in SQLNext Topic ⮕
SELECT Statement in SQLSometimes, cleaning is necessary — not just in classrooms, but in databases too. Maybe a student record was added by mistake, or an old entry needs to be cleared. The DELETE
statement helps you do just that — cleanly and precisely.
DELETE FROM table_name
WHERE condition;
Without a WHERE
clause, all records in the table will be deleted. Use with caution.
Let’s say we have a table with a few student entries:
CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class VARCHAR(10),
age INT,
city VARCHAR(30)
);
INSERT INTO students VALUES
(1, 'Arjun Deshmukh', '10A', 15, 'Pune'),
(2, 'Priya Reddy', '9B', 14, 'Hyderabad'),
(3, 'Karan Mehta', '10A', 15, 'Surat'),
(4, 'Neha Iyer', '8C', 13, 'Chennai');
Let’s remove the record of student with roll number 4:
DELETE FROM students
WHERE roll_no = 4;
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 | Surat
Remove all students from class ‘9B’:
DELETE FROM students
WHERE class = '9B';
This is where beginners often make a critical mistake:
DELETE FROM students;
-- All student records are deleted from the table 😱
Always double-check your WHERE clause before running DELETE!
Let’s delete a student who is in class ‘10A’ and lives in ‘Surat’:
DELETE FROM students
WHERE class = '10A' AND city = 'Surat';
If you really want to delete everything (e.g., before reimporting fresh data), it’s better to use:
TRUNCATE TABLE students;
TRUNCATE
is faster and doesn’t log individual row deletions like DELETE
.
Preview before deleting — always:
SELECT * FROM students WHERE class = '10A';
Once you're sure, then:
DELETE FROM students WHERE class = '10A';
The DELETE
statement helps you maintain a clean and accurate database. Whether you're correcting errors, removing outdated records, or managing data flow — this command ensures your tables stay relevant and precise.
With your data cleaned and controlled, it's time to dive into SQL Constraints — the invisible rules that keep your tables valid and consistent.
DELETE FROM students WHERE roll_no = 111;
⬅ Previous Topic
UPDATE Statement in SQLNext Topic ⮕
SELECT 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.