Updated on 2023-05-23 GMT+08:00

Deleting Data from a Table

You can delete outdated data from a table by row.

SQL statements can only access and delete an independent row by declaring conditions that match the row. If a table has a primary key column, you can use it to specify a row. You can delete several rows that match the specified condition or delete all the rows from a table.

For example, to delete all the rows whose c_customer_sk column is 3869 from table customer_t1, run the following statement:

1
DELETE FROM customer_t1 WHERE c_customer_sk = 3869;

Delete the records whose c_customer_sk is 6885 and 4321 from the customer_t1 table.

1
DELETE FROM customer_t1 WHERE c_customer_sk in (6885, 4321);

Delete the records whose c_customer_sk is greater than 4000 and less than 5000 from the customer_t1 table.

1
DELETE FROM customer_t1 WHERE c_customer_sk > 4000 and c_customer_sk < 5000;

To delete all rows from the table, run either of the following statements:

1
DELETE FROM customer_t1;
1
TRUNCATE TABLE customer_t1;

If you need to delete an entire table, you are advised to use the TRUNCATE statement rather than DELETE.

To delete a table, execute the following statement:

1
DROP TABLE customer_t1;