Skip to content

Commit

Permalink
docs: delete from table as alias (#13568)
Browse files Browse the repository at this point in the history
* Update dml-delete-from.md

* Update dml-delete-from.md
  • Loading branch information
soyeric128 authored Nov 3, 2023
1 parent 8683889 commit c3be309
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions docs/doc/14-sql-commands/10-dml/dml-delete-from.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: DELETE

import FunctionDescription from '@site/src/components/FunctionDescription';

<FunctionDescription description="Introduced: v1.1.60"/>
<FunctionDescription description="Introduced or updated: v1.2.174"/>

Removes one or more rows from a table.

Expand All @@ -15,43 +15,36 @@ Databend ensures data integrity with atomic operations. Inserts, updates, replac
## Syntax

```sql
DELETE FROM <table_name> [[AS] alias]
DELETE FROM <table_name> [AS <table_alias>]
[WHERE <condition>]
```
- `AS <table_alias>`: 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
Expand All @@ -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
Expand All @@ -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.

Expand Down

1 comment on commit c3be309

@vercel
Copy link

@vercel vercel bot commented on c3be309 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.