Yandex

DELETE Statement
Removing Records from SQL Tables



Introduction

Sometimes, 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.

Syntax of DELETE Statement

DELETE FROM table_name
WHERE condition;

Important:

Without a WHERE clause, all records in the table will be deleted. Use with caution.

1. Sample Table – Students

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');

2. Deleting a Specific Record

Let’s remove the record of student with roll number 4:

DELETE FROM students
WHERE roll_no = 4;

3. View the Remaining 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  | Surat

4. Deleting Based on a Condition

Remove all students from class ‘9B’:

DELETE FROM students
WHERE class = '9B';

5. What Happens If You Skip WHERE?

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!

6. DELETE with AND Conditions

Let’s delete a student who is in class ‘10A’ and lives in ‘Surat’:

DELETE FROM students
WHERE class = '10A' AND city = 'Surat';

7. Deleting All Rows – Safely

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.

8. Checking Before Deleting

Preview before deleting — always:

SELECT * FROM students WHERE class = '10A';

Once you're sure, then:

DELETE FROM students WHERE class = '10A';

Best Practices

  • Never run DELETE without WHERE unless you're absolutely sure.
  • Use SELECT first to preview what will be deleted.
  • Use PRIMARY KEY or UNIQUE fields when targeting specific rows.

Summary

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.

What’s Next?

With your data cleaned and controlled, it's time to dive into SQL Constraints — the invisible rules that keep your tables valid and consistent.

QUIZ

Question 1:Which of the following SQL statements correctly deletes all students from class '10B'?

Question 2:Using DELETE without a WHERE clause will remove all rows from the table.

Question 3:Which of the following statements about the DELETE command are correct?

Question 4:What does the following statement do?
DELETE FROM students WHERE roll_no = 111;

Question 5:DELETE and TRUNCATE can both be used to remove all records from a table, but TRUNCATE is faster.

Question 6:What are some risks of using the DELETE statement in SQL?



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