Delete SQL

Galaxy Glossary

How do you remove data from a table in SQL?

The DELETE statement in SQL is used to remove rows from a table. It's a crucial part of data manipulation, allowing you to update your database by removing unwanted or outdated information. Proper use of the WHERE clause is essential for targeted deletion.
Sign up for the latest in SQL knowledge from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Description

The DELETE statement is a fundamental command in SQL for removing rows from a table. It's a part of the DML (Data Manipulation Language) and is used to modify the data within a database. Unlike truncating a table, which removes all rows and cannot be undone, DELETE allows for more control over which rows are removed. This is particularly useful when you need to selectively delete data based on specific criteria. For instance, you might want to delete all orders from a specific customer or remove products that are no longer in stock. The DELETE statement is crucial for maintaining data integrity and accuracy within a database. It's essential to use the WHERE clause carefully to avoid unintended data loss. Incorrect use of the WHERE clause can lead to the deletion of more data than intended.

Why Delete SQL is important

The DELETE statement is vital for maintaining a clean and accurate database. It allows for selective data removal, preventing data redundancy and ensuring that the database reflects the current state of the data. This is crucial for applications that need to manage and update data frequently.

Example Usage


-- Example table (Customers)
CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    City VARCHAR(50)
);

-- Insert some sample data
INSERT INTO Customers (CustomerID, FirstName, LastName, City) VALUES
(1, 'John', 'Doe', 'New York'),
(2, 'Jane', 'Smith', 'Los Angeles'),
(3, 'Peter', 'Jones', 'Chicago'),
(4, 'Mary', 'Brown', 'Houston');

-- Delete a specific customer
DELETE FROM Customers WHERE CustomerID = 2;

-- Delete all customers from New York
DELETE FROM Customers WHERE City = 'New York';

-- Verify the changes (important!) 
SELECT * FROM Customers;

Common Mistakes

Want to learn about other SQL terms?