1. Home
SQL

SQL Tutorial: Learn Structured Query Language Basics

Learn all SQL tutorial concepts in detail and master your career today.

  • 59
  • 9 Hours
right-top-arrow

Tutorial Playlist

81 Lessons
9

SQL Delete Statement Example

Updated on 23/07/2024463 Views

SQL delete statement plays such an important role in data administration and control in relational databases. It gives users a great chance to get rid of records from the table based on set conditions. With that, these tables can be tracked during user interaction and the database operations can be streamlined.

SQL delete statements are essential for cases where any group of data needs to be purged, updated, or simply removed, and the reason might be redundancy or irrelevancy. This sentence is in a structured language, usually beginning with the delete keyword and the name of the targeted table, with the optional where statement to determine the specific filter criteria for deletion.

SQL Delete Statement Example

Let's say we have a table called employees with the following structure:

CREATE TABLE employees (

employee_id INT PRIMARY KEY,

first_name VARCHAR(50),

last_name VARCHAR(50),

department_id INT

);

Now, by the way, can we remove an employee with employee_id 101 from the table? The SQL delete statement for this operation would be:

DELETE FROM employees

WHERE employee_id = 101;

This saying is removing those rows (es) from the employee's table of which the employees_id is equal to 101. Here is a detailed syntax of the SQL statement that will be executed: Upon executing this statement, the records of employee 101 will be removed from the table.

Please bear in mind that the SQL delete statement stipulates where cause where to select which rows to be deleted. For the case of WHERE is not specified, all rows in the table will be deleted, and this is not advisable unless you actually want to clear the whole table.

SQL Delete Statements with Multiple Values

SQL will delete rows that sweep a range of values using the DELETE statement together with the WHERE clause. Here's an example of how to delete rows within a specified range of values:

DELETE FROM your_table

WHERE your_column BETWEEN start_value AND end_value;

In this example:

  • your_table," the name of the table from which you want to delete rows,"
  • the_column is the column that is predominantly displayed.
  • start_value and end_value are the parameter values for the range of tabular items. All data from the given column where the value will be within this_range will be removed.

For instance, if you have a table called students with a column age, and you want to delete rows where the age falls between 18 and 21, you would execute:

DELETE FROM students

WHERE age BETWEEN 18 AND 21;

This query will abstract all the rows about the student's table, where the age is between 18 and 21 inclusive. Make sure to verify the conditions before applying the delete operations to prevent redundancy or data loss errors.

SQL Delete Statement with a Cascade

Forming cascading deletes in a set of database tables uses defining relations between tables and establishing a reaction when a record in the parent table is deleted. This allows referential integrity inside the database to be ensured by naturally ensuring that the delete operation on the child tables also equally takes place if integrity is required.

In the SQL delete statement with a cascade, you usually tune into delete cascading deletes by designing a foreign key constraint with the On Delete Cascade option. Here's an overview of how it works:

  1. Create Foreign Key Constraint: While setting up a foreign key constraint, reference on delete cascade to transmit a message that simultaneous deletion of all related records in the child table will happen if any referenced record in the parent table is deleted.
  1. Cascade Deletion: Once the foreign key constraint is defined with a cascading option on delete cascade, any deletion operation on the parent table automatically will initiate the deletion of corresponding records on the child tables(s).

SQL Delete Statement with Join

The SQL delete statement with join becomes more relevant when it is plausible to know the differences between deleting operations from different join types in SQL.

Inner Joins and Deleting Related Rows

Consider a scenario where you have two tables: orders can be put through the constraints section, and below is an example of the clause that explicitly tells the database engine that orders and order_details are dependent. You would use an inner join to relate the two tables and perform the deletion:

DELETE orders, order_details

FROM orders

INNER JOIN order_details ON orders.order_id = order_details.order_id

WHERE orders.customer_id = 123;

In this example:

  • The join clause where inner is used connects the tables' orders and order_details via the order_id.
  • With delete as where clause will specify the condition for the deletion, it depicts, for example, delete orders for a particular customer(customer_id = 123).
  • Delete statement specifies both orders and order_details table, exactly why both tables rows will tightly be deleted at the same time.

Outer Joins and Deletion Strategies

Column removal, even in cases of left outer joins, requires careful consideration of how to deal with records that exist in just one of the two tables.

Deleting Records Only from the Parent Table

If you want to delete records only from the parent table and leave the related records in the child table intact, you will use a left outer join and a where clause targeting the parent table:

DELETE FROM parent_table

USING parent_table

LEFT JOIN child_table ON parent_table.id = child_table.parent_id

WHERE child_table.parent_id IS NULL;

This happens when deleting from child_table which removes rows that are not found in parent_table.

Conclusion

SQL delete statements conclude these statements influence the structurization of data within relational databases. They provide users with a means of returning records from a table(s) based on the conditions defined so that data in the database can be both maintained and checked for consistency. It may be one-row deleting for individual records, removal of records based on a selected range of values, or it may be cascading deletion to uphold the integrity of the referential data; that power to handle all these with just one command is offered by deleting statements.

Frequently Asked Questions

1. What is the DELETE command for SQL?

A. The SQL DELETE command is used to delete one or more rows that match particular conditions into a tablet.

2. How to delete the SQL table?

A. To delete a whole SQL table, you should use the DROP TABLE command wherein you provide the table name between the statement brackets, which completely deletes the table structure and all data related to the table.

3. How to delete from select SQL?

A. To delete from select in SQL, you can utilize a DELETE statement with a WHERE clause that makes a condition based on the results of the query and, in a way, diversifies the deleting process, allowing removing based on selected data.

4. Which SQL statement deletes data?

A. The symbolic query language (SQL) query that is used for deleting data is the delete statement, which removes bunches of records from a table by certain conditions.

5. How do I DELETE a row in a table in SQL?

A. One of the ways to erase a row from a table using SQL is by using the delete statement with the where clause, which is specified to identify the row that is meant to be deleted based on the conditions.

6. How do I DELETE all data from a table in SQL?

A. The only way to delete the table data from an SQL is to delete and no additional criteria are allowed which will remove all records from the table but the table structure will still be in place.

image

Devesh

Passionate about Transforming Data into Actionable Insights through Analytics, with over 3+ years of experience working in Data Analytics, Data V…Read More

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...