Safwan Ull Karim
Safwan Ull Karim

Reputation: 662

How to delete specific rows in a sql table

I am new to SQL and I was looking at the DELETE keyword. I want to know how can I delete multiple rows in one go. Eg I want to delete CategoryID 2,3,5. I am trying

DELETE FROM Categories
WHERE CategoryID="2"AND CategoryID="3" AND CategoryID="5";

but no rows and deleted. And if I use OR then everything gets deleted.

Table name Categories

 CategoryID   CategoryName          
   1           Beverages         
   2           Condiments        
   3           Confections       
   4           Dairy Products    
   5           Grains/Cereals    
   6           Meat/Poultry  

Upvotes: 4

Views: 19232

Answers (3)

Jeet Shah
Jeet Shah

Reputation: 5

USE 'OR' instead of 'AND' since it is a logical error -
you cannot have ID as 2 AND 3 AND 5
it has to be ID=2 OR 3 OR 5

DELETE FROM Categories WHERE CategoryID="2"OR CategoryID="3" OR CategoryID="5";

Upvotes: 0

Akhil S Kamath
Akhil S Kamath

Reputation: 1058

In your query case,

DELETE FROM Categories
WHERE CategoryID="2"AND CategoryID="3" AND CategoryID="5";

there is no row with the data with same category id as 2,3,5. So you can use 'IN' for getting the respective rows.(3 rows from your data)

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1271151

Use IN:

DELETE FROM Categories
WHERE CategoryID IN (2, 3, 5);

Upvotes: 9

Related Questions