Delete Query is used for deleting the existing rows(records) from table. Generally DELETE query is used along with WHERE clause to delete the certain number of rows that fulfills the specified condition. However DELETE query can be used without WHERE clause too, in that case the query would delete all the rows of specified table.
Syntax
1) To Delete a particular set of rows:
DELETE FROM TableName WHERE condition;
2) To Delete all the rows of a table:
DELETE FROM TableName;
Example:
Lets take the same table EMPLOYEES which we had in the last tutorial.
+------+----------+---------+----------+ |SSN | EMP_NAME | EMP_AGE |EMP_SALARY| +------+----------+---------+----------+ | 101 | Steve | 23 | 9000.00 | | 223 | Peter | 24 | 2550.00 | | 388 | Shubham | 19 | 2444.00 | | 499 | Chaitanya| 29 | 6588.00 | | 589 | Apoorv | 21 | 1400.00 | | 689 | Rajat | 24 | 8900.00 | | 700 | Ajeet | 20 | 18300.00 | +------+----------+---------+----------+
Delete all the records that have SSN greater than 400:
SQL> DELETE FROM EMPLOYEES WHERE SSN > 400;
After the successful execution of above query the table would be having below mentioned records:
+------+----------+---------+----------+ |SSN | EMP_NAME | EMP_AGE |EMP_SALARY| +------+----------+---------+----------+ | 101 | Steve | 23 | 9000.00 | | 223 | Peter | 24 | 2550.00 | | 388 | Shubham | 19 | 2444.00 | +------+----------+---------+----------+
Delete the data of employees having age greater than or equal to 24:
SQL> DELETE FROM EMPLOYEES WHERE EMP_AGE >=24;
Result:
+------+----------+---------+----------+ |SSN | EMP_NAME | EMP_AGE |EMP_SALARY| +------+----------+---------+----------+ | 101 | Steve | 23 | 9000.00 | | 388 | Shubham | 19 | 2444.00 | +------+----------+---------+----------+
DELETE all the records of table EMPLOYEES
SQL> DELETE FROM EMPLOYEES;
Leave a Reply