-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_Michailidis.txt
215 lines (167 loc) · 5.22 KB
/
query_Michailidis.txt
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
-- A All the records from a table
select *
from books;
-- B A projection with 3 columns
select fname, lname, gender
from authors
Order by fname, lname, gender desc;
-- C Restriction Queries
select *
from books
where genre = 'HORROR';
select *
from borrowers
where lname like 'J%SON%';
select *
from authors
where country_origin not like '%GREECE%';
select *
from copies
WHERE pur_date >= '01-MAY-2017' AND pur_date <= '01-MAY-2019';
-- D Join Queries
-- d two tables
select *
from loans l join copies c on l.cop_id= c.cop_id
-- d three tables
select *
from loans l join copies c on l.cop_id = c.cop_id join books b on c.book_isbn = b.isbn;
-- d more tables
select *
from (authors a join books b on a.id = b.author_id join copies c on b.isbn = c.book_isbn) left outer join loans l on c.cop_id = l.cop_id;
-- E Aggregate Functions
--e1
select * from authors
where lname like 'K%G%' ;
--e2
select genre, count(*) from books
group by genre ;
--e3
select book_isbn, count(*)
from loans l join copies c on l.cop_id= c.cop_id
group by book_isbn
having count(*) > = all (select count(*)
from loans l join copies c on l.cop_id= c.cop_id
group by book_isbn);
--e4
select *
from authors a join books b on a.id = b.author_id
order by a.lname;
--e5
select COUNT (*)
from books
where to_char(pub_date, 'YYYY') LIKE '1981'
--e6
select a.fname, a.lname, b.genre
from authors a join books b on a.id = b.author_id
where b.genre = 'SCI-FI' or b.genre = 'HORROR';
--e7
select b.title
from (loans l join copies c on l.cop_id = c.cop_id) join books b on c.book_isbn = b.isbn
where to_char(pub_date, 'MON') LIKE 'OCT';
--e8
select *
from (select count(*) dead
from authors
where date_death is not null) d_authors, (select count(*) alive
from authors
where date_death is null) a_authors;
--e9
select fname, lname
from authors
where date_death is not null and date_death - date_birth = (select max(date_death - date_birth) from authors where date_death is not null);
--e10
select fname, lname, date_birth
from authors
where date_birth = (select min(date_birth) from authors where date_death is null);
--e11
select a.lname, count(*)
from authors a join books b on a.id = b.author_id
group by a.lname;
--e12
select a.fname, a.lname
from (authors a join books b on a.id = b.author_id join copies c on b.isbn = c.book_isbn) left outer join loans l on c.cop_id = l.cop_id
where l.loan_date is null;
--e13
select b.isbn, b.title, b.genre
from (books b join copies c on b.isbn = c.book_isbn) left outer join loans l on c.cop_id = l.cop_id
where l.loan_date is null;
--e14
select to_char(pub_date, 'YYYY'), sum(price)
from books
group by to_char(pub_date, 'YYYY')
order by to_char(pub_date, 'YYYY') asc;
--e15
select gender, sum(price)
from books b join authors a on a.id = b.author_id
group by gender;
--e16
select isbn, count(*)
from books b join copies c on b.isbn = c.book_isbn
group by isbn
having count(*) > = all (select count(*) "Total Copies"
from books b join copies c on b.isbn = c.book_isbn
group by isbn);
--e17
select *
from authors
where country_origin not like 'ENGLAND';
--e18
select bor.fname, bor.lname
from books b join copies c on b.isbn = c.book_isbn join loans l on c.cop_id = l.cop_id join borrowers bor on l.b_id = bor.id
where b.title like 'BLUES FOR A BLACK CAT';
--e19
select a.fname, sum(n_pages) "SUM OF PAGES"
from authors a join books b on a.id = b.author_id
group by a.fname
order by sum(n_pages) desc;
--e20
select b.isbn, b.title
from authors a join books b on a.id = b.author_id
where a.country_origin like 'ENGLAND' and b.price = (select max(b.price) from authors a join books b on a.id = b.author_id where a.country_origin like 'ENGLAND');
-- F Bonus
--f1
with temp1 as
(select bor.id, sum(b.n_pages) as sum_pages from books b join copies c on b.isbn = c.book_isbn join loans l on c.cop_id = l.cop_id join borrowers bor on l.b_id = bor.id
group by bor.id)
select bor.id, bor.fname, bor.lname, sum_pages from temp1 t join borrowers bor on t.id = bor.id
where sum_pages >= all (select max(sum_pages) from temp1);
--f2
with temp1 as
(select b_id, count(*) as total_loans from loans
group by b_id)
select bor.id, bor.fname, bor.lname, total_loans from temp1 t join borrowers bor on t.b_id = bor.id
where total_loans >= all (select max(total_loans) from temp1);
--f3
select bor.gender, count(*) as num_loans from loans l join borrowers bor on l.b_id = bor.id
group by bor.gender
--f4
select fname, lname, concat(SUBSTR(fname,0,3), SUBSTR(lname,0,4)) as username
from borrowers;
--f5
select loan_date, loan_date+14 as Return_Date
from loans;
--f6
with temp1 as
(select author_id, sum(n_pages) as sum_pages from books group by author_id)
select author_id, a.fname, a.lname from temp1 t join authors a on t.author_id = a.id
where sum_pages >= all (select max(sum_pages) from temp1);
--f7
select genre, count(*)
from books
group by genre
having count(*) >= all (select count(*)
from books
group by genre);
--f8
with temp1 as
(select to_char (loan_date, 'YYYY') as year, COUNT(*) as n_loans from loans group by to_char (loan_date, 'YYYY'))
select year from temp1
where n_loans >= all (select max(n_loans) from temp1);
--f9
select *
from books b join authors a on b.author_id = a.id
where a.country_origin like 'GREECE';
-- f10
select *
from books
where title like '%MISERY%';