0% found this document useful (0 votes)
30 views9 pages

SetA 1 Unlocked

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views9 pages

SetA 1 Unlocked

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Indian Institute of Information Technology, Allahabad

Database Management System (DBMS)


(Faculty: Dr. Ranjana Vyas)

SET A
Q1) Table: Weather

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| recordDate | date |
| temperature | int |
+---------------+---------+
id is the column with unique values for this table.
There are no different rows with the same recordDate.
This table contains information about the temperature on a certain day.

Write a solution to find all dates' Id with higher temperatures compared to its previous dates (yesterday).

Print the result table in any order.


Write the query and show the output on the given table below

Weather table:
+----+------------+-------------+
| id | recordDate | temperature |
+----+------------+-------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+----+------------+-------------+ (5 marks)

Q2). SQL Schema


Table: Customer

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| customer_id | int |
| product_key | int |
+-------------+---------+
This table may contain duplicate rows.
customer_id is not NULL.
product_key is a foreign key (reference column) to Product table.
Table: Product

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| product_key | int |
+-------------+---------+
product_key is the primary key (column with unique values) for this table.

Write a solution to report the customer IDs from the Customer table that bought all the products in the Product table.

Input:
Customer table:
+-------------+-------------+
| customer_id | product_key |
+-------------+-------------+
| 1 | 5 |
| 2 | 6 |
| 3 | 5 |
| 3 | 6 |
| 1 | 6 |
+-------------+-------------+
Product table:
+-------------+
| product_key |
+-------------+
| 5 |
| 6 |
+-------------+

(5 marks)

Q3) SQL Schema


Table: Users

+----------------+---------+
| Column Name | Type |
+----------------+---------+
| user_id | int |
| join_date | date |
| favorite_brand | varchar |
+----------------+---------+
user_id is the primary key (column with unique values) of this table.
This table has the info of the users of an online shopping website where users can sell and buy items.
Table: Orders

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| order_id | int |
| order_date | date |
| item_id | int |
| buyer_id | int |
| seller_id | int |
+---------------+---------+
order_id is the primary key (column with unique values) of this table.
item_id is a foreign key (reference column) to the Items table.
buyer_id and seller_id are foreign keys to the Users table.

Table: Items

+---------------+---------+
| Column Name | Type |
+---------------+---------+
| item_id | int |
| item_brand | varchar |
+---------------+---------+
item_id is the primary key (column with unique values) of this table.

Write a solution to find for each user, the join date and the number of orders they made as a buyer in 2019
Users table:
+---------+------------+----------------+
| user_id | join_date | favorite_brand |
+---------+------------+----------------+
| 1 | 2018-01-01 | Lenovo |
| 2 | 2018-02-09 | Samsung |
| 3 | 2018-01-19 | LG |
| 4 | 2018-05-21 | HP |
+---------+------------+----------------+
Orders table:
+----------+------------+---------+----------+-----------+
| order_id | order_date | item_id | buyer_id | seller_id |
+----------+------------+---------+----------+-----------+
| 1 | 2019-08-01 | 4 | 1 | 2 |
| 2 | 2018-08-02 | 2 | 1 | 3 |
| 3 | 2019-08-03 | 3 | 2 | 3 |
| 4 | 2018-08-04 | 1 | 4 | 2 |
| 5 | 2018-08-04 | 1 | 3 | 4 |
| 6 | 2019-08-05 | 2 | 2 | 4 |
+----------+------------+---------+----------+-----------+

Items table:
+---------+------------+
| item_id | item_brand |
+---------+------------+
| 1 | Samsung |
| 2 | Lenovo |
| 3 | LG |
| 4 | HP |
+---------+------------+ (5 Marks)
Q4) Scenario:
You're managing a library database with the following tables:

1. Books:
● Columns: book_id (Primary Key), title, author, genre, publication_year, quantity_in_stock
2. Authors:
● Columns: author_id (Primary Key), author_name, nationality

Books Table:
book_i title author genre publication_yea quantity_in_stoc
d r k
1 The Catcher in the J.D. Fiction 1951 20
Rye Salinger
2 Pride and Prejudice Jane Romanc 1813 15
Austen e
3 To Kill a Harper Lee Drama 1960 25
Mockingbird

Authors Table:
author_i author_nam nationalit
d e y
1 J.D. American
Salinger
2 Jane Austen British
3 Harper Lee American
Questions:

1. Create a PL/SQL procedure to display all books published after the year 2000.
2. Write a PL/SQL block to update the quantity of a book in stock after a return. Assume a book's book_id and the number of copies
returned as inputs.
3. Develop a PL/SQL function to calculate the average publication year of books in a given genre. The function should take the genre as
input and return the average publication year.
4. Design a PL/SQL trigger to automatically update the quantity_in_stock column of the Books table whenever a new book is added to the
inventory.
5. Implement a PL/SQL function to find the total number of books authored by an author. The function should take an author_id as input
and return the count of books.
6. Create a PL/SQL procedure to delete an author from the Authors table based on their author_id. Ensure that all records related to the
author are removed from associated tables to maintain data integrity.

(12 Marks)

Q5) You are managing a university database with the following tables:

1. Students:
● Columns: student_id (Primary Key), name, age, major, GPA
2. Courses:
● Columns: course_id (Primary Key), title, department, instructor
Students Table:
student_i name age major GP
d A
1 Alice Johnson 20 Computer 3.75
Science
2 Bob Smith 22 Engineering 3.50
3 Charlie 21 Mathematics 3.90
Brown

Courses Table:
course_i title department instructor
d
101 Introduction to Computer Dr. Smith
Programming Science
102 Electrical Circuits Engineering Dr.
Johnson
103 Calculus I Mathematics Dr. Brown

These tables represent the students enrolled in the university along with their details such as student ID, name, age, major, and GPA. Additionally, there's a
table listing various courses offered by the university, including details such as course ID, title, department, and instructor.
1) Write a PL/SQL block to display the names and majors of students who have a GPA higher than 3.5.
2) Write a PL/SQL block to calculate the average age of students in each department. (8 Marks)

You might also like