I
I
Ivan Rumyantsev2018-03-05 17:23:25
MySQL
Ivan Rumyantsev, 2018-03-05 17:23:25

How to display the maximum number of satellites for the planet?

select * from ( select NAME as `Название планет` , COUNT(Sputnik.Planet_idPlanet) as `Количество спутников` from Planet 
inner join Sputnik on Planet.idPlanet = Sputnik.Planet_idPlanet 
group by Sputnik.Planet_idPlanet ) as `results`
Where...;

I don't know how to write further to display the planet with the maximum number of satellites.
Subquery (displays the name and number of satellites of the planet), but I don’t know how to form the main query.
Please do not offer through limit 1. Through MAX please.
5a9d524bc2e36461387152.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anatoly Zharov, 2018-03-05
@Hipstoun

select `results`.`Название планет`, max(`results`.`Количество спутников`) from ...

A
Artem Spiridonov, 2018-03-05
@customtema

I would denormalize the number of satellites into a planet property. The solution is trivial.

C
cicatrix, 2018-03-05
@cicatrix

Something like this:

select max(moonCount) from (
    select 
        p.idPlanet, 
        count(m.idSputnik) moonCount
    from Planet p
    join Sputnik m on m.Planet_idPlanet = p.idPlanet
    group by p.idPlanet);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question