The DELETE Query
The DELETE Query is used to delete existing records in a table. if you are working with DELETE Query you should know that when deleting records in a table. Notice the WHERE clause in the DELETE Query. The WHERE clause specifies which records that should be deleted. otherwise all the records would be deleted.
The basic syntax of the DELETE query with a WHERE clause is as follows -
DELETE FROM table_name WHERE [define your condition];
Consider the tbl_student_record table having the following records.
id | name | class | subject | city | phone | fee | fee_date |
---|---|---|---|---|---|---|---|
1 | Mohan | 2nd | Math | Agra | 999999990 | 500 | 2018-01-02 |
2 | Pooja | 3rd | English | Delhi | 999999991 | 200 | 2018-01-11 |
3 | Rahul | 4th | Math | Agra | 999999992 | 300 | 2018-01-05 |
4 | Suresh | 2nd | English | Agra | 999999993 | 400 | 2018-01-22 |
5 | Vivek | 3rd | Math | Agra | 999999994 | 500 | 2018-01-01 |
6 | Anuj | 2nd | English | Delhi | 999999995 | 600 | 2018-01-31 |
7 | Sanju | 4th | Math | Agra | 999999996 | 400 | 2018-01-17 |
8 | Rohit | 3rd | English | Delhi | 999999997 | 700 | 2018-01-10 |
9 | Mahesh | 3rd | Math | Agra | 999999998 | 800 | 2018-01-24 |
10 | Munesh | 4th | English | Delhi | 999999999 | 200 | 2018-01-09 |
The following SQL query deletes the student id "5" from the "tbl_student_record" table:
Example 1 : The following query will delete record for a student whose ID number is 5 in the table.
DELETE FROM tbl_student_record WHERE id =5
After Query Execution : Record ID 5 has been deleted.
id | name | class | subject | city | phone | fee | fee_date |
---|---|---|---|---|---|---|---|
1 | Mohan | 2nd | Math | Agra | 999999990 | 500 | 2018-01-02 |
2 | Pooja | 3rd | English | Delhi | 999999991 | 200 | 2018-01-11 |
3 | Rahul | 4th | Math | Agra | 999999992 | 300 | 2018-01-05 |
4 | Suresh | 2nd | English | Agra | 999999993 | 400 | 2018-01-22 |
6 | Anuj | 2nd | English | Delhi | 999999995 | 600 | 2018-01-31 |
7 | Sanju | 4th | Math | Agra | 999999996 | 400 | 2018-01-17 |
8 | Rohit | 3rd | English | Delhi | 999999997 | 700 | 2018-01-10 |
9 | Mahesh | 3rd | Math | Agra | 999999998 | 800 | 2018-01-24 |
10 | Munesh | 4th | English | Delhi | 999999999 | 200 | 2018-01-09 |
Example 2 : The following SQL query deletes the "Agra" Students from the "tbl_student_record" table.
DELETE FROM tbl_student_record WHERE city ="Agra"
After Query Execution : student records has been deleted whose City Agra in the table.
id | name | class | subject | city | phone | fee | fee_date |
---|---|---|---|---|---|---|---|
2 | Pooja | 3rd | English | Delhi | 999999991 | 200 | 2018-01-11 |
6 | Anuj | 2nd | English | Delhi | 999999995 | 600 | 2018-01-31 |
8 | Rohit | 3rd | English | Delhi | 999999997 | 700 | 2018-01-10 |
10 | Munesh | 4th | English | Delhi | 999999999 | 200 | 2018-01-09 |