How to Delete a Row in SQL – Example Query (2024)

In SQL, you can delete a row in a table by using the DELETE query and the WHERE clause.

In the article, I will walk you through how to use the DELETE query and WHERE clause to delete rows. I will also show you how to delete multiple rows from a table at once.

How to use the DELETE query in SQL

This is the basic syntax for using the the DELETE query:

DELETE FROM table_nameWHERE condition of which row(s) to delete;

In this example, we have a table called cats that currently has ten rows in it. The columns would be id, name and gender.

How to Delete a Row in SQL – Example Query (1)

We want to delete the row with the id of 8 which is Loki's row.

How to Delete a Row in SQL – Example Query (2)

The first line of the DELETE query would look like this:

DELETE FROM cats

In the second line, we are going to specify which row by using the id=8 after the WHERE clause.

WHERE id=8;

Here is the complete syntax to delete Loki's row:

DELETE FROM catsWHERE id=8;

This is what the new cats table looks like:

How to Delete a Row in SQL – Example Query (3)

We can see that our DELETE query worked because Loki's information is no longer there.

How to Delete a Row in SQL – Example Query (4)

How to Delete multiple rows from a table in SQL

One way we can delete multiple rows from our cats table is to change the condition from id to gender.

If we wanted to delete the rows with just the male cats, then we can use the gender="M" condition.

DELETE FROM catsWHERE gender="M";

Our new cats table would look like this:

How to Delete a Row in SQL – Example Query (5)

Now the cats table is only showing the female cats.

How to delete multiple rows using the BETWEEN operator with the AND operator in SQL

If we wanted to delete a number of rows within a range, we can use the AND operator with the BETWEEN operator.

In this example, we want to delete rows with ids of 4-7 inclusive.

Here is the syntax for that:

DELETE FROM catsWHERE id BETWEEN 4 AND 7;

This is the result from that DELETE query:

How to Delete a Row in SQL – Example Query (6)

We can see that rows 1-3 and 8-10 are left in our table. The ids of 4-7 have been successfully deleted.

How to delete multiple rows using the IN operator in SQL

We can specify which names to delete from the cats table using the IN operator.

In this example, I want to delete the names of Lucy, Stella, Max and Tiger from our original cats table here:

How to Delete a Row in SQL – Example Query (7)

We need to specify the column and use the IN operator to list the names we want deleted.

DELETE FROM catsWHERE name IN ("Lucy","Stella","Max","Tiger");

This is what the new result would look like:

How to Delete a Row in SQL – Example Query (8)

Our DELETE query was successful, because those four cats are no longer present in the table.

How to delete all records in the table in SQL

If you want to delete all of the information from your table, then you would use this syntax:

DELETE FROM table_name;

In order to delete all of the cats from our cats table, then we would use this code.

DELETE FROM cats;

Conclusion

In this article, we learned about the different ways to delete information from a SQL table.

This is the basic syntax for using the DELETE query:

DELETE FROM table_nameWHERE condition of which row(s) to delete;

If you want to delete one row from the table, then you have to specify a condition.

WHERE id=8;

There are a few ways to delete multiple rows in a table.

If you wanted to delete a number of rows within a range, you can use the AND operator with the BETWEEN operator.

DELETE FROM table_nameWHERE column_name BETWEEN value 1 AND value 2;

Another way to delete multiple rows is to use the IN operator.

DELETE FROM table_nameWHERE column_name IN (value 1, value 2, value 3, etc...);

If you want to delete all records from the table then you can use this syntax.

DELETE FROM table_name;

I hope you enjoyed this article and best of luck on your SQL journey.

How to Delete a Row in SQL – Example Query (2024)

FAQs

How to Delete a Row in SQL – Example Query? ›

DELETE

DELETE
In the database structured query language (SQL), the DELETE statement is used to remove one or more records from a table. A subset may be defined for deletion using a condition, otherwise all records are removed.
https://en.wikipedia.org › wiki › Delete_(SQL)
FROM table_name WHERE column_name BETWEEN value 1 AND value 2; Another way to delete multiple rows is to use the IN operator. DELETE FROM table_name WHERE column_name IN (value 1, value 2, value 3, etc...); If you want to delete all records from the table then you can use this syntax.

How do you DELETE a row in SQL query? ›

To delete rows in a SQL Server table, use the DELETE statement: delete from sessions where id = 10; The WHERE clause is optional, but you'll usually want it, unless you really want to delete every row from the table.

How do I DELETE a row? ›

Select the cells, rows, or columns that you want to delete. Right-click, and then select the appropriate delete option, for example, Delete Cells & Shift Up, Delete Cells & Shift Left, Delete Rows, or Delete Columns.

How to delete row in MySQL example? ›

How to Delete in MySQL. To delete rows in a MySQL table, use the DELETE FROM statement: DELETE FROM products WHERE product_id=1; The WHERE clause is optional, but you'll usually want it, unless you really want to delete every row from the table.

Can we DELETE row from table in SQL? ›

The Syntax for Using the SQL Delete Command

The table from which we want to delete rows is specified in the table_name parameter of the DELETE FROM statement. There is an optional WHERE clause in which we can specify the condition according to which the rows should get deleted.

How do I DELETE a row in query editor? ›

Using the Query Editor

Select the table from which you want to delete rows. Go to the “Home” tab in the Query Editor and click on the “Remove Rows” button. Choose the desired option, such as “Remove Rows” or “Remove Blank Rows”. Click on the “Close & Apply” button to save the changes and exit the Query Editor.

Which command is used to delete a row? ›

To remove rows from a table, use the DELETE command. You can use the DELETE command to remove specific rows or all rows from a table. To delete rows, specify the table and an optional search condition (WHERE) that specifies which rows to delete.

What is the key to delete rows? ›

To quickly delete a row in Excel, select a row and use the shortcut CTRL - (minus sign). To quickly delete multiple rows, select multiple rows and use the same shortcut.

How do I DELETE rows from a table? ›

Right-click in a table cell, row, or column you want to delete. On the Mini toolbar, click Delete.

Which SQL command is used to delete all rows? ›

All rows in the table will be deleted using the SQL TRUNCATE command. A where clause cannot be used in this.

What is the difference between DELETE and truncate? ›

The DELETE statement removes rows one at a time and records an entry in the transaction log for each deleted row. TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction log. DELETE command is slower than TRUNCATE command.

How to delete row in SQL using ALTER command? ›

To remove columns, use the ALTER TABLE command; to remove rows (records) from a table, the DELETE FROM command is used. These are really the only important facts you need to keep in mind.

What is the drop command in SQL? ›

What is the DROP command in SQL? DROP command in SQL removes table definition and all the data from one or more database tables. It is a Data Definition Language (DDL) command. It removes all data/indexes/triggers/constraints/ and permission specific to that table.

Top Articles
Latest Posts
Article information

Author: Tyson Zemlak

Last Updated:

Views: 5594

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Tyson Zemlak

Birthday: 1992-03-17

Address: Apt. 662 96191 Quigley Dam, Kubview, MA 42013

Phone: +441678032891

Job: Community-Services Orchestrator

Hobby: Coffee roasting, Calligraphy, Metalworking, Fashion, Vehicle restoration, Shopping, Photography

Introduction: My name is Tyson Zemlak, I am a excited, light, sparkling, super, open, fair, magnificent person who loves writing and wants to share my knowledge and understanding with you.