EDUCBA

EDUCBA

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

SQL DELETE Statement

By Priya PedamkarPriya Pedamkar

Secondary Sidebar
SQL Tutorial
  • Functions
    • SQL Date Function
    • SQL Server Functions
    • SQL String Functions
    • SQL Compare String
    • Timestamp to Date in SQL
    • SQL REGEX
    • SQL Window Functions
    • SQL Syntax
    • SQL CONCAT
    • SQL ALTER TABLE
    • SQL MOD()
    • SQL Timestamp
    • SQL Min and Max
    • SQL TO_DATE()
    • SQL DATEADD()
    • SQL DATEDIFF()
    • SQL HOUR()
    • SQLite? functions
    • ANY in SQL
    • LIKE Query in SQL
    • SQL NOT NULL
    • SQL NOT IN
    • SQL MAX()
    • SQL MIN()
    • SQL SUM()
    • SQL COUNT
    • SQL identity
    • SQL DELETE Trigger
    • SQL Declare Variable
    • SQL Text Search
    • SQL COUNT DISTINCT
    • SQL TEXT
    • SQL Limit Order By
    • 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 COALESCE
    • SQL BLOB
    • SQL ROW_NUMBER
    • SQL Server Replace
    • SQL Ranking Function
    • SQL Server Permission
  • 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
    • SQL Quick References
    • SQL Like Wildcard
    • SQL Like with Multiple Values
    • SQL Examples
    • SQL Server Versions
    • SQL DROP DB
    • SQL Case Insensitive
    • SQL Expressions
    • Database in SQL
    • SQL Data Types
    • SQL Keywords
    • Composite Key in SQL
    • SQL WAITFOR
    • SQL Constraints
    • Transactions in SQL
    • First Normal Form
    • SQL Server Data Types
    • SQL Administration
    • SQL Variables
    • SQL Enum
    • SQL GROUP BY WHERE
    • SQL ROW
    • SQL EXECUTE
    • SQL EXCLUDE
    • SQL Performance Tuning
    • SQL UUID
    • Begin SQL
    • SQL Update Join
    • Cheat sheet SQL
  • Operators
    • SQL Operators
    • SQL Arithmetic Operators
    • SQL Logical Operators
    • SQL String Operators
    • Ternary Operator in SQL
  • Commands
    • SQL Commands
    • sqlplus set commands
    • SQL Alter Command
    • SQL Commands Update
    • SQL DML Commands
    • SQL DDL Commands
    • FETCH in SQL
  • Clause
    • SQL Clauses
    • SQL IN Operator
    • SQL SELECT DISTINCT Multiple Columns
    • SQL Null Values
    • SQL LIKE
    • SQL LIKE Query
    • SQL LIKE Operator
    • SQL LIKE Clause
    • SQL NOT Operator
    • SQL Minus
    • SQL WHERE Clause
    • SQL with Clause
    • SQL HAVING Clause
    • GROUP BY clause in SQL
    • SQL GROUP BY DAY
    • ORDER BY Clause in SQL
    • SQL ORDER BY CASE
    • SQL ORDER BY DESC
    • SQL ORDER BY DATE
    • SQL ORDER BY Alphabetical
    • SQL ORDER BY Ascending
    • SQL Order by Count
    • SQL GROUP BY Month
    • SQL GROUP BY Multiple Columns
    • 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
    • SQL Foreign Key
    • Primary Key in SQL
    • Foreign Key in SQL
    • Unique Key in SQL
    • SQL UNIQUE Constraint
    • SQL Primary Key
    • Alternate Key in SQL
    • SQL Super Key
  • T-SQL
    • T-SQL pivot
    • T-SQL Formatter
    • T-SQL TRY CATCH
    • T-SQL CTE
    • T-SQL CASE
    • T-SQL DATEPART
    • T-SQL Date Format
    • T-SQL ROUND
    • T-SQL Loop
    • T-SQL IIF
    • T-SQL Union
    • T-SQL CREATE TABLE
    • T-SQL INSERT
    • T-SQL Stuff
    • T-SQL ISNULL
    • T-SQL ADD Column
    • T-SQL DATEDIFF
  • 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
    • LEFT OUTER JOIN in SQL
    • SQL Right Join
    • SQL Cross Join
    • SQL Outer Join
    • SQL Full Join
    • SQL Self Join
    • Natural Join SQL
    • SQL Multiple Join
  • Advanced
    • SQL Aliases
    • SQL Hosting
    • SQL Auto Increment
    • SQL Injection
    • SQL Wildcards
    • SQL Check
    • SQL Indexes
    • Select Distinct
    • SQL BETWEEN
    • SQLPlus spool
    • SQL Create Table
    • SQL Schema
    • Comparison Operators in SQL
    • SQL_plus
    • SQL Formatter
    • SQL LEFT INNER JOIN
    • SQL Plus Command
    • SQLPlus not found
    • SQL Injection Attack
    • Aggregate Functions in SQL
    • SQL REVOKE
    • SQL Select Distinct Count
    • IF ELSE Statement in SQL
    • SQL CASE Statement
    • SQL While Loop
    • SQL BIGINT
    • SQL Crosstab
    • SQL Wildcard Character
    • SQL INSTR()
    • SQL now
    • SQL synonyms
    • SQLite?export to csv
    • 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 Clear Table
    • SQL DESCRIBE TABLE
    • SQL Mapping
    • Cursors in SQL
    • AND in SQL
    • Wildcard in SQL
    • SQL FETCH NEXT
    • SQL Views
    • SQL Delete View
    • Triggers in SQL
    • SQL UPDATE Trigger
    • SQL AFTER UPDATE Trigger
    • SQL Update Statement
    • SQL DROP TRIGGER
    • SQL DROP Table
    • Types of SQL Views
    • SQL Port
    • SQL Clustered Index
    • 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
    • ROLLUP in SQL
    • Escape Character SQL
    • SQL Cluster
    • SQL Backup
    • SQL Pattern Matching
    • SQL Users
    • ISNULL SQL Server
    • SQL pivot
    • SQL Import CSV
    • SQL if then else
    • SQL ignore-case
    • SQL Matches
    • SQL Search String
    • SQL Column Alias
    • SQL extensions
    • SQL Substring Function
    • Charindex SQL
  • SqlAlchemy
    • What is SQLAlchemy
    • SqlAlchemy ORM
    • SQLAlchemy count
    • SQLAlchemy update object
    • SQLAlchemy pip
    • SQLAlchemy Connection
    • SQLAlchemy Metadata
    • SQLAlchemy Raw SQL
    • SQLAlchemy Filter in List
    • SQLAlchemy Alias
    • SQLAlchemy unique
    • SQLAlchemy JSONB
    • SQLAlchemy Async
    • SQLAlchemy Types
    • SQLAlchemy Many to Many
    • SQLAlchemy Example
    • SQLAlchemy Model
    • SQLAlchemy Data Types
    • SQLAlchemy Filter
    • SQLAlchemy SQLite
    • SQLAlchemy DateTime
    • SQLAlchemy create_engine
    • SQLAlchemy Delete
    • SQLAlchemy Migrations
  • NoSQL
    • NoSQL Databases List
    • NoSQL Data Modeling
    • Types of NoSQL Databases
    • NoSQL Injection
    • NoSQL vs SQL Databases
    • NoSQL Use Cases
    • NoSQL Key Value
  • Interview Questions
    • SQL Interview Questions
    • Advance SQL Interview Questions
    • SQL Joins Interview Questions
    • SQL Server Interview Questions
    • SQL Current Month

