This assignment focuses on PostgreSQL database operations. You will create and manage three tables (books, customers, orders), insert sample data, and perform essential SQL queries. Key tasks include CRUD operations, constraints, JOINs, aggregations, filtering, and data manipulation to reinforce your understanding of relational databases.
1οΈβ£ Install PostgreSQL on your system if it is not already installed.
2οΈβ£ Open pgAdmin or your PostgreSQL terminal.
3οΈβ£ Create a new database named "bookstore_db" or any appropriate name.
4οΈβ£ Connect to the newly created database.
1οΈβ£ Create a "books" table with the following fields:
id
(Primary Key): A unique identifier for each book.title
: The name of the book.author
: The author of the book.price
: The price of the book (must be non-negative, enforced via CHECK constraint).stock
: The number of available copies in stock.published_year
: The year the book was published.
2οΈβ£ Create a "customers" table with the following fields:
id
(Primary Key): A unique identifier for each customer.name
: The full name of the customer.email
: The email address of the customer (must be unique).joined_date
: The date the customer registered (default to the current date).
3οΈβ£ Create an "orders" table with the following fields:
id
(Primary Key): A unique identifier for each order.customer_id
(Foreign Key): References theid
field in the "customers" table.book_id
(Foreign Key): References theid
field in the "books" table.quantity
: The number of books ordered (must be greater than zero).order_date
: The date and time when the order was placed (default to the current timestamp).
| id | title | author | price | stock | published_year |
| --- | ---------------------------- | ----------------- | ----- | ----- | -------------- |
| 1 | The Pragmatic Programmer | Andrew Hunt | 40.00 | 10 | 1999 |
| 2 | Clean Code | Robert C. Martin | 35.00 | 5 | 2008 |
| 3 | You Don't Know JS | Kyle Simpson | 30.00 | 8 | 2014 |
| 4 | Refactoring | Martin Fowler | 50.00 | 3 | 1999 |
| 5 | Database Design Principles | Jane Smith | 20.00 | 0 | 2018 |
| id | name | email | joined_date |
| --- | ------- | ------------------ | ------------ |
| 1 | Alice | alice@email.com | 2023-01-10 |
| 2 | Bob | bob@email.com | 2022-05-15 |
| 3 | Charlie | charlie@email.com | 2023-06-20 |
| id | customer_id | book_id | quantity | order_date |
| --- | ----------- | ------- | -------- | ----------- |
| 1 | 1 | 2 | 1 | 2024-03-10 |
| 2 | 2 | 1 | 1 | 2024-02-20 |
| 3 | 1 | 3 | 2 | 2024-03-05 |
1οΈβ£ Find books that are out of stock.
Sample Output:
title
----------------------------
Database Design Principles
2οΈβ£ Retrieve the most expensive book in the store.
Sample Output:
| id | title | author | price | stock | published_year |
| --- | ----------- | ------------- | ----- | ----- | -------------- |
| 4 | Refactoring | Martin Fowler | 50.00 | 3 | 1999 |
3οΈβ£ Find the total number of orders placed by each customer.
Sample Output:
| name | total_orders |
| ------- | ------------ |
| Alice | 2 |
| Bob | 1 |
4οΈβ£ Calculate the total revenue generated from book sales.
Sample Output:
total_revenue
-----------------
135.00
5οΈβ£ List all customers who have placed more than one order.
Sample Output:
| name | orders_count |
| ------- | ------------ |
| Alice | 2 |
6οΈβ£ Find the average price of books in the store.
Sample Output:
avg_book_price
----------------------------
35.00
7οΈβ£ Increase the price of all books published before 2000 by 10%.
Sample Output: (No table output, but affected rows will be updated accordingly.)
| id | title | author | price | stock | published_year |
|-----|------------------------------|--------------------|--------|-------|----------------|
| 1 | The Pragmatic Programmer | Andrew Hunt | 44.00 | 10 | 1999 |
| 2 | Clean Code | Robert C. Martin | 35.00 | 5 | 2008 |
| 3 | You Don't Know JS | Kyle Simpson | 30.00 | 8 | 2014 |
| 4 | Refactoring | Martin Fowler | 55.00 | 3 | 1999 |
| 5 | Database Design Principles | Jane Smith | 20.00 | 0 | 2018 |
8οΈβ£ Delete customers who haven't placed any orders.
Sample Output: (No table output, but affected rows will be removed accordingly.)
| id | name | email | joined_date |
| --- | ------- | ------------------ | ------------ |
| 1 | Alice | alice@email.com | 2023-01-10 |
| 2 | Bob | bob@email.com | 2022-05-15 |
1οΈβ£ Prepare a document that includes:
- SQL code for table creation, sample data insertion, and all queries.
- Comments explaining the purpose of each query.
2οΈβ£ Save your file as "PostgreSQL_Assignment.sql" or another appropriate name.
- What is PostgreSQL?
- What is the purpose of a database schema in PostgreSQL?
- Explain the Primary Key and Foreign Key concepts in PostgreSQL.
- What is the difference between the
VARCHAR
andCHAR
data types? - Explain the purpose of the
WHERE
clause in aSELECT
statement. - What are the
LIMIT
andOFFSET
clauses used for? - How can you modify data using
UPDATE
statements? - What is the significance of the
JOIN
operation, and how does it work in PostgreSQL? - Explain the
GROUP BY
clause and its role in aggregation operations. - How can you calculate aggregate functions like
COUNT()
,SUM()
, andAVG()
in PostgreSQL?
- Ensure your SQL queries execute successfully before submission.
- Add comments to explain complex queries where necessary.
- Maintain clean formatting and proper indentation for better readability.
Date | Marks | Deadline Time |
---|---|---|
24 March, 2025 | 60 Marks | Until 11:59 PM |
25 March, 2025 | 50 Marks | Until 11:59 PM |
After 25 October, 2025 | 30 Marks | Until 11:59 PM |
πΉ Submit only the GitHub repository link containing your solution file.
Participation in this assignment is mandatory for all students. It lays the foundation for upcoming assignments focused on PostgreSQL and Prisma, designed for those aspiring to excel as full-stack developers.
Approach this task with dedication, precision, and a commitment to excellence. Plagiarism will not be tolerated. Please make sure that the code you submit is your work. Any instance of plagiarism will result in a score of 0. Additionally, if any AI-generated content or ChatGPT-generated responses are detected, the score will also be 0. Best of luck! π‘π₯