SQL: Creating Databases and Tables - DELETE

DELETE

The DELEE statement deletes rows of a table. Also, returns the number of rows deleted.

Syntax:

-- DELETE
DELETE FROM table
WHERE condition;

Example: Delete Bing from link

-- Select
SELECT * FROM link;
##   id            url   name description  rel
## 1  1 www.google.com Google      Google <NA>
## 2  2 www.google.com Google      Google <NA>
## 3  3  www.yahoo.com  Yahoo       Yahoo <NA>
## 4  4   www.bing.com   Bing        Bing <NA>
## 5  5 www.amazon.com Amazon      Amazon <NA>
-- DELETE Bing
DELETE FROM link
WHERE name LIKE 'B%';
## data frame with 0 columns and 0 rows
-- Select
SELECT * FROM link;
##   id            url   name description  rel
## 1  1 www.google.com Google      Google <NA>
## 2  2 www.google.com Google      Google <NA>
## 3  3  www.yahoo.com  Yahoo       Yahoo <NA>
## 4  5 www.amazon.com Amazon      Amazon <NA>

Example: Delete A from link

-- DELETE A
DELETE FROM link
WHERE name = 'A'
RETURNING *;
## data frame with 0 columns and 0 rows
-- Select
SELECT * FROM link;
##   id            url   name description  rel
## 1  1 www.google.com Google      Google <NA>
## 2  2 www.google.com Google      Google <NA>
## 3  3  www.yahoo.com  Yahoo       Yahoo <NA>
## 4  5 www.amazon.com Amazon      Amazon <NA>