Related Courses

JDBC Training Course

PHP course

Windows 10 Training

SQL Course Training

PL/SQL Certification Courses

Oracle Certification Courses

Home Data Science Data Science Tutorials SQL Tutorial SQL DELETE Statement

SQL DELETE Statement

Introduction to SQL DELETE Statement

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, and provisions for filtering rows for deletion and rollback, 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 are 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 :

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,938 ratings)

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 Statement

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

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.

SQL DELETE Statement

  • Basically, this statement is used to delete the existing records from the table. We need to define the table name from which we have deleted the records.
  • In the below first syntax we have not used the where condition so all the records from a table will be deleted. In the fourth syntax, we have used only on where condition so all the rows will be deleted that match this condition. In the second syntax, we are using two conditions with a where clause so data is deleted at the time matching both the condition.

Syntax –

  • Delete from name_of_table;
  • Delete from name_of_table where name_of_column1 = value AND name_of_column = values;
  • Delete from name_of_table where name_of_column <comparison operator> values;
  • Delete from name_of_table where condition;

Below is the parameter description of syntax as follows.

  • Delete – This statement is used to delete the rows from a table. We can use delete statement with and without where condition to delete rows from the table.
  • From – This is the SQL keyword that was used to define the table name from which we have deleted the records.
  • Name of table – This is nothing but the table name from which we have deleted the records. At the time of deleting records or executing a query, we need to define the table name.
  • Where condition – This is the most important parameter at the time of execution. The where condition in the delete statement of SQL will be used to delete the specified row which was we have defined in the where condition. Where condition is very useful to delete specific condition row by using the sql delete statement.
  • Name of column – This is the table column that was used to delete data from a table. If suppose we have used delete statement with specified column it will deleting the records from the table.
  • AND – This keyword is used in the delete statement to delete the rows from the specified column that we have defined in the query.
  • At the time of using a single column in where condition by using delete query it will be deleting the specified row which was matches the condition.

SQL DELETE Query

  • SQL delete query is not case sensitive we can use DELETE or delete to execute the sql delete query.
  • The below example shows that the query is not case sensitive. In the below first example we have used sql delete query in the lowercase letter, after using sql delete query it will execute without issuing any error. In the second example, we have used sql delete query in uppercase letter, after using a delete query in the uppercase letter it will also execute without issuing any error. So we can say that the SQL delete query is not case sensitive.
delete from sql_insert where id = 1001;
DELETE from sql_insert where id = 1001;

gg

  • At the time of executing any delete query on the production database server, we need to be very careful, because it may take downtime of the system.
  • Suppose our application needs to contain only new records and we have to need to delete old records same time we can use this statement to delete old records from the table.
  • To delete the records from a table we can also use the comparison operator, after using the comparison operator the matched rows will be deleted.

Examples

  • Below is the example of SQL delete statements are as follows. We are using sql_delete and sql_delete1 table to define examples of sql delete as follows.
Select * from sql_delete;
Select * from sql_delete1;

arrrr

  1. In the below example, we have deleted the specified records by using the where condition. We have used where condition on the id column. We can see that two records will be deleted because where condition will matches the two rows.
DELETE from sql_delete1 where id = 1001;
Select * from sql_delete1;

22

2. In the below example we are using a two-column with where condition to delete records from a table. We are using the id and stud_grade column with the where condition.

DELETE from sql_delete1 where id = 1002 and stud_grade = 1;
Select * from sql_delete1;

33

3. In the below example we are using the comparison operator to delete the records from a table. We are using >, < and = operator to delete the records.

DELETE from sql_delete1 where id > 1005;
DELETE from sql_delete1 where id < 1003;
DELETE from sql_delete1 where id = 1004;
Select * from sql_delete1;

SQL DELETE ll

4. In below example we are deleting the records as per varchar data type column are as follows.

Delete from sql_delete1 where stud_name = ‘ABC’;
Select * from sql_delete1;

SQL DELETE 4

5. In the below example we are deleting whole the records from the sql_delete1 table as follows.

Delete from sql_delete1;
Select * from sql_delete1;

SQL DELETE kkk

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 Statement. 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
Popular Course in this category
JDBC Training (6 Courses, 7+ Projects)
  6 Online Courses |  7 Hands-on Projects |  37+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

PHP Training (5 Courses, 3 Project)4.9
Windows 10 Training (4 Courses, 4+ Projects)4.8
SQL Training Program (7 Courses, 8+ Projects)4.7
PL SQL Training (4 Courses, 2+ Projects)4.7
Oracle Training (14 Courses, 8+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - JDBC Training Course Learn More