user15697038
user15697038

Reputation:

Deleting Records in SQL using a DELETE query

Below is the query I have which returns results that I want to delete from a table.

SELECT * 
FROM tableA
GROUP BY id
HAVING COUNT(*) > 5 

How do I go about deleting the records that show up from this query? I was thinking of using this DELETE query.

DELETE FROM tableA
WHERE 
     (SELECT * 
      FROM tableA
      GROUP BY id
      HAVING COUNT(*) > 5)

But this does not seem to be working.

Upvotes: 0

Views: 37

Answers (2)

Craig
Craig

Reputation: 1226

In this sort of situation, you probably need to do something like this:

DELETE from tableA
where [id] IN
(SELECT [id] from tableA
group by id
having count(*) > 5)

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269463

You can use in:

delete from tableA
where id in (select id from tableA group by id having count(*) > 5)

Upvotes: 1

Related Questions