Products table is as follows:

Database

I want to choose from it categories with the maximum amount of prices. For this, I have the following query option:

 select categoryID, sum(Price) from Products group by categoryID having sum(Price) >= all ( select sum(Price) from Products group by categoryID ) 

But this syntax error comes out:

enter image description here

And if you replace all with max :

 select categoryID, sum(Price) from Products group by categoryID having sum(Price) = max ( select sum(Price) from Products group by categoryID ) 

This is also a syntax error:

enter image description here

Please tell me what I'm doing wrong, and how to fix it. Thank you in advance :)

  • I do not know whether swears on capital letters. In the picture you have a categoryID with a large, and in the code with a small one. - danilshik
  • No, I replaced categoryID with CategoryID, but the error is the same. - Olga Climova September

1 answer 1

The maximum amount can be searched by subquery.

 select categoryID, sum(Price) from Products group by categoryID having sum(Price) >= ( select max(sm) from ( select sum(Price) as sm from Products group by categoryID ) ) 
  • Taki is not clear why> = all does not work. It seems he is for such moments and need. - Olga Climova
  • Because your condition is in the wrong place. - Roman C
  • What does "in the wrong place" mean? - Olga Climova