Practical 4: Daleting Data
2024, May 8
The DELETE
statement is used to delete records or rows from a table.
To delete records from a table, use the following syntax:
DELETE FROM "tablename"
WHERE "columnname" OPERATOR "value"
[AND|OR "columnname" OPERATOR "value"];
Note: The WHERE
clause is optional. If omitted, all records will be deleted.
Consider the table employee
.
DELETE FROM employee;
If you leave off the WHERE
clause, all records will be deleted!
DELETE FROM employee
WHERE lastname = 'May';
DELETE FROM employee
WHERE firstname = 'Mike' OR firstname = 'Eric';
Use the SELECT
statement to verify your deletions:
DELETE
statements.Jonie Weber-Williams just quit, remove her record from the table:
DELETE FROM myemployees_ts0211
WHERE lastname = 'Weber-Williams';
It's time for budget cuts. Remove all employees who are making over 70000 dollars:
DELETE FROM myemployees_ts0211
WHERE salary > 70000;
Create at least two of your own DELETE
statements:
-- Delete employees from the "Sales" department
DELETE FROM myemployees_ts0211
WHERE department = 'Sales';
-- Delete employees hired before the year 2000
DELETE FROM myemployees_ts0211
WHERE hire_date < '2000-01-01';
Delete all records from the table:
DELETE FROM myemployees_ts0211;
Test your understanding with the following questions: