-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAssignment-7.sql
165 lines (127 loc) · 6.48 KB
/
Assignment-7.sql
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
CREATE DATABASE ASSIGN7;
USE ASSIGN7;
--import students.csv into database
SELECT * FROM STUDENTS;
--1a. Students who passed SQL or Excel but failed Python
select *,Student_Name from Students where SQL_Pass_Fail = 'Pass' or Excel_Pass_Fail = 'Pass'
intersect
select *, Student_Name from Students where Python_pass_Fail = 'Fail';
--1b. Students who passed Python or Excel but failed SQL
select *,Student_Name from Students where Python_Pass_Fail = 'Pass' or Excel_Pass_Fail = 'Pass'
intersect
select *, Student_Name from Students where SQL_pass_Fail = 'Fail';
--1c. Students who passed SQL or Python but failed Excel
select *,Student_Name from Students where Python_Pass_Fail = 'Pass' or SQL_Pass_Fail = 'Pass'
intersect
select *, Student_Name from Students where Excel_pass_Fail = 'Fail';
--1d. Students who failed SQL or Excel but passed Python
select *,Student_Name from Students where Excel_Pass_Fail = 'Fail' or SQL_Pass_Fail = 'Fail'
intersect
select *, Student_Name from Students where Python_pass_Fail = 'Pass';
--1e. Students who failed Python or Excel but passed SQL
select *,Student_Name from Students where Python_Pass_Fail = 'Fail' or Excel_Pass_Fail = 'Fail'
intersect
select *, Student_Name from Students where SQL_pass_Fail = 'Pass';
--1f. Students who failed SQL or Python but passed Excel
select *,Student_Name from Students where Python_Pass_Fail = 'Fail' or SQL_Pass_Fail = 'Fail'
intersect
select *, Student_Name from Students where Excel_pass_Fail = 'Pass';
---22. Write a query to add a grade column in a given table. Allot grade to each
--student according to their score given in the table.
--Grades : A+, A, A-, B+, B, B-, C+, C-, C, D+, D, F( Score >=93 A+, Score < 60 F)
Create table marks(
ID INT,
NAME VARCHAR(10),
SCORE INT);
SELECT * FROM MARKS;
Insert into marks values(1,'Simisola',60),(2,'Ivan',80),(3,'Metodija',52),(4,'Callum',98),
(5,'Leia',84),(6,'Aparecida',82),(7,'Ursula',69),(8,'Ramazan',78),
(9,'Corona',87),(10,'Alise',57),(11,'Galadriel',89),(12,'Merel',99),
(13,'Cherice',55),(14,'Nithya',81),(15,'Elsad',71),(16,'Liisi',90),
(17,'Johanna',90),(18,'Anfisa',90),(19,'Ryosuke',97),(20,'Sakchai',61),
(21,'Elbert',63),(22,'Katelyn',51);
Select *,case when score >=93 then 'A+'when score >=90 then 'A'
when score >= 87 then 'A-'when score >=85 then 'B+'
when score >=83 then 'B'when score >=80 then 'B-'
when score >=77 then 'C+'when score >=75 then 'C'
when score >=73 then 'D+'when score >=70 then 'D'
when score >= 60 then 'D-'when score < 60 then 'F'
end as Grade from MARKS;
--2.a Create a table from Marks table, containing the number of students who
--passed and failed according to their score.
Create table pass_fail(
result varchar(10),
NUMBER_OF_STUDENT INT);
INSERT INTO pass_fail
select case
when Score >= 60 then 'Pass'
when Score < 60 then 'Fail'
else NULL end as RESULT, COUNT(*) as NUMBER_OF_STUDENT from Marks
group by
case
when Score >= 60 then 'Pass'
when Score < 60 then 'Fail'
else NULL end;
select * from pass_fail;
--2.b create a table from marks, which have the following columns
Create table GRADES(
GRADE varchar(10),
NUMBER_OF_STUDENTS INT);
INSERT INTO GRADES
Select case when score >=93 then 'A+'when score >=90 then 'A'
when score >= 87 then 'A-'when score >=85 then 'B+'
when score >=83 then 'B'when score >=80 then 'B-'
when score >=77 then 'C+'when score >=75 then 'C'
when score >=73 then 'D+'when score >=70 then 'D'
when score >= 60 then 'D-'when score < 60 then 'F'
end as GRADE, COUNT(*) AS NUMBER_OF_STUDENTS from Marks
Group by case when score >=93 then 'A+'when score >=90 then 'A'
when score >= 87 then 'A-'when score >=85 then 'B+'
when score >=83 then 'B'when score >=80 then 'B-'
when score >=77 then 'C+'when score >=75 then 'C'
when score >=73 then 'D+'when score >=70 then 'D'
when score >= 60 then 'D-'when score < 60 then 'F'
end;
SELECT * FROM GRADES;
--3a. Students whose names start with A and who joined in march
select *,case
when Student_Name like 'A%' and Month(Joining_Date) = 3 then Student_Name
end as New from Students;
--3b. Students having C in their name and at least 7 characters in their name.
select *, case
when Student_Name like '%C%' and len(Student_Name)>=7 then Student_Name
end As New_Students from Students;
--3c. In which month the maximum number of students registered
select top 1 MONTH(Joining_Date) as Month,case
when Month(Joining_Date)=1 then count(*)
when Month(Joining_Date)=2 then count(*)
when Month(Joining_Date)=3 then count(*)
when Month(Joining_Date)=4 then count(*)
when Month(Joining_Date)=5 then count(*)
when Month(Joining_Date)=6 then count(*)
when Month(Joining_Date)=7 then count(*)
when Month(Joining_Date)=8 then count(*)
when Month(Joining_Date)=9 then count(*)
when Month(Joining_Date)=10 then count(*)
when Month(Joining_Date)=11 then count(*)
when Month(Joining_Date)=12 then count(*)
end As Total_Registration from Students group by Month(Joining_Date) order by Count(*) desc;
--3d. Student with minimum duration(days, and months) in EXCEL, SQL and python.
select top 1 Student_Name, case
when datediff(day,Joining_Date,Leaving_Date) = 0 then Student_Name else 1
end AS Min_Duration from Students order by datediff(day,Joining_Date,Leaving_Date) asc ;
--3e. Section wise average duration (days and months) of students in course.
--Day
select section, case
when section = 'A' then avg(Datediff(day,Joining_Date,Leaving_Date))
when section = 'B' then avg(Datediff(day,Joining_Date,Leaving_Date))
when section = 'C' then avg(Datediff(day,Joining_Date,Leaving_Date))
when section = 'D' then avg(Datediff(day,Joining_Date,Leaving_Date))
END AS AVG_DURATION_IN_DAYS from Students group by Section;
--Month
select section, case
when section = 'A' then avg(Datediff(Month,Joining_Date,Leaving_Date))
when section = 'B' then avg(Datediff(Month,Joining_Date,Leaving_Date))
when section = 'C' then avg(Datediff(Month,Joining_Date,Leaving_Date))
when section = 'D' then avg(Datediff(Month,Joining_Date,Leaving_Date))
END AS AVG_DURATION_IN_MONTH from Students group by Section;