SELECT id,name,class,sex,student_id,mark FROM `student6` a
LEFT JOIN (SELECT student_id, AVG(mark) as mark
FROM student_mark GROUP BY student_id ) b on a.id= b.student_id
Video Tutorial on all SQL JOINs including LEFT JOIN
Maximum mark of each student in all exams
SELECT id,name,class,sex,student_id,mark FROM `student6` a
LEFT JOIN (SELECT student_id, MAX(mark) as mark
FROM student_mark GROUP BY student_id ) b on a.id= b.student_id
Out of two exams conducted every month, highest mark is considered.
SELECT id,name,class,sex,student_id,mark, month FROM `student6` a
LEFT JOIN (SELECT student_id, MAX(mark) as mark,
month(exam_dt) as month
FROM student_mark GROUP BY month(exam_dt),student_id) b
on a.id= b.student_id
SELECT id,name,class,sex,student_id,mark, month FROM `student6` a
LEFT JOIN (SELECT student_id, AVG(mark) as mark,
month(exam_dt) as month
FROM student_mark GROUP BY month(exam_dt),student_id) b
on a.id= b.student_id
Output will be same table with change in data for Maximum column.
Consider mark of student for the exams conducted in later part of the month.
Here we will try by using maximum exam date for each month
SELECT id,name,class,sex,mark,student_id,
exam_dt FROM `student6` a
LEFT JOIN (SELECT mark, student_id,exam_dt from student_mark
where exam_dt in
(SELECT MAX(exam_dt) from student_mark group by month(exam_dt)) ) b
on a.id= b.student_id
id
name
class
sex
mark
student _id
exam_dt
1
John Deo
Four
female
76
1
2017-04-19
1
John Deo
Four
female
90
1
2017-05-24
1
John Deo
Four
female
75
1
2017-06-21
2
Max Ruin
Three
male
75
2
2017-04-19
2
Max Ruin
Three
male
86
2
2017-05-24
2
Max Ruin
Three
male
87
2
2017-06-21
3
Arnold
Three
male
72
3
2017-04-19
3
Arnold
Three
male
80
3
2017-05-24
3
Arnold
Three
male
61
3
2017-06-21
4
Krish Star
Four
female
82
4
2017-04-19
4
Krish Star
Four
female
85
4
2017-05-24
4
Krish Star
Four
female
87
4
2017-06-21
5
John Mike
Four
female
95
5
2017-04-19
5
John Mike
Four
female
84
5
2017-05-24
5
John Mike
Four
female
87
5
2017-06-21
LEFT JOIN three tables
Show maximum mark with student details and house name
SELECT a.id,a.name,a.class,b.mark,c.h_name FROM `student6` a
LEFT JOIN
(SELECT student_id, MAX(mark) as mark FROM student_mark
GROUP BY student_id ) b
on a.id= b.student_id
LEFT JOIN student_house c on a.house_id=c.house_id
id
name
class
mark
h_name
1
John Deo
Four
93
Queen
2
Max Ruin
Three
87
King
3
Arnold
Three
80
King
4
Krish Star
Four
87
Jack
5
John Mike
Four
95
Queen
LEFT JOIN & ORDER BY
SELECT a.id,a.name,a.class,b.mark,c.h_name FROM `student6` a
LEFT JOIN
(SELECT student_id, MAX(mark) as mark FROM student_mark
GROUP BY student_id ) b
on a.id= b.student_id
LEFT JOIN student_house c on a.house_id=c.house_id
order by h_name
Maximum average Mark scored by using Order by and LIMIT
We will display list in the order of average value from maximum to minimum and then use LIMIT query to get first record.
SELECT id,name,class,sex,student_id,mark FROM `student6` a
LEFT JOIN (SELECT student_id, AVG(mark) as mark FROM student_mark
GROUP BY student_id )
b on a.id= b.student_id order by mark desc limit 0,1