Sql | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Sql

I have 3 matrix Athletes(idathletes, name, age, country) Coach(idcoach, name, age, country) Training(idtraining, salary, athletes_idathletes, coach_idcoach)... I want to show the max sum of the coach and his name.... One coach can train more than one athletes

19th May 2020, 9:07 PM
Jason Papastavrou
Jason Papastavrou - avatar
10 Answers
+ 3
SELECT c.`name`, sum(t.`salary`) AS 'balance' FROM `Coach` c JOIN `Training` t ON t.`coach_idcoach` = c.`id` WHERE c.`idcoach` = 1 change the id in last line
19th May 2020, 9:09 PM
Ore
Ore - avatar
+ 1
SELECT c.name, sum(t.salary) "sum_salary" FROM Coach as c JOIN Training as t ON c.idcoach = t.coach_idcoach GROUP BY c.name ORDER BY "sum_salary" DESC; The first result in output will have Max salary.
17th Feb 2021, 6:44 AM
Vlad 🇺🇦
Vlad 🇺🇦 - avatar
0
This prints me the sum of all coaches... I want only the sum of one coach
19th May 2020, 9:26 PM
Jason Papastavrou
Jason Papastavrou - avatar
0
It prints the sum of only 1 coach(idcoach = 1). Jason Papastavrou Try it and see for yourself
19th May 2020, 9:30 PM
Ore
Ore - avatar
0
Yeah you are rigtht... But if i dont know in which idcoach place is he... What should i do?
19th May 2020, 9:36 PM
Jason Papastavrou
Jason Papastavrou - avatar
0
Jason Papastavrou You have to know the id of the coach you are searching for because idcoach is the primary key
19th May 2020, 10:23 PM
Ore
Ore - avatar
0
Ok and one last question, if i want to show the names of the athletes he trains
19th May 2020, 10:40 PM
Jason Papastavrou
Jason Papastavrou - avatar
0
You can chain different JOIN statements SELECT c.`name` AS 'Coach', group_concat(a.`name`) AS 'Athletes' sum(t.`salary`) AS 'balance' FROM `Training` t JOIN `Coach` c ON t.`coach_idcoach` = c.`idcoach` JOIN `Athletes` a ON t.`athletes_idathletes` = a.`idathletes` WHERE c.`id` = 1
19th May 2020, 10:53 PM
Ore
Ore - avatar
0
You are the best
19th May 2020, 11:17 PM
Jason Papastavrou
Jason Papastavrou - avatar
19th May 2020, 11:28 PM
Ore
Ore - avatar