Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Sum with multiple conditions and join

I have a table like this below:

date, country, session

May I know how to sum up all the sessions based on the country and get the top 5 results?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The result will be something like this below:

Malaysia, 9000
Singapore, 8000
Brunei, 7000
Indonesia, 6000
Vietnam, 5000

>Solution :

SELECT COUNT(country) as sessionsCount , country 
FROM sessions 
GROUP BY country 
ORDER BY sessionsCount DESC 
LIMIT 5

The COUNT() function returns the number of rows that matches a specified criterion.

The ORDER BY keyword is used to sort the result-set in ascending or descending order.
To sort the records in descending order, use the DESC keyword.

The LIMIT clause is used to specify the number of records to return.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading