0

If I have those two tables:

Planet(Name,size)

Moon(Name, PlanetName*)

How can I delete planets that have more than 3 moons? I not sure if my answer is correct or not

DELETE Name FROM Planet INNER JOIN Moon WHERE Moon >3 
0

3 Answers 3

3

Your answer is not correct. The planets that have more than three moons are given by:

select planetname from moon group by panetname having count(*) > 3; 

You can then delete the planets using in or exists:

delete from planet where planetname in (select planetname from moon group by planetname having count(*) > 3 ); 
Sign up to request clarification or add additional context in comments.

Comments

1
delete from planets where (select count(*) from moon where moon.planetname = planet.name) > 3 

or

delete from planets where exists(select count(*) from moon where moon.planetname = planet.name group by planetname -- you may not need that, because for each row they are all equal, due to the where condition having count(*) > 3 ) 

Comments

0

Your query is wrong ! because your query donot mention the count .

Try this query

DELETE from planets WHERE Name = ((SELECT COUNT(*) FROM moon WHERE moon.planetname = planet.name) > 3 ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.