SQL DELETE Statement
SQL DELETE Statement :
In SQL DELETE statement is used to delete rows or records in a table. We can either delete single row or multiple rows using delete statement.
SQL DELETE Syntax :
DELETE FROM table_name WHERE column_name = record_value;
In the above syntax we can delete all the records where following condition holds to be true -
column_name = record_value;
Demo Database :
Consider the following demo table -
+----+----------+-----+-----------+----------+ | ID | ENAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Kota | 2000.00 | | 2 | Raj | 23 | Delhi | 1500.00 | | 3 | Anand | 21 | Karachi | 2000.00 | | 4 | Saurabh | 25 | Mumbai | 6500.00 | | 5 | Poonam | 29 | Bhopal | 8500.00 | | 6 | Komal | 23 | Pune | 4500.00 | | 7 | Omkar | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
SQL DELETE Statement : Delete single record
DELETE FROM employee WHERE name = 'Raj';
It will delete single record as the above database have only one record having name = ‘Raj’. Query results into -
+----+----------+-----+-----------+----------+ | ID | ENAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Kota | 2000.00 | | 3 | Anand | 21 | Karachi | 2000.00 | | 4 | Saurabh | 25 | Mumbai | 6500.00 | | 5 | Poonam | 29 | Bhopal | 8500.00 | | 6 | Komal | 23 | Pune | 4500.00 | | 7 | Omkar | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+
Now consider the below query -
DELETE FROM employee WHERE ID > 3;
Here all records whose ID > 3 will be deleted.
+----+----------+-----+-----------+----------+ | ID | ENAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Kota | 2000.00 | | 2 | Raj | 23 | Delhi | 1500.00 | | 3 | Anand | 21 | Karachi | 2000.00 | +----+----------+-----+-----------+----------+
If we did not put where condition then all the records from the table will be deleted and query will results into 0 records.
DELETE FROM employee;
will results into
0 record(s) found