-
Notifications
You must be signed in to change notification settings - Fork 2
/
library.sql
583 lines (550 loc) · 12.5 KB
/
library.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
-- Active: 1731863607241@@127.0.0.1@3306@library
CREATE DATABASE library;
USE library;
DROP TABLE IF EXISTS books;
DROP TABLE IF EXISTS members;
DROP Table IF EXISTS book_issues;
CREATE Table books (
book_id int PRIMARY KEY,
title VARCHAR(250) NOT NULL,
author VARCHAR(50) NOT NULL,
genre VARCHAR(50),
price DECIMAL(10, 2) DEFAULT 500.00
);
CREATE Table members (
member_id int PRIMARY KEY,
name VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE book_issues (
issue_id int PRIMARY KEY,
member_id int,
book_id int,
issued_date DATE,
Foreign Key (member_id) REFERENCES members (member_id) on DELETE CASCADE,
Foreign Key (book_id) REFERENCES books (book_id) on DELETE CASCADE
);
INSERT INTO
books (
book_id,
title,
author,
genre,
price
)
VALUES (
1,
'To Kill a Mockingbird',
'Harper Lee',
'Fiction',
350.00
),
(
2,
'1984',
'George Orwell',
'Dystopian',
400.00
),
(
3,
'The Great Gatsby',
'F. Scott Fitzgerald',
'Classic',
300.00
),
(
4,
'Moby Dick',
'Herman Melville',
'Adventure',
450.00
),
(
5,
'Pride and Prejudice',
'Jane Austen',
'Romance',
320.00
),
(
6,
'The Catcher in the Rye',
'J.D. Salinger',
'Classic',
280.00
),
(
7,
'Brave New World',
'Aldous Huxley',
'Dystopian',
380.00
),
(
8,
'The Hobbit',
'J.R.R. Tolkien',
'Fantasy',
420.00
),
(
9,
'War and Peace',
'Leo Tolstoy',
'Historical Fiction',
500.00
),
(
10,
'Anna Karenina',
'Leo Tolstoy',
'Romance',
450.00
),
(
11,
'Crime and Punishment',
'Fyodor Dostoevsky',
'Philosophical Fiction',
400.00
),
(
12,
'The Alchemist',
'Paulo Coelho',
'Fiction',
350.00
),
(
13,
'The Kite Runner',
'Khaled Hosseini',
'Drama',
360.00
),
(
14,
'One Hundred Years of Solitude',
'Gabriel García Márquez',
'Magical Realism',
390.00
),
(
15,
'The Road',
'Cormac McCarthy',
'Post-Apocalyptic',
310.00
),
(
16,
'Life of Pi',
'Yann Martel',
'Adventure',
330.00
),
(
17,
'The Book Thief',
'Markus Zusak',
'Historical Fiction',
370.00
),
(
18,
'The Da Vinci Code',
'Dan Brown',
'Mystery',
400.00
),
(
19,
'Angels & Demons',
'Dan Brown',
'Thriller',
380.00
),
(
20,
'Harry Potter and the Sorcerer\'s Stone',
'J.K. Rowling',
'Fantasy',
450.00
),
(
21,
'Harry Potter and the Chamber of Secrets',
'J.K. Rowling',
'Fantasy',
460.00
),
(
22,
'The Hunger Games',
'Suzanne Collins',
'Dystopian',
350.00
),
(
23,
'Catching Fire',
'Suzanne Collins',
'Dystopian',
360.00
),
(
24,
'Mockingjay',
'Suzanne Collins',
'Dystopian',
370.00
),
(
25,
'Divergent',
'Veronica Roth',
'Dystopian',
340.00
),
(
26,
'The Fault in Our Stars',
'John Green',
'Romance',
300.00
),
(
27,
'Paper Towns',
'John Green',
'Mystery',
290.00
),
(
28,
'Looking for Alaska',
'John Green',
'Young Adult',
310.00
),
(
29,
'The Maze Runner',
'James Dashner',
'Science Fiction',
380.00
),
(
30,
'The Shining',
'Stephen King',
'Horror',
410.00
);
INSERT INTO
members (member_id, name, email)
VALUES (
1,
'John Doe',
),
(
2,
'Jane Smith',
),
(
3,
'Robert Brown',
),
(
4,
'Emily Davis',
),
(
5,
'Michael Johnson',
),
(
6,
'Sarah Wilson',
),
(
7,
'David Miller',
),
(
8,
'Linda Taylor',
),
(
9,
'James Anderson',
),
(
10,
'Patricia Thomas',
),
(
11,
'Christopher Lee',
),
(
12,
'Barbara Moore',
),
(
13,
'Daniel Jackson',
),
(
14,
'Elizabeth Harris',
),
(
15,
'Paul Clark',
),
(
16,
'Jessica Lewis',
),
(
17,
'Andrew Walker',
),
(
18,
'Karen Young',
),
(
19,
'Joshua Allen',
),
(
20,
'Nancy King',
),
(
21,
'Ryan Scott',
),
(
22,
'Laura Wright',
),
(
23,
'Kevin Green',
),
(
24,
'Ashley Adams',
),
(
25,
'Brian Baker',
),
(
26,
'Samantha Hall',
),
(
27,
'Jason Phillips',
),
(
28,
'Megan Carter',
),
(
29,
'Steven Mitchell',
),
(
30,
'Angela Perez',
);
INSERT INTO
book_issues (
issue_id,
member_id,
book_id,
issued_date
)
VALUES (1, 1, 2, '2024-11-01'),
(2, 2, 4, '2024-11-02'),
(3, 3, 1, '2024-11-03'),
(4, 4, 5, '2024-11-04'),
(5, 5, 3, '2024-11-05'),
(6, 6, 6, '2024-11-06'),
(7, 7, 8, '2024-11-07'),
(8, 8, 10, '2024-11-08'),
(9, 9, 9, '2024-11-09'),
(10, 10, 7, '2024-11-10'),
(11, 11, 11, '2024-11-11'),
(12, 12, 13, '2024-11-12'),
(13, 13, 15, '2024-11-13'),
(14, 14, 12, '2024-11-14'),
(15, 15, 14, '2024-11-15'),
(16, 16, 16, '2024-11-16'),
(17, 17, 18, '2024-11-17'),
(18, 18, 20, '2024-11-18'),
(19, 19, 17, '2024-11-19'),
(20, 20, 19, '2024-11-20'),
(21, 21, 21, '2024-11-01'),
(22, 22, 23, '2024-11-02'),
(23, 23, 24, '2024-11-03'),
(24, 24, 22, '2024-11-04'),
(25, 25, 25, '2024-11-05'),
(26, 26, 26, '2024-11-06'),
(27, 27, 27, '2024-11-07'),
(28, 28, 28, '2024-11-08'),
(29, 29, 29, '2024-11-09'),
(30, 30, 30, '2024-11-10');
-- showin the results of insert values:
SELECT * FROM book_issues;
SELECT * FROM members;
SELECT * FROM books;
DELETE FROM members WHERE member_id = 1;
UPDATE members SET name = 'sajid' WHERE member_id = 13;
UPDATE members
SET
email = '[email protected]'
WHERE
member_id = 13;
ALTER TABLE members ADD COLUMN age INT;
UPDATE members SET age = 24 WHERE member_id = 13;
-- updating the record
UPDATE books SET price = 870.00 WHERE book_id = 1;
UPDATE members
SET
age = CASE
WHEN member_id = 2 THEN 30
WHEN member_id = 3 THEN 40
WHEN member_id = 4 THEN 20
WHEN member_id = 5 THEN 35
WHEN member_id = 6 THEN 16
WHEN member_id = 7 THEN 29
WHEN member_id = 8 THEN 38
WHEN member_id = 9 THEN 34
WHEN member_id = 10 THEN 44
WHEN member_id = 11 THEN 47
WHEN member_id = 12 THEN 58
WHEN member_id = 13 THEN 24
WHEN member_id = 14 THEN 22
WHEN member_id = 15 THEN 27
WHEN member_id = 16 THEN 22
WHEN member_id = 17 THEN 24
WHEN member_id = 18 THEN 25
WHEN member_id = 19 THEN 45
WHEN member_id = 20 THEN 39
WHEN member_id = 21 THEN 36
WHEN member_id = 22 THEN 41
WHEN member_id = 23 THEN 38
WHEN member_id = 24 THEN 45
WHEN member_id = 25 THEN 29
WHEN member_id = 26 THEN 38
WHEN member_id = 27 THEN 46
WHEN member_id = 28 THEN 33
WHEN member_id = 29 THEN 43
WHEN member_id = 30 THEN 29
ELSE age
END;
-- using greater and less than operation
SELECT * FROM books WHERE price > 430;
SELECT * FROM book_issues WHERE member_id <= 11;
-- sum operation
SELECT SUM(price) AS total_price FROM books;
SELECT SUM(member_id) AS total_memberid FROM members;
-- Average price of books
SELECT AVG(price) AS average_price FROM books;
-- searching max min operation:
SELECT MAX(price) AS max_price FROM books;
SELECT MIN(price) AS min_price FROM books;
-- sorting ascending and descending
SELECT * FROM books ORDER BY price ASC;
SELECT * FROM book_issues ORDER BY member_id DESC;
-- searching for book with a specefic table
SELECT * FROM books WHERE book_id = 4;
SELECT email, age FROM members WHERE member_id = 13;
-- soritng memebers name from ascending order
SELECT * FROM members ORDER BY name ASC;
-- filterig email by specefic domain
SELECT * FROM members WHERE email LIKE '[email protected]';
-- sorting the issue_date using ascendig and descendig order
SELECT * FROM book_issues ORDER BY issued_date ASC;
SELECT * FROM book_issues ORDER BY issued_date DESC;
-- counitng the number of books from the count
SELECT COUNT(issued_date) AS books_count FROM book_issues;
-- searching books between range of price
SELECT * FROM books WHERE price BETWEEN 400 AND 650;
-- selecting distinct genre from books
SELECT DISTINCT genre FROM books;
-- distinct is a kind of word that actually eliminate the non repetitive values from the database
SELECT count(DISTINCT age) FROM members;
-- counting the unique genre from the book tables
SELECT COUNT(DISTINCT genre) AS unique_genre FROM books;
SELECT count(DISTINCT age) AS unique_age FROM members;
-- finding the earliest and latest issued date
DESCRIBE book_issues;
SELECT MIN(issued_date) AS earliest_issue_date FROM book_issues;
SELECT MAX(issued_date) AS latest_issue_date FROM book_issues;
-- aggregating the total number of books issued
SELECT member_id, COUNT(issue_id) AS total_issued
FROM book_issues
GROUP BY
member_id;
SELECT book_id, COUNT(issue_id) AS total_issued
FROM book_issues
GROUP BY
issue_id;
-- selecting books from there price and sort them as ascending and descending order
SELECT * FROM books ORDER BY price ASC;
SELECT * FROM books ORDER BY price DESC;
-- counting the issued books for specefic member
SELECT COUNT(*) AS issue_count FROM book_issues WHERE member_id = 4;
-- Finding books with null genre
SELECT * FROM books WHERE genre is NULL;
-- Using case to conditional statments
-- *** powerful use for to handel conditional case
SELECT
book_id,
title,
author,
price,
CASE
WHEN price <= 300 THEN 'affordable'
WHEN price BETWEEN 320 AND 450 THEN 'Quite costly'
ELSE 'Expensive'
END as price_categorization
FROM books;