-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode for Tables.txt
More file actions
153 lines (125 loc) · 4.08 KB
/
Copy pathcode for Tables.txt
File metadata and controls
153 lines (125 loc) · 4.08 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
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
DROP TABLE IF EXISTS students;
CREATE TABLE students(
sid int not null,
name text not null,
primary key(sid)
);
CREATE TABLE teachers(
tid int not null,
name text not null,
primary key(tid)
);
CREATE TABLE subjects(
subid int not null,
name text not null,
primary key(subid)
);
DROP TABLE IF EXISTS grades;
CREATE TABLE grades(
studentID int not null references students(sid),
teacherID int not null references teachers(tid),
subjectID int not null references subjects(subid),
grade varchar(3),
primary key(studentID, teacherID, subjectID)
);
--
-- SQL as DML: Data Manipulation Language -- Add test data to the tables.
--
INSERT INTO students (sid, name) VALUES(1, 'ნატალი');
INSERT INTO students (sid, name) VALUES(2, 'მაია');
INSERT INTO students (sid, name) VALUES(3, 'თეონა');
INSERT INTO students (sid, name) VALUES(4, 'თამარი');
INSERT INTO students (sid, name) VALUES(5, 'დავითი');
INSERT INTO students (sid, name) VALUES(6, 'მარიტა');
INSERT INTO students (sid, name) VALUES(7, 'ელენე');
select *
from Students;
INSERT INTO teachers (tid, name) VALUES (1, 'ქეთინო');
INSERT INTO teachers (tid, name) VALUES (2, 'ლელა');
INSERT INTO teachers (tid, name) VALUES (3, 'ვლადიმერი');
INSERT INTO teachers (tid, name) VALUES (4, 'გიორგი');
select *
from teachers;
INSERT INTO subjects (subid, name) VALUES (1, 'ისტორია');
INSERT INTO subjects (subid, name) VALUES (2, 'ბიოლოგია');
INSERT INTO subjects (subid, name) VALUES (3, 'მათემატიკა');
INSERT INTO subjects (subid, name) VALUES (4, 'ქართული');
select *
from subjects;
INSERT INTO grades (studentID, teacherID, subjectID, grade) VALUES (1, 2, 1, 'A');
INSERT INTO grades (studentID, teacherID, subjectID, grade) VALUES (1, 2, 2, 'B');
INSERT INTO grades (studentID, teacherID, subjectID, grade) VALUES (7, 4, 3, 'C+');
INSERT INTO grades (studentID, teacherID, subjectID, grade) VALUES (7, 3, 2, 'F');
INSERT INTO grades (studentID, teacherID, subjectID, grade) VALUES (6, 2, 1, 'B+');
INSERT INTO grades (studentID, teacherID, subjectID, grade) VALUES (2, 4, 3, 'C');
INSERT INTO grades (studentID, teacherID, subjectID, grade) VALUES (3, 4, 3, 'B');
select *
from grades;
--
-- SQL as DML: Data Manipulation Language -- Queries to answer interesting quesstions about the data.
--
-- Students in order by name:
select *
from students
order by name ASC;
-- Names of students in any class taught by Adams:
select name
from students
where sid in
(select studentID
from grades
where teacherID in
(select tid
from teachers
where name = 'გიორგი')
);
-- Names of teachers who taught Biology:
select name
from teachers
where tid in
(select teacherID
from grades
where subjectID in
(select subid
from subjects
where name = 'ბიოლოგია')
);
-- Namaes of teachers who have not yet taught:
select name
from teachers
where tid not in
(select teacherID
from grades);
-- Names of students who have not yet taken any classes:
select name
from students
where sid not in
(select studentID
from grades);
-- Names of students in the same class:
select name
from students
where sid in
(SELECT studentID
FROM grades g1
WHERE
(SELECT COUNT(*)
FROM grades g2
WHERE g1.subjectID = g2.subjectID
AND g1.teacherID = g2.teacherID ) > 1
ORDER BY subjectID
);
select t.name as "მასწავლებელი",
sub.name as "საგანი",
s.name as "მოსწავლე"
from grades g1,
grades g2,
students s,
teachers t,
subjects sub
where g1.teacherID = g2.teacherID
and g1.subjectID = g2.subjectID
and g1.studentID = s.sid
and g1.teacherID = t.tid
and g1.subjectID = sub.subid
order by t.name, sub.name, s.name;