insert into student3_total(s_id,mark) select id, sum(math + social +science) from student3 group by id
The above query will total all marks of each student and then store them in student3_total table. We can use AVG, MAX,MIN commands in place of SUM command to get desire result.
INSERT INTO student3_total(s_id,mark) SELECT id, AVG(math + social +science) from student3 GROUP BY id
create table student3_total select id, AVG(math + social +science) from student3 group by id
SELECT id, name, class, social, science, math, s_id, mark
FROM `student3` , student3_total
WHERE id = s_id
| id | name | class | social | science | math | s_id | mark |
|---|---|---|---|---|---|---|---|
| 2 | Max Ruin | Three | 85 | 56 | 85 | 2 | 226 |
| 3 | Arnold | Three | 55 | 40 | 75 | 3 | 170 |
| 4 | Krish Star | Four | 60 | 50 | 70 | 4 | 180 |
| 5 | John Mike | Four | 60 | 80 | 90 | 5 | 230 |
| 6 | Alex John | Four | 55 | 90 | 80 | 6 | 225 |
| 7 | My John Rob | Fifth | 78 | 60 | 70 | 7 | 208 |
| 8 | Asruid | Five | 85 | 80 | 90 | 8 | 255 |
| 9 | Tes Qry | Six | 78 | 60 | 70 | 9 | 208 |
| 10 | Big John | Four | 55 | 40 | 55 | 10 | 150 |
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Mukesh | 19-01-2019 |
| i have some data , and want to sum Debit and Credit group by account id using select statement not do while or for next statement. i want result in single line statement. ACCOUNT NO. DATE PAYMENT_MODE(Debit/Credit) Amount 000001 01/01/2019 Debit 10000 000001 02/01/2019 Debit 20000 000001 03/01/2019 Credit 15000 000001 03/01/2019 Credit 10000 result Account No Debit Credit 000001 30000 25000 | |
| smo1234 | 19-01-2019 |
| Use Group by to sum based on PAYMENT_MODE(Debit/Credit) Amount. SELECT ACCOUNT_NO, PAYMENT_MODE,SUM(PAYMENT_MODE ) as MODES FROM table_name WHERE ACCOUNT_NO='000001' GROUP BY PAYMENT_MODE Read more on SQL SUM with GROUP Query | |
23-09-2019 | |
| Use Group by | |