w3resource

SQL Delete

SQL Delete records from a table

The SQL DELETE command is used to delete rows or records from a table.

Syntax:

DELETE FROM { table_name | ONLY (table_name) }
[{ WHERE search_condition | WHERE CURRENT OF cursor_name }]

Parameter:

Name Description
table_name Name of the table.
search_condition specify a search condition (logical expression) that has one or more conditions.

Syntax diagram - DELETE STATEMENT

Syntax diagram - DELETE STATEMENT

Some important questions on the SQL DELETE statement

What is the purpose of the DELETE statement in SQL?

  • The DELETE statement is used to remove one or more records from a table in a database.

  • How do you delete specific records from a table using the DELETE statement?

  • To delete specific records from a table, you include a WHERE clause in the DELETE statement with conditions that specify which records to remove based on column values.

  • Can you rollback a DELETE statement in SQL?

  • Yes, if the database management system supports transactions and if the DELETE statement is executed within a transaction, you can rollback the transaction to undo the effects of the DELETE operation.

  • What are some alternatives to using the DELETE statement for data removal?

  • Depending on the scenario, alternatives to using the DELETE statement include using the TRUNCATE TABLE statement (to remove all records from a table without logging individual deletions), using stored procedures with additional logic for validation and logging, or using soft delete mechanisms where records are marked as inactive rather than physically deleted.

  • What is the significance of the WHERE clause in a DELETE statement?

  • The WHERE clause in a DELETE statement is used to specify the conditions that must be met for records to be deleted. It allows for selective deletion of records based on specified criteria.

  • What happens if you execute a DELETE statement without a WHERE clause?

  • If you execute a DELETE statement without a WHERE clause, it will remove all records from the specified table, resulting in the deletion of all data.

  • How do you ensure data integrity when using the DELETE statement?

  • To ensure data integrity, you should always make sure to have proper backup procedures in place before executing DELETE statements, especially when dealing with critical data. Additionally, testing DELETE statements in a development or staging environment before executing them in a production environment can help prevent accidental data loss.

  • How do you delete records from multiple tables using a single DELETE statement?

  • To delete records from multiple tables using a single DELETE statement, you can use a subquery in the WHERE clause or specify joins between tables to establish relationships and delete records based on those relationships.

  • What are the components of a DELETE statement?

  • The main components of a DELETE statement include the DELETE keyword, the FROM clause specifying the table from which records will be deleted, and an optional WHERE clause to specify the conditions for deleting records.

  • What are the potential risks associated with executing a DELETE statement?

  • One potential risk is accidentally deleting important data if the WHERE clause is not properly specified. It's essential to double-check the conditions before executing the DELETE statement to avoid unintended data loss.

  • Table of Contents :

    Sample tables associated with this page have shown bellow:

    Sample table: daysorder


    Sample table: customer1


    To remove all rows from the table 'daysorder', the following SQL statement can be used :

    
    -- Delete all records from the table named 'daysorder'
    DELETE FROM daysorder;
    

    Explanation:

    • This SQL statement deletes all records from the table named 'daysorder'.

    • DELETE FROM is the SQL command used to remove records from a table.

    • daysorder is the name of the table from which records will be deleted.

    • The semicolon ; denotes the end of the SQL statement.

    Output:

    Sql select re-ordering columns

    SQL deleting records with where

    In this page, we are going to discuss, how the WHERE clause along with SQL DELETE command can be used to remove number of rows against some conditions.

    Example:

    To remove rows from the table 'customer1' with the following condition -

    1. 'cust_country' must be 'Canada',

    the SQL statement can be used:

    SQL Code

    
    -- This SQL code deletes records from the 'customer1' table where the 'cust_country' column value is 'Canada'.
    DELETE FROM customer1 -- This line specifies the action of deleting records from the 'customer1' table.
    WHERE cust_country='Canada'; -- This line specifies the condition for deletion, selecting only rows where the 'cust_country' column has the value 'Canada'.
    

    Explanation:

    • This SQL code performs a deletion operation on a database table named 'customer1'.

    • It removes records from the table where the country of the customer is specified as 'Canada'.

    • The operation is focused on one specific condition, which is the country of the customer being 'Canada'. Any record meeting this condition will be deleted.

    • The 'DELETE FROM' statement is used to remove records from the specified table.

    • The 'WHERE' clause is used to specify the condition that determines which records will be deleted. In this case, only records where the 'cust_country' column has the value 'Canada' will be deleted.

    Output:

    Sql select re-ordering columns

    SQL Delete records using comparison operators

    In this page, we are going to discuss, how the COMPARISON operator along with SQL DELETE command can be used to remove specific rows from a table.

    Sample tables associated with this page have shown bellow:

    Sample table: customer1


    Sample table: daysorder


    Example

    To remove rows from the table 'customer1' with the following condition -

    1. 'cust_country' must be 'India',

    2. 'cus_city' must not be 'Chennai',

    the following SQL statement can be used :

    SQL Code:

    
    -- This SQL code deletes records from the 'customer1' table where the 'cust_country' column value is 'India'
    -- and the 'cust_city' column value is not 'Chennai'.
    DELETE FROM customer1 -- This line specifies the action of deleting records from the 'customer1' table.
    WHERE cust_country='India' -- This line specifies the first condition for deletion, selecting only rows where the 'cust_country' column has the value 'India'.
    AND cust_city<>'Chennai'; -- This line specifies the second condition for deletion, selecting only rows where the 'cust_city' column value is not 'Chennai'.
    

    Explanation:

    • This SQL code deletes records from the 'customer1' table based on two conditions.

    • It removes records where the country of the customer is specified as 'India' and the city is not 'Chennai'.

    • The operation is focused on two specific conditions, which are the country of the customer being 'India' and the city not being 'Chennai'. Any record meeting both conditions will be deleted.

    • The 'DELETE FROM' statement is used to remove records from the specified table.

    • The 'WHERE' clause is used to specify the conditions that determine which records will be deleted. In this case, records where the 'cust_country' column has the value 'India' and the 'cust_city' column value is not 'Chennai' will be deleted.

    Output:

    Sql select re-ordering columns

    SQL delete records using IN operator

    In this page, we are going to discuss, how the SQL IN operator along with SQL DELETE command can be used to remove specific rows from a table.

    Example:

    To remove rows from the table 'daysorder' with the following condition -

    1. 'advance_amount' should be other than 1000 and 2000,

    the following SQL statement can be used:

    SQL Code:

    
    -- This SQL code deletes records from the 'daysorder' table where the 'advance_amount' column value is not 1000 or 2000.
    DELETE FROM daysorder -- This line specifies the action of deleting records from the 'daysorder' table.
    WHERE advance_amount -- This line specifies the condition for deletion, selecting only rows where the 'advance_amount' column does not match the specified values.
    NOT IN (1000,2000); -- This line specifies the values that the 'advance_amount' column should not have in order for the corresponding rows to be deleted.
    

    Explanation:

    • This SQL code performs a deletion operation on a database table named 'daysorder'.

    • It removes records from the table where the 'advance_amount' column value is neither 1000 nor 2000.

    • The operation is focused on one specific condition, which is the 'advance_amount' column not being equal to either 1000 or 2000. Any record meeting this condition will be deleted.

    • The 'DELETE FROM' statement is used to remove records from the specified table.

    • The 'WHERE' clause is used to specify the condition that determines which records will be deleted. In this case, records where the 'advance_amount' column value is not in the list (1000, 2000) will be deleted.

    Output:

    Sql select re-ordering columns

    SQL delete records using BETWEEN operator

    In this page, we are going to discuss, how the SQL BETWEEN operator along with SQL DELETE command can be used to remove specific rows from a table.

    Example:

    To remove rows from the table 'daysorder' with the following condition -

    1. 'advance_amount' should be within the range of 100 and 800,

    the following SQL statement can be used:

    SQL Code:

    
    -- This SQL code deletes records from the 'daysorder' table where the 'advance_amount' column value is between 100 and 800.
    DELETE FROM daysorder -- This line specifies the action of deleting records from the 'daysorder' table.
    WHERE advance_amount -- This line specifies the condition for deletion, selecting only rows where the 'advance_amount' column value falls within a specified range.
    BETWEEN 100 AND 800; -- This line specifies the range of values for the 'advance_amount' column, inclusive of both 100 and 800.
    

    Explanation:

    • This SQL code performs a deletion operation on a database table named 'daysorder'.

    • It removes records from the table where the 'advance_amount' column value falls within the range of 100 to 800.

    • The operation is focused on one specific condition, which is the 'advance_amount' column value being between 100 and 800. Any record meeting this condition will be deleted.

    • The 'DELETE FROM' statement is used to remove records from the specified table.

    • The 'WHERE' clause is used to specify the condition that determines which records will be deleted. In this case, records where the 'advance_amount' column value is between 100 and 800 (inclusive) will be deleted.

    Output:

    Sql select re-ordering columns

    SQL delete records using LIKE operator

    In this page, we are going to discuss, how the SQL LIKE operator along with SQL DELETE command can be used to remove specific rows from a table.

    Example:

    To remove rows from the table 'customer1' with the following condition -

    1. 'cust_city' should begin with the letter 'L',

    the following SQL statement can be used:

    SQL Code:

    
    -- This SQL code deletes records from the 'customer1' table where the 'cust_city' column value starts with 'L'.
    DELETE FROM customer1 -- This line specifies the action of deleting records from the 'customer1' table.
    WHERE cust_city -- This line specifies the condition for deletion, selecting only rows where the 'cust_city' column value matches a specified pattern.
    LIKE 'L%'; -- This line specifies the pattern that the 'cust_city' column values should match, where 'L%' means any string starting with 'L'.
    

    Explanation:

    • This SQL code performs a deletion operation on a database table named 'customer1'.

    • It removes records from the table where the 'cust_city' column value starts with the letter 'L'.

    • The operation is focused on one specific condition, which is the 'cust_city' column value starting with 'L'. Any record meeting this condition will be deleted.

    • The 'DELETE FROM' statement is used to remove records from the specified table.

    • The 'WHERE' clause is used to specify the condition that determines which records will be deleted. In this case, records where the 'cust_city' column value starts with 'L' will be deleted, using the 'LIKE' operator for pattern matching.

    Output:

    Sql select re-ordering columns

    See our Model Database

    Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

    Previous: UPDATE using subqueries
    Next: SQL Delete with subqueries

    

    Follow us on Facebook and Twitter for latest update.