I was asked to create a database for both students' tardiness and school policy's violations.
Now, I have three separate tables:
tbl_ClassList:
Student_ID Student_Name
1000 Lee, Jonder
1001 Chow, Stephen
1002 Kim, Martin
1003 Johns, Kevin
1004 Hearfield, Olivia
1005 Jarrs, Marlon
tbl_Tardy:
Record_No Student_ID
1 1001
2 1001
3 1000
4 1003
5 1002
6 1003
7 1001
8 1001
9 1002
10 1004
tbl_Violation:
Record_No Student_ID
1 1000
2 1000
3 1004
4 1005
5 1001
6 1002
7 1003
What I was asked to do is to generate a list that combines that contains information about the students including his/her ID, name, number of tardiness and the number of violations. Something like this:
Student_ID Student_Name No. of Tardy No. of Violation
1000 Lee, Jonder 1 2
1001 Chow, Stephen 4 1
1002 Kim, Martin 2 1
1003 Johns, Kevin 2 1
1004 Hearfield, Olivia 1 1
1005 Jarrs, Marlon 0 1
Is there any type of Joins I can use to achieve the output? Please help me.
You can find separate aggregates for tardy and violations inside subqueries and left join them with classlist table. Use coalesce to get zero in case there is no row for tardy/violation.
select c.*,
coalesce(t.count_tardy, 0) as no_of_tardy,
coalesce(v.count_violations, 0) as no_of_violations,
from tbl_classlist c
left join (
select student_id,
count(*) as count_tardy
from tbl_tardy
group by student_id
) t on c.student_id = t.student_id
left join (
select student_id,
count(*) as count_violations
from tbl_violation
group by student_id
) v on c.student_id = v.student_id;
Related
I have three tables
Student
studenid stuname
101 john
102 aron
103 mary
104 lucy
Subject
studenid subjid subjname
101 1 maths
102 2 science
103 3 computer
104 4 english
Marks
subjid mark
1 50
2 40
3 55
4 60
1 40
2 55
3 60
I want output like this where studenid (sum of mark as total)
studenid stuname mark
101 john 90
102 aron 95
103 mary 115
104 lucy 60
Thank you in advance for yout help, i want output like this even join query or subquery which is best for timing
This just requires a straight left join across all tables, with an aggregation by student.
SELECT
st.studenid,
st.stuname,
COALESCE(SUM(m.mark), 0) AS mark
FROM Student st
LEFT JOIN Subject su
ON st.studenid = su.studenid
LEFT JOIN Marks m
ON su.subjid = m.subjid
GROUP BY
st.studenid,
st.stuname;
Demo
Note that if studenid be a primary key in the Student table, then strictly we would only need to aggregate by this column alone.
Below are the given tables:
`student` `subject`
------------------- -------------------
id name id subject
------------------- -------------------
1 Alice 1 Maths
2 Bob 2 Science
3 Eve 3 Economics
------------------- -------------------
`marks`
-----------------------------------------------------
id student_id subject_id marks
-----------------------------------------------------
1 1 1 30
2 1 2 40
3 2 3 50
4 3 1 60
5 3 2 70
-----------------------------------------------------
I need an output which should look like below:
`o/p`
----------------------------------------------
name subject marks
----------------------------------------------
Alice Maths 30
Alice Science 10
Alice Economics NULL
Bob Maths NULL
Bob Science NULL
Bob Economics 50
Eve Maths 60
Eve Science 70
Eve Economics NULL
----------------------------------------------
Please note that I am targeting MySQL 5.6.x syntax and I have created a SQL fiddle of above here to ease access to the question.
Here you go
select
st.name,
su.subject,
m.marks
from student as st
cross join subject as su
left join marks as m on m.student_id=st.id and m.subject_id=su.id
order by st.name, su.subject
SELECT student.name,
subject.subject,
marks.marks
FROM student
JOIN subject ON student.id = subject.id
JOIN marks ON student.id = marks.id
ORDER BY student.name,
subject.subject
So I have two tables, table 1:Music Styles being with fields with StyleID and StyleName, with StyleName being a genre and table 2:entertainers with fields SyleID and EntertainerID.
I need help finding is:
1) What’s the most popular genre of music style in the database of entertainers?
This is what i have so far:
SELECT StyleID, StyleName
from EA_Music_Styles
WHERE StyleID = (SELECT StyleID
from EA_Entertainer_Styles
WHERE StyleID = (SELECT MAX(StyleID)
FROM EA_Entertainer_Styles));
But i got an error code of "Subquery returns more than 1 row"
I need help, don't know exactly what to do? First time beginner.
Here are the tables:
Table 1:
StyleID StyleN
1 40's Ballroom Music
2 50's Music
3 60's Music
4 70's Music
5 80's Music
6 Country
7 Classical
8 Classic Rock & Roll
9 Rap
10 Contemporary
11 Country Rock
12 Elvis
13 Folk
14 Chamber Music
15 Jazz
16 Karaoke
17 Motown
18 Modern Rock
19 Rhythm and Blues
20 Show Tunes
21 Standards
22 Top 40 Hits
23 Variety
24 Salsa
25 90's Music
Table 2:
StyleID EntertainerID
3 1003
3 1008
4 1010
6 1007
6 1008
7 1009
7 1011
7 1012
8 1003
10 1001
10 1013
11 1007
13 1004
13 1012
14 1009
14 1011
15 1005
15 1013
17 1002
19 1002
19 1005
20 1001
20 1011
21 1001
21 1009
21 1010
22 1006
22 1010
23 1002
23 1006
24 1005
24 1006
SO the result would output the two StyleID's, 7 & 21 and StyleName which would be Classical & Standards
You just need to join your two tables and get the max count for entertainers
SELECT ms.StyleName, count(*) most_popular
from EA_Music_Styles ms
INNER JOIN A_Entertainer_Styles es
ON ms.StyleID = es.StyleID
group by ms.StyleName
order by 2 desc
limit 1
This will give you the most popular (I take that it is the one that has more rows) StyleName. I count the ocurrences of StyleName and order it desc getting only the first result of this.
Here is how you can solve the problem.
In your entertainers table user group by StyleID while also selecting the count. This will give you the styleID count which you can sort by descending and limit 1 to find to most popular one
select StyleID, count(StyleID) c from EA_Entertainer_Styles
group by StyleID
order by c desc
limit 1;
Now use the styleID form the first step to get the StyleName from your table Music.
select StyleName from EA_Music_Styles
where StyleID = id_from_step1
You can use this two in a single query by joining them as in the answer shown by Jorge Campos
Max gives you the highest number in the database, not the most used. You should use count for that.
Next example should give correct result.
select StyleName from EA_Music_Styles
where StyleID = (
select StyleID
from EA_Entertainer_Styles
group by StyleID
order by count(StyleID) desc
limit 1)
I have three tables:
Students
-------------------------------------------------------------
studentId first last gender weight
-------------------------------------------------------------
1 John Doe m 185
2 John Doe2 m 130
3 John Doe3 m 250
Lifts
-------------------
liftId name
-------------------
1 Bench Press
2 Power Clean
3 Parallel Squat
4 Deadlift
5 Shoulder Press
StudentLifts
------------------------------------------------
studentLiftId studentId liftId weight
------------------------------------------------
1 1 1 185
2 2 3 130
3 3 1 190
4 1 2 120
5 2 1 155
6 3 2 145
7 1 1 135
8 1 1 205
9 2 3 200
10 1 3 150
11 2 2 110
12 3 3 250
I would like to have four top lists:
Bench Press
Parallel Squat
Power Clean
Total of the above 3
I can successfully grab a top list for each specific lift using the following query:
SELECT s.studentId, s.first, s.last, s.gender, s.weight, l.name, sl.weight
FROM Students s
LEFT JOIN (
SELECT *
FROM StudentLifts
ORDER BY weight DESC
) sl ON sl.studentId = s.studentId
LEFT JOIN Lifts l ON l.liftId = sl.liftId
WHERE l.name = 'Bench Press'
AND s.gender = 'm'
AND s.weight > 170
GROUP BY s.studentId
ORDER BY sl.weight DESC
However, I am stuck on how to add the highest total of each lift for each student. How can I first find the highest total for each student in each lift, and then add them up to get a total of all three lifts?
Edit
The result set that I am looking for would be something like:
-------------------------------------------------
studentId first last weight
-------------------------------------------------
3 John Doe3 585
1 John Doe 475
2 John Doe2 465
I also forgot to mention that I would actually like two lists, one for students above 170 and one for students below 170.
SELECT -- join student a total weight to the student table
A.studentId,
A.first,
A.last,
C.totalWeight
FROM
Student A,
(
SELECT -- for each studet add the max weights
sum(B.maxWeight) as totalWeight,
B.studentID
FROM (
SELECT -- for each (student,lift) select the max weight
max(weight) as maxWeight,
studentId,
liftID
FROM
StudentLifts
GROUP BY
studentId,
liftID
) B
GROUP BY
studentId
) C
WHERE
A.studentID = C.studentId
-- AND A.weight >= 170
-- AND A.weight < 170
-- pick one here to generate on of the two lists.
tab1
id code name
1001 0 palani
1002 1001 shanker
1003 1002 raghu
1004 1003 kabhir
1005 1003 vani
1006 1002 priya
tab2
id code name amount tax
1 1002 b 100 1
2 1002 j 20 10
3 1003 jk 23 20
4 1004 jk 675 9
5 1005 o 67 3
6 1003 u 122 4
7 1003 o 98 1
8 1003 iu 98 1
9 1002 po 4 0.4
10 1005 pl 1 0.1
12 1005 tf 1 0.1
24 1006 e 23 2.3
id 1001 see code 1001 corresponding id 1002
id 1002 see code 1002 corresponding id 1003,1006
id 1003 see code 1003 corresponding id 1004,1005
others no need
Result need like this
code amount tax
1001 124 11.4
1002 364 28.3
1003 744 12.2
Please send the mysql query for this
GROUP BY is your friend:
select tab1.id, sum (tab2.amount) amount, sum (tab2.tax) tax
from
tab1, tab2
where tab1.code = tab2.id
and tab1.id in (1001, 1002, 1003)
group by tab1.id
This will get what you want from tab2
SELECT code, SUM(amount), SUM(tax) FROM tab GROUP BY code ORDER BY code
I can't see how tab1 fits into what you want
does it work (not tested)
select SUM(t2.amount) AS Amount,SUM(t2.tax) AS totalTax,t2.code FROM
table2 t2
INNER JOIN table1 t1 ON t1.code=t2.code