SELECT student, (SUM(mark1)+SUM(mark2)+SUM(mark3)....+SUM(markn)) AS Total
FROM your_table
GROUP BY student
Answer from Maulik patel on Stack OverflowSELECT student, (SUM(mark1)+SUM(mark2)+SUM(mark3)....+SUM(markn)) AS Total
FROM your_table
GROUP BY student
Another way of doing this is by generating the select query. Play with this fiddle.
SELECT CONCAT('SELECT ', group_concat(`COLUMN_NAME` SEPARATOR '+'), ' FROM scorecard')
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA` = (select database())
AND `TABLE_NAME` = 'scorecard'
AND `COLUMN_NAME` LIKE 'mark%';
The query above will generate another query that will do the selecting for you.
- Run the above query.
- Get the result and run that resulting query.
Sample result:
SELECT mark1+mark2+mark3 FROM scorecard
You won't have to manually add all the columns anymore.
NOTE: I have no idea what 'GATE Mock Test Paper' is, or if that's supposed to have a bearing on the answer, so fwiw ...
As others have pointed out, technically all of the answers could be true ... though this will depend on the RDBMS (ie, flavor of SQL) as well as any configs/settings.
If the intention is to pick answers that are ANSI compliant, then I'd say 'III' is the correct answer since all non-aggregates should be part of the group by. Then again, if you start looking at some of the nitty-gritty ANSI details you'll find this requirement can sometimes be relaxed.
As you'll find out, most RDBMS products don't enforce such a strict standard: all non-aggregates being a member of the group by clause
As Michael Kutz has mentioned, without 'A' and/or 'B' in the select/projection list, answers 'I' and 'II' will provide some confusing answers. [And if you ever find yourself working in a real world RDBMS environment you'll find developers writing these types of queries all the time, but then not being able to describe what it is they're asking for let alone being able to explain the results they end up with.]
Again, answer 'III' is the only one I'd say can be clearly explained ... what I'm looking for ... the results I end up with.
Answer
All of them are valid. However, I and II are logical nonsense.
Expected Results
For every A,B combination, you will get 1 row irregardless of what columns/aggregates are listed in the SELECT clause.
If you have 100 distinct combinations of A,B, I expect your result to show 100 rows for I,II, and III.
Logic
Answer I will show a series of values. You will have no idea which values are for which A,B combination because A and B are not part of your SELECT statement.
Because you don't have an ORDER BY clause, you can make no assumptions about which row matches to which combination of A,B.
Answer II will show a series of values for each distinct value of A. You will have no idea which values are for which A,B combination because B is not part of your SELECT statement.
Because you don't have an ORDER BY clause, you can make no assumptions about which row matches to which combination of A,B.
Answer III will show a series of values for each distinct combination of A,B.
Conclusion
From a syntax stand point, they are all valid.
From a logical stand point, only III is valid.