EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

SQL DELETE

Home » Data Science » Data Science Tutorials » SQL Tutorial » SQL DELETE

SQL DELETE

Introduction to SQL DELETE

SQL DELETE statement is a data manipulation language (DML) command that is used to remove one or more rows or data records from a database table or view in SQL database servers. DELETE command can be used within the body of a user-defined function. Since it does not delete the rows permanently, a DELETE transaction can be rolled back.

DELETE statement can be used along with conditional and limiting clauses such as TOP and WHERE for filtering and removing only specific rows. Albeit, DELETE might sound fancier by now as it has higher interoperability, provisions for filtering rows for deletion and rollback, but its performance gets compromised when removing all the rows from the database table. Ergo, in such situations, we should use the TRUNCATE command because it’s faster than DELETE.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

There are a few other commands in SQL like DROP and TRUNCATE table which is also used to remove data from the database objects. But they differ from each other slightly in the following ways :

Differences between DELETE, TRUNCATE, and DROP TABLE statements

DELETE TABLE TRUNCATE TABLE DROP TABLE
DELETE statement removes one or multiple rows from the database objects. TRUNCATE statement that removes all the data rows from the database object. DROP statement that removes all the records in the table and also destroys the table structure.
It is a Data Manipulation Language(DML) statement. It is a Data Definition Language(DDL) statement. It is a Data Definition Language(DDL) statement.
We can use a WHERE or TOP clause. We cannot use a WHERE or TOP clause. We cannot use a WHERE or TOP clause.
We can perform a rollback operation to undo a DELETE table statement. We cannot perform a rollback operation to undo a TRUNCATE table statement. We cannot perform a rollback operation to undo a DROP table statement.
Table rows are not deleted permanently. Table rows are deleted permanently. Table and all its relationships, index, privileges, etc are deleted permanently.

Syntax:

The basic syntax for writing the DELETE statement in SQL is as follows :

DELETE FROM table_name;

Parameters:

  • table_name : Name of the table or view that has to be removed.

The good thing about the DELETE statement is that it allows us to limit the rows which have to be removed from the table. Follow the following syntax for the same.

DELETE [TOP (expression | percentage)] FROM table_name
WHERE condition;

The arguments used in this piece of syntax is as follows :

  • expression | percentage: Expression stands for the number of top rows that have to be deleted. If we don’t have any specific number in mind, we can use the percentage instead to limit the number of rows for deletion.
  • condition: It is the filtering condition for selecting only a specific number of rows for removal.

Having discussed the syntax and arguments used in the syntax, we are all set to try a few examples based on DELETE statement in SQL.

Examples of SQL DELETE

In order to illustrate the basic functionality of SQL DELETE statements, what could be better than trying a few examples on data records of a dummy table. Ergo, let us first create a table called “customer_june”. We can use the following code snippet to perform the said task.

CREATE TABLE customer_june
(
customer_id integer,
customer_name character varying(255),
city character varying(255),
contact_no character varying(255),
email_address character varying(255)
);

After successfully creating the table, let us first insert a few data records in the table to work with. Use the following insert statement for the same. For the uninitiated, INSERT statement is used for inserting new data rows in a database table.

INSERT INTO customer_june(
customer_id, customer_name, city, contact_no, email_address)
VALUES (1001,'Rahul Sharma','Mumbai','9878987823','rs12@gmail.com'),
(1004,'Ronda Floyd','Florida','8787878787','rfloyd@gmail.com'),
(1002,'George Cooper','Kanhas City','9090909089','NULL'),
(1003,'Mithika Kapoor','Mumbai','9876547823','m1kapoor@yahoomail.com'),
(1005,'Gloria Gonsalez','New York','7545754535','gloria.gon@rediffmail.com'),
(1006,'Rahul Kashyap','New Delhi','9876504321','rk89@gmail.com');
select * from customer_june;

SQL DELETE 1

Now we are all set to try a few examples to illustrate the basic functionality of DELETE statements.

Example #1: Basic DELETE query to delete or remove records from a database object such as a database table or view.

DELETE FROM customer_details;

The query has returned successfully. This statement will remove all the rows from the customer_details table while keeping the table structure intact.

Example #2: DELETE statements with WHERE to remove only specific rows from the database table.

DELETE FROM customer_june
WHERE city = 'Kanhas City';

Now we have successfully deleted the row with misspelt city name from the “customer_june” table. The final data looks something like this after modifications.

