Deleting Data
Introduction to SQL
Database
SQL Basics
Creating a Database
Using a Database
Creating a Table
Common Data Types
Inserting Data
Selecting Data
WHERE Clause
Comparison Operators
AND, OR, and NOT
ORDER BY Clause
LIMIT Clause
DISTINCT
LIKE Operator
IN, BETWEEN, and IS NULL
Updating Data
Deleting Data
ALTER TABLE
DROP TABLE
PRIMARY KEY and NOT NULL
FOREIGN KEY and Relationships
Aggregate Functions
GROUP BY Clause
HAVING Clause
JOIN Basics
INNER JOIN
LEFT JOIN
Aliases
Subqueries
Indexes
User Permissions and Security
Common Mistakes Beginners Make
Practice Ideas
Deleting Data
Deleting Data
Definition: The DELETE statement is used to remove existing records from a table. While UPDATE modifies data, DELETE permanently erases the entire row from the database.
Why: Beginner SQL tutorials include deletion shortly after learning to insert and update data. This completes the "CRUD" cycle (Create, Read, Update, Delete), which are the four essential functions of any persistent storage system.
Syntax
To remove data, you use the DELETE FROM keywords followed by the table name and a WHERE clause to identify which record(s) should be removed.
DELETE FROM table_name WHERE condition;
Example: Removing a Specific Record
If a student with ID #3 leaves the institution, you would remove their entire record from the students table using this query:
DELETE FROM students WHERE id = 3;
Explanation
- DELETE FROM students: This targets the specific table you want to clean up.
- WHERE id = 3: This is the most important safeguard. It tells the database to only delete the row where the ID matches 3.
- Permanent Action: Unlike a computer's "Recycle Bin," a SQL
DELETEis typically permanent. Once executed, the data cannot be recovered easily.
Key Notes
- The "Nuclear" Option: If you omit the
WHEREclause (e.g.,DELETE FROM students;), the database will delete every single record in the table, leaving you with an empty table structure. - DELETE vs. DROP: Use
DELETEto remove data (rows) but keep the table itself. UseDROP TABLEonly if you want to delete the entire table structure and everything inside it. - Safety Tip: Professional developers often run a
SELECTquery with the sameWHEREclause first (e.g.,SELECT * FROM students WHERE id = 3;) to confirm exactly which row will be deleted before they commit to the action.
🏋️ Test Yourself With Exercises
Take our quiz on Deleting Data to test your knowledge.
Browse Quizzes »