Home > Enterprise >  MYSQL adding columns value of rows
MYSQL adding columns value of rows

Time:10-03

I have a problem in which I have a STUDENT table as

 ------------- ------------- ------------- ------------- --------------- 
| roll_number | name        | subject_one | subject_two | subject_three |
 ------------- ------------- ------------- ------------- --------------- 
|           1 |  Sheila     |          32 |          48 |            64 |
|           2 | Rachel      |          24 |          21 |            25 |
|           3 | Christopher |          55 |          12 |            10 |
 ------------- ------------- ------------- ------------- --------------- 

I want the print the output as


 ------------- ------------- ------------- 
| roll_number | name        | total       | 
 ------------- ------------- ------------- 
|           1 |  Sheila     |          144|
|           2 | Rachel      |          70 |     
|           3 | Christopher |          77 |
 ------------- ------------- ------------- 

and select all student having marks greater than 75 ?? How can I achieve this using MYSQL ??

CodePudding user response:

I think you just need the aggregate functions and use having a function is enough
I am not sure, its can help you or not

SELECT roll_number , name , (subject_one subject_two subject_three) AS total FROM STUDENT HAVING total > 75 ;

CodePudding user response:

If retrieves only students whose total marks greater than 75 Then enable WHERE clause. If all students info needed then disable WHERE clause.

SELECT roll_number
     , name
     , (subject_one   subject_two   subject_three) total
FROM STUDENT
WHERE (subject_one   subject_two   subject_three) > 75

Please check from url https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=f60fa22ca2104ea0fb7ba53a2d30fcf1

  • Related