Question : how  to sum top 3 columns values in mysql query?

Hi,

I am using MYSQL database. I want to sum top three class hours using mysql query?


SELECT SUM(CLASS_HOURS) AS TOTALHOURS FROM `tbl_class` ORDER BY CLASS_HOURS DESC LIMIT 3


When i execute this query i am getting total hours as TOTALHOURS=15,  It sums all. It should sum only top three class_hours and desired out put is 12

Can any one help me how to resolve this?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Class_hours    Sub_Name
 
2	        English
1	        Chemistry
3	        Adv. Mathematics        	
5	        General Science
4	        Basic Management
 
 
 
SELECT SUM(CLASS_HOURS) AS TOTALHOURS FROM `tbl_class` ORDER BY CLASS_HOURS DESC LIMIT 3
Open in New Window Select All

Answer : how  to sum top 3 columns values in mysql query?

SELECT SUM(CLASS_HOURS) AS TOTALHOURS FROM (
   SELECT CLASS_HOURS FROM `tbl_class` ORDER BY CLASS_HOURS DESC LIMIT 3
) AS T
Random Solutions  
 
programming4us programming4us