- SQL - Home
- SQL - Roadmap
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Comments
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Cheatsheet
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL DELETE Statement
SQL DELETE Statement
The DELETE statement in SQL is used to remove one or more records (rows) from an existing table. Unlike DROP, which removes the entire table, DELETE only removes the data while keeping the table structure intact.
You can delete specific records based on a condition using the WHERE clause. If no condition is given, all rows in the table will be deleted.
Syntax
Following is the basic syntax to delete all rows from a table in MySQL:
DELETE FROM table_name;
Example
Assume we have creating a table named CUSTOMERS, which contains the personal details of customers including their name, age, address and salary etc.
CREATE TABLE CUSTOMERS ( ID INT NOT NULL, NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25), SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
Now insert values into this table using the INSERT statement as follows:
INSERT INTO CUSTOMERS VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00), (2, 'Khilan', 25, 'Delhi', 1500.00), (3, 'Kaushik', 23, 'Kota', 2000.00), (4, 'Chaitali', 25, 'Mumbai', 6500.00), (5, 'Hardik', 27, 'Bhopal', 8500.00), (6, 'Komal', 22, 'Hyderabad', 4500.00), (7, 'Muffy', 24, 'Indore', 10000.00);
The table will be created as follows:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Now, we will delete all records from the CUSTOMERS table without removing the table structure as shown below:
DELETE FROM CUSTOMERS;
This will remove all data from the table, but the table will still exist with all its columns:
Query OK, 7 rows affected (0.02 sec)
This will remove all data from the table, but the table will still exist with all its columns.
Verifying the Deletion
You can run a SELECT query to verify if the rows were deleted as shown below:
SELECT * FROM CUSTOMERS;
The result will be empty as shown below:
Empty set (0.00 sec)
Delete a Specific Row
You can delete a single record from a table by using the DELETE statement with a WHERE clause. This targets only the row(s) that match the specified condition.
Syntax
Following is the basic syntax to delete a specific row in SQL:
DELETE FROM table_name WHERE condition;
Example
In the following example, we delete the row from the CUSTOMERS table where the ID is equal to 3:
DELETE FROM CUSTOMERS WHERE ID = 3;
This will remove the record of Kaushik from the CUSTOMERS table. We get the output as shown below:
Query OK, 1 row affected (0.02 sec)
Verification
You can verify whether the row has been deleted using the SELECT statement as shown below:
SELECT * FROM CUSTOMERS;
We can see in the table below that there is no row with ID 3 associated with 'kaushik':
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Delete Multiple Rows
In SQL, you can delete multiple rows from a table at once using the DELETE statement with appropriate conditions. Depending on your requirement, you can use a single condition or combine multiple conditions using logical operators like AND, OR, etc.
Syntax
Following is the basic syntax to delete multiple rows in SQL:
DELETE FROM table_name WHERE condition1 AND condition2 OR ... conditionN;
Example: Deleting Rows with a Single Condition
In this example, we delete all customers all customers from the CUSTOMERS table whose age is equal to '25':
DELETE FROM CUSTOMERS WHERE AGE = 25;
We get the output as shown below:
Query OK, 2 rows affected (0.00 sec)
Verification
You can verify whether the records have been deleted using the SELECT statement as shown below:
SELECT * FROM CUSTOMERS;
We can see in the table below that there are no rows where the AGE is 25:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Example: Deleting Rows with Multiple Conditions
You can also use multiple conditions to delete specific rows.
In this example, we delete customers who are from 'Delhi' as well as those who have a salary below 3000:
DELETE FROM CUSTOMERS WHERE ADDRESS = 'Delhi' OR SALARY < 3000;
Following is the output obtained:
Query OK, 3 rows affected (0.02 sec)
Verification
You can verify whether the correct rows were deleted using the following SELECT query:
SELECT * FROM CUSTOMERS;
We can see in the table below that there are no rows where the address is 'Delhi' and the salary is less than 3000:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Important Points About Deleting Data from a Table
Following are the important points to remember when deleting a table in SQL:
- The DELETE removes rows from a table, not the table itself.
- Always use a WHERE clause to make sure you don't delete all the rows by accident.
- If you are using a transaction and your database supports it, you can undo (rollback) a DELETE operation.
DELETE vs TRUNCATE vs DROP
These SQL commands are used to remove data or entire tables, but they work in different ways:
| Command | Removes Data | Removes Structure | Can Rollback | Condition Support |
|---|---|---|---|---|
| DELETE | Yes | No | Yes (if transaction supported) | Yes (using WHERE) |
| TRUNCATE | Yes (all rows) | No | No (in most DBMS) | No |
| DROP | Yes | Yes | No | No |
Conclusion
The SQL DELETE command allows you to remove specific or all records from a table. It is safe (if used with conditions), and used for cleaning or updating records. Always cross-check your WHERE clause to avoid accidental data loss.