The DELETE statement is used to delete a single record or multiple records from a table in MySQL.
The basic syntax is as follows:
DELETE FROM table WHERE conditions; |
EXAMPLE - WITH ONE CONDITION
Let's look at a simple MySQL DELETE query example, where we just have one condition in the DELETE statement.
For example:
DELETE FROM contacts WHERE Name = 'Dipesh'; |
We have Employee Table
Employee | ||||
Emp_id | Name | Surname | Salary | Dept_id |
1 | Rajesh | Khanna | 45000 | 2 |
2 | Sneha | Gupta | 55000 | 4 |
3 | Sandeep | Nehte | 95000 | 1 |
4 | Kirti | Patil | 25000 | 3 |
5 | Nilesh | Jadhav | 45000 | 3 |
6 | Dipesh | Das | 35000 | 5 |
7 | Kailash | Rane | 62000 | 1 |
8 | Kavita | Deshpande | 48000 | 3 |
9 | Sanjay | Datt | 50000 | 3 |
10 | Shruti | Hasan | 78000 | 1 |
After Execute:
(1 row(s) affected)
(0 ms taken)
(1 row(s) affected)
(0 ms taken)
Emp_id | Name | Surname | Salary | Dept_id |
1 | Rajesh | Khanna | 45000 | 2 |
2 | Sneha | Gupta | 55000 | 4 |
3 | Sandeep | Nehte | 95000 | 1 |
4 | Kirti | Patil | 25000 | 3 |
5 | Nilesh | Jadhav | 45000 | 3 |
7 | Kailash | Rane | 62000 | 1 |
8 | Kavita | Deshpande | 48000 | 3 |
9 | Sanjay | Datt | 50000 | 3 |
10 | Shruti | Hasan | 78000 | 1 |
EXAMPLE - WITH TWO CONDITIONS
Let's look at a MySQL DELETE example, where we just have two conditions in the DELETE statement.
For example:
DELETE FROM contacts WHERE Name = 'Sanjay' AND Salary < 30000; |
This MySQL DELETE example would delete all records from the contacts table where the last_name is 'Johnson' and the customer_id is less than 1000
No comments:
Post a Comment