Categories: DBMS

SQL: DELETE Query

What is Delete Query?

In SQL, the DELETE Query removes existing records from a table. Depending on the condition specified in the WHERE clause, we can delete a single record or multiple records.

NOTE: This command must be used with extreme caution because it permanently deletes the data.

Syntax:

DELETE FROM table_name [WHERE condition];  

where table_name is the table that has to be deleted, the WHERE clause in SQL DELETE statement is optional here.

Example:

Let’s take an example of student table:

ROLLNAMESEMESTERYEAR
1RAMESH53
2SUJIT21
3SURESH11
4SUJIT74
student
  • Delete all the records:
DELETE FROM student;

The given query will delete all the records from the student table, as we haven’t mentioned any conditions for deletion.

  • Delete a particular Record from a Table:
DELETE FROM student WHERE NAME="SUJIT";

The above query will delete two records from the above table as there are two students with the name “SUJIT“.

Output:

ROLLNAMESEMESTERYEAR
1RAMESH53
3SURESH11
student
TRUNCATE vs DELETE:

TRUNCATE and DELETE are both SQL commands for removing data from a table, but they have some key differences:

  • DELETE is a DML (Data Manipulation Language) operation, whereas TRUNCATE is a DDL (Data Definition Language) operation. This means that TRUNCATE is used to modify the table’s structure, whereas DELETE is used to modify the data within the table.
  • TRUNCATE deletes all rows from a table, whereas DELETE deletes specific rows based on a condition.
  • TRUNCATE is not transactional, which means it cannot be reversed. DELETE, on the other hand, is a transactional command that can be reversed if necessary.
  • DELETE does not reset the identity value to the initial seed value, whereas TRUNCATE does.

Note: also read about SQL: UPDATE Query

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago