-
Notifications
You must be signed in to change notification settings - Fork 0
/
labb1.sql
122 lines (100 loc) · 2.96 KB
/
labb1.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
select firstName, lastName
from Students;
select firstName, lastName
from Students
order by lastName, firstName;
select *
from Students
where pNbr like '75%';
select firstName, lastName, pNbr
where mod(substr(pNbr, 10, 1), 2) = 0;
select count(*)
from Students;
select *
from Courses
where courseCode like 'FMA%';
select *
from Courses
where credits > 5;
select courseCode
from TakenCourses
where pNbr = '790101-1234';
select courseName, credits
from Courses
join TakenCourses on (TakenCourses.courseCode = Courses.courseCode)
where TakenCourses.pNbr = '790101-1234';
select sum(credits)
from Courses c
join TakenCourses tc on (tc.courseCode = c.courseCode)
where tc.pNbr = '790101-1234';
select avg(tc.grade)
from Courses c
join TakenCourses tc on (tc.courseCode = c.courseCode)
where tc.pNbr = '790101-1234';
select courseCode
from TakenCourses tc
join Students s on (s.pNbr = tc.pNbr)
where s.firstName = 'Eva' and s.lastName = 'Alm';
select courseName, credits
from Courses c
join TakenCourses tc on (tc.courseCode = c.courseCode)
join Students s on (tc.pNbr = s.pNbr)
where s.firstName = 'Eva' and s.lastName = 'Alm';
select sum(credits)
from Courses c
join TakenCourses tc on (tc.courseCode = c.courseCode)
join Students s on (tc.pNbr = s.pNbr)
where s.firstName = 'Eva' and s.lastName = 'Alm';
select avg(tc.grade)
from Courses c
join TakenCourses tc on (tc.courseCode = c.courseCode)
join Students s on (tc.pNbr = s.pNbr)
where s.firstName = 'Eva' and s.lastName = 'Alm';
select s.firstName, s.lastName, sum(COALESCE(c.credits, 0)) as c_sum
from Students s
left join TakenCourses tc on (s.pNbr = s.pNbr)
join Courses c on(tc.courseCode = c.courseCode)
group by s.firstName, s.lastName
having c_sum = 0;
select s.firstName, s.lastName, avg(tc.grade) as g_avg
from Students s
join TakenCourses tc on (tc.pNbr = s.pnBr)
order by g_avg DESC
limit 0, 5;
select s.pNbr, sum(COALESCE(c.credits, 0))
from Students s
left join TakenCourses tc on (s.pNbr = s.pNbr)
join Courses c on(tc.courseCode = c.courseCode)
group by s.firstName, s.lastName;
select s.firstName, s.lastName, sum(COALESCE(c.credits, 0))
from Students s
left join TakenCourses tc on (s.pNbr = s.pNbr)
join Courses c on(tc.courseCode = c.courseCode)
group by s.firstName, s.lastName;
select s.firstName, s.lastName, s.pNbr
from Students s
join Students ss on (s.firstName = ss.firstName and s.lastName = ss.lastName and s.pNbr != ss.pNbr);
create table Students (
pNbr
char(11),
firstName varchar(20) not null,
lastName varchar(20) not null,
primary key (pNbr)
);
create table Courses (
courseCode char(6),
courseName varchar(50) not null,
credits
integer not null check (credits > 0),
primary key (courseCode)
);
create table TakenCourses (
pNbr
char(11),
courseCode char(6),
grade
integer not null check (grade >= 3 and grade <= 5),
primary key (pNbr, courseCode),
foreign key (pNbr) references Students(pNbr),
foreign key (courseCode) references Courses(courseCode)
);