Repository navigation
Expand file tree
/
Copy pathlibrary.sql
More file actions
63 lines (55 loc) · 2.11 KB
/
Copy pathlibrary.sql
File metadata and controls
63 lines (55 loc) · 2.11 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
-- ============================================================================
-- Task 2: Library database
-- Database: library (stored locally as the SQLite file library.db)
--
-- Run with:
-- python run_library.py
--
-- The runner creates library.db, executes every statement below in order, and
-- prints the result of each query labelled "-- OUTPUT:".
--
-- For MySQL, create and select the database first:
-- CREATE DATABASE library;
-- USE library;
-- ============================================================================
DROP TABLE IF EXISTS books;
CREATE TABLE books (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
author TEXT NOT NULL,
year INTEGER NOT NULL,
price DECIMAL(10, 2) NOT NULL CHECK (price >= 0)
);
INSERT INTO books (id, title, author, year, price) VALUES
(1, 'The Great Gatsby', 'F. Scott Fitzgerald', 1925, 450.00),
(2, 'Clean Code', 'Robert C. Martin', 2008, 850.00),
(3, 'The Pragmatic Programmer','Andrew Hunt and David Thomas', 1999, 720.00),
(4, 'To Kill a Mockingbird', 'Harper Lee', 1960, 390.00),
(5, 'Python Crash Course', 'Eric Matthes', 2019, 650.00),
(6, '1984', 'George Orwell', 1949, 299.00),
(7, 'Design Patterns', 'Erich Gamma et al.', 1994, 1250.00),
(8, 'The Hobbit', 'J. R. R. Tolkien', 1937, 510.00);
-- Query 1: display books priced above 500.
-- OUTPUT: Books priced above 500
SELECT id, title, author, year, price
FROM books
WHERE price > 500
ORDER BY price DESC;
-- Query 2: display all books sorted by price, highest first.
-- OUTPUT: All books sorted by price (highest first)
SELECT id, title, author, year, price
FROM books
ORDER BY price DESC;
-- Query 3: count the total number of books.
-- OUTPUT: Total number of books
SELECT COUNT(*) AS total_books
FROM books;
-- Query 4: update the price of one book using WHERE.
UPDATE books
SET price = 799.99
WHERE title = 'Clean Code';
-- Verify the update.
-- OUTPUT: Updated price for Clean Code
SELECT id, title, price
FROM books
WHERE title = 'Clean Code';