diff --git a/docs/doc/14-sql-commands/10-dml/dml-delete-from.md b/docs/doc/14-sql-commands/10-dml/dml-delete-from.md index c05b120ae723..8d6e6939fa24 100644 --- a/docs/doc/14-sql-commands/10-dml/dml-delete-from.md +++ b/docs/doc/14-sql-commands/10-dml/dml-delete-from.md @@ -4,7 +4,7 @@ title: DELETE import FunctionDescription from '@site/src/components/FunctionDescription'; - + Removes one or more rows from a table. @@ -15,43 +15,36 @@ Databend ensures data integrity with atomic operations. Inserts, updates, replac ## Syntax ```sql -DELETE FROM [[AS] alias] +DELETE FROM [AS ] [WHERE ] ``` +- `AS `: Allows you to set an alias for a table, making it easier to reference the table within a query. This helps simplify and shorten the SQL code, especially when dealing with complex queries involving multiple tables. See an example in [Deleting with subquery using EXISTS / NOT EXISTS clause](#deleting-with-subquery-using-exists--not-exists-clause). -DELETE FROM does not support the USING clause yet. If you need to use a subquery to identify the rows to be removed, include it within the WHERE clause directly. See examples in [Subquery-Based Deletions](#subquery-based-deletions). +- DELETE does not support the USING clause yet. If you need to use a subquery to identify the rows to be removed, include it within the WHERE clause directly. See examples in [Subquery-Based Deletions](#subquery-based-deletions). ## Examples -### Direct Row Deletion +### Example 1: Direct Row Deletion + +This example illustrates the use of the DELETE command to directly remove a book record with an ID of 103 from a "bookstore" table. ```sql --- create a table +-- Create a table and insert 5 book records CREATE TABLE bookstore ( book_id INT, book_name VARCHAR ); --- insert values INSERT INTO bookstore VALUES (101, 'After the death of Don Juan'); INSERT INTO bookstore VALUES (102, 'Grown ups'); INSERT INTO bookstore VALUES (103, 'The long answer'); INSERT INTO bookstore VALUES (104, 'Wartime friends'); INSERT INTO bookstore VALUES (105, 'Deconstructed'); --- show the table before deletion -SELECT * FROM bookstore; - -101|After the death of Don Juan -102|Grown ups -103|The long answer -104|Wartime friends -105|Deconstructed - --- delete a book (Id: 103) +-- Delete a book (Id: 103) DELETE FROM bookstore WHERE book_id = 103; --- show the table again after deletion +-- Show all records after deletion SELECT * FROM bookstore; 101|After the death of Don Juan @@ -60,30 +53,35 @@ SELECT * FROM bookstore; 105|Deconstructed ``` -### Subquery-Based Deletions +### Example 2: Subquery-Based Deletions When using a subquery to identify the rows to be deleted, [Subquery Operators](../30-query-operators/subquery.md) and [Comparison Operators](../30-query-operators/comparisons/index.md) can be utilized to achieve the desired deletion. The examples in this section are based on the following two tables: ```sql --- Table: employees -+----+----------+------------+ -| id | name | department | -+----+----------+------------+ -| 1 | John | HR | -| 2 | Mary | Sales | -| 3 | David | IT | -| 4 | Jessica | Finance | -+----+----------+------------+ - --- Table: departments -+----+------------+ -| id | department | -+----+------------+ -| 1 | Sales | -| 2 | IT | -+----+------------+ +-- Create the 'employees' table +CREATE TABLE employees ( + id INT, + name VARCHAR, + department VARCHAR +); + +-- Insert values into the 'employees' table +INSERT INTO employees VALUES (1, 'John', 'HR'); +INSERT INTO employees VALUES (2, 'Mary', 'Sales'); +INSERT INTO employees VALUES (3, 'David', 'IT'); +INSERT INTO employees VALUES (4, 'Jessica', 'Finance'); + +-- Create the 'departments' table +CREATE TABLE departments ( + id INT, + department VARCHAR +); + +-- Insert values into the 'departments' table +INSERT INTO departments VALUES (1, 'Sales'); +INSERT INTO departments VALUES (2, 'IT'); ``` #### Deleting with subquery using IN / NOT IN clause @@ -106,6 +104,14 @@ WHERE EXISTS ( FROM DEPARTMENTS WHERE EMPLOYEES.DEPARTMENT = DEPARTMENTS.DEPARTMENT ); + +-- Alternatively, you can delete employees using the alias 'e' for the 'EMPLOYEES' table and 'd' for the 'DEPARTMENTS' table when their department matches. +DELETE FROM EMPLOYEES AS e +WHERE EXISTS ( + SELECT * + FROM DEPARTMENTS AS d + WHERE e.DEPARTMENT = d.DEPARTMENT +); ``` This deletes employees who belong to a department that exists in the departments table. In this case, it would delete employees with IDs 2 and 3.