-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries.sql
More file actions
63 lines (51 loc) · 2.01 KB
/
queries.sql
File metadata and controls
63 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- It Shows each student with their marks in every subject
SELECT s.Name AS Student_Name,
sub.SubjectName AS Subject,
m.Marks AS Marks
FROM Students s
JOIN Marks m ON s.StudentID = m.StudentID -- connect student with their marks
JOIN Subjects sub ON m.SubjectID = sub.SubjectID -- get subject name for each mark
ORDER BY s.StudentID, sub.SubjectID; -- keep data neat and ordered
-- Find total marks scored by each student
SELECT s.Name AS Student_Name,
SUM(m.Marks) AS Total_Marks
FROM Students s
JOIN Marks m ON s.StudentID = m.StudentID
GROUP BY s.StudentID; -- one total per student
-- Calculate average marks of every student
SELECT s.Name AS Student_Name,
ROUND(AVG(m.Marks), 2) AS Average_Marks
FROM Students s
JOIN Marks m ON s.StudentID = m.StudentID
GROUP BY s.StudentID;
-- Show students whose total marks are above the limit
SELECT s.Name AS Student_Name,
SUM(m.Marks) AS Total_Marks
FROM Students s
JOIN Marks m ON s.StudentID = m.StudentID
GROUP BY s.StudentID
HAVING SUM(m.Marks) > 450; -- filter by total marks
-- Get the highest mark scored in each subject
SELECT sub.SubjectName AS Subject,
MAX(m.Marks) AS Highest_Marks
FROM Marks m
JOIN Subjects sub ON m.SubjectID = sub.SubjectID
GROUP BY sub.SubjectID;
-- Temporary result to store total and average marks of students
WITH StudentTotals AS (
SELECT s.StudentID,
s.Name,
SUM(m.Marks) AS Total_Marks,
ROUND(AVG(m.Marks), 2) AS Average_Marks
FROM Students s
JOIN Marks m ON s.StudentID = m.StudentID
GROUP BY s.StudentID
)
SELECT *
FROM StudentTotals
WHERE Average_Marks > 85; -- show only good performers
-- Find students who failed in at least one subject
SELECT DISTINCT s.Name AS Student_Name
FROM Students s
JOIN Marks m ON s.StudentID = m.StudentID
WHERE m.Marks < 40; -- marks below pass level