SELECT * FROM customer_june;

SQL DELETE 2

Example #3: DELETE statements with subqueries to remove only specific rows from the database table.

DELETE FROM customer_june
WHERE EXISTS (SELECT * FROM customer_june
WHERE city LIKE '%New%'
ORDER BY customer_id DESC
LIMIT 1) ;

Popular Course in this category
JDBC Training (6 Courses, 7+ Projects)6 Online Courses | 7 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,686 ratings)
Course Price

View Course

Related Courses
PHP Training (5 Courses, 3 Project)Windows 10 Training (4 Courses, 4+ Projects)SQL Training Program (7 Courses, 8+ Projects)PL SQL Training (4 Courses, 2+ Projects)Oracle Training (14 Courses, 8+ Projects)

The query has returned successfully. The purpose of this query was to illustrate usage of subqueries in DELETE statements. We can try a number of subquery permutations and combinations in the conditional expression part when filtering records.

Conclusion

DELETE statement in SQL is used to remove one or more rows from a database table or view. It is a DML command. We can use conditional clauses such as WHERE and TOP within DELETE query to filter out and remove only specific rows.

Recommended Articles

This is a guide to SQL DELETE. Here we discuss the introduction, differences, Syntax, and Parameters with examples respectively. You may also have a look at the following articles to learn more –

  1. SQL MID
  2. SQL For loop
  3. SQL Clone Table
  4. SQL ORDER BY CASE

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
SQL Tutorial
  • Functions
    • SQL Date Function
    • SQL String Functions
    • SQL Compare String
    • SQL Window Functions
    • SQL Timestamp
    • SQL TO_DATE()
    • SQL DATEADD()
    • ANY in SQL
    • LIKE Query in SQL
    • BETWEEN in SQL
    • LTRIM() in SQL
    • TOP in SQL
    • SQL Select Top
    • Merge SQL
    • SQL TRUNCATE()
    • SQL UNION
    • SQL ALL
    • SQL INTERSECT
    • SQL Alias
    • SQL Server Substring
    • CUBE in SQL
    • SQL RANK()
    • SQL CTE
    • SQL LAG()
    • SQL MID
    • SQL avg()
    • SQL WEEK
    • SQL DELETE
    • SQL DATEPART()
    • SQL DECODE()
    • SQL DENSE_RANK()
    • SQL NTILE()
    • SQL NULLIF()
    • SQL Stuff
    • SQL Ceiling
    • SQL EXISTS
    • SQL LEAD()
    • SQL ROW_NUMBER
    • SQL Server Replace
    • T-SQL INSERT
    • SQL Ranking Function
  • Basic
    • What is SQL
    • Careers in SQL
    • Careers in SQL Server
    • IS SQL Microsoft?
    • SQL Management Tools
    • What is SQL Developer
    • Uses of SQL
    • How to Install SQL Server
    • What is SQL Server
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • SQL GROUP BY DAY
    • SQL GROUPING SETS
  • Queries
    • SQL Insert Query
    • SQL SELECT Query
    • SQL SELECT RANDOM
    • SQL Except Select
    • SQL Subquery
    • SQL SELECT DISTINCT
    • SQL WITH AS Statement
  • Keys
    • SQL Keys
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • Alternate Key in SQL
  • Joins
    • Join Query in SQL
    • Types of Joins in SQL
    • Types of Joins in SQL Server
    • SQL Inner Join
    • SQL Join Two Tables
    • SQL Delete Join
    • SQL Left Join
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • Aggregate Functions in SQL
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL INSTR()
    • What is Procedure in SQL
    • Stored Procedure in SQL?
    • SQL Server Constraints
    • SQL DELETE ROW
    • Column in SQL
    • Table in SQL
    • SQL Virtual Table
    • SQL Merge Two Tables
    • SQL Table Partitioning
    • SQL Temporary Table
    • SQL Clone Table
    • SQL Rename Table
    • SQL LOCK TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • Views in MySQL
    • SQL COMMIT
    • Distinct Keyword in SQL
    • PARTITION BY in SQL
    • SQL Set Operators
    • SQL UNION ALL
    • Metadata in SQL
    • SQL Bulk Insert
    • Array in SQL
    • SQL REGEXP
    • JSON in SQL
    • SQL For loop
    • EXPLAIN in SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL Import CSV
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - JDBC Training Course Learn More