You are on page 1of 3

ST.

XAVIER'S COLLEGE
B.Sc Computer Science Hons [2nd Year]
SQL Assignment Sheet

1. Consider the following relational schema for a library database:


Book (Bookid, Title, Publisher_name)
Book_authors (Bookid,Author_name)
Publisher (Name, Address, Phone)
Book_copies (Bookid, Branchid, No_of_copies)
Book_loans (Bookid, Branchid, Cardno, Date_out, Due_date)
Library_branch (Branchid, branch_name, address)
Borrower (Card_no, Name, Address, Phone)
a) Create the tables defined above within relational schema. All types of integrity constraints
must be mentioned.
b) Insert data within tables through appropriate form design.
c) Write the SQL queries for the following:
i) Retrieve the names of all borrowers who do not have any books checked out.
ii) For each library branch, retrieve the branch name and the total number of
books loaned out from that branch.
iii) How many copies of books titled “ The City Of Joy” are owned by each branch?
iv) Retrieve the names, address and no. of books checked out for all borrowers
who have more than five books checked out.
v) Retrieve the borrower names who have borrowed maximum number of
books.

2. Consider the following database:


Customers (cid, cname, city, discount)
Agents (aid, aname, city, percent, commission)
Products (pid, pname, city, quantity, price)
Orders (ono, month, cid, aid, pid, qty, tot_price)
Here the column name city appears in Customers, Agents, Products and indicates Customer
city, city where the agent is based and the city where the product is warehoused respectively.
Create the database through the appropriate SQL commands. Use integrity constraints
wherever necessary.
Enter sufficient data through a user-friendly form and also write the SQL queries for the
following:
i) Find all (cid, aid, pid) triples for customer, agent, product combinations that are all in
the same city.
ii) get product names ordered by at least one customer based in Delhi through an agent
based in Kolkata.

iii) Find customers who have the second largest discount. The customer list must be in
sorted order.

3. Consider the following database:


Student (snum, sname, major, level, age)
Class (name, time_schedule, room, faculty_id)
Enrolled (snum, cname)
Faculty (faculty_id, fname, dept_id)
Create and populate the database through appropriate SQL commands. Use integrity
constraints, if required. Data must be entered through a user-friendly screen.

Write the SQL queries for the following:


i) Find the age of the oldest student who is either a C.Sc major or is enrolled in a
course taught by Prof. Ghosh.

Select max(age),sname from student,faculty where student.major=”C.Sc” OR


faculty.fname=”Prof. Ghosh”;

ii) Find the names of faculty members who teach in every room in which some class
is taught.

Select fname from faculty,class where faculty.faculty_id=class.faculty_id AND *

iii) Find the names of students who are enrolled in the maximum number of
classes .The students name must be in sorted order in the output.

4. Consider the following database:


Sailors (sid, sname, rating, age)
Boats (bid, bname, colour)
Reserves (sid, bid, day)
Create the database, define the necessary integrity constraints. Enter the data through
appropriate form design and write SQL queries for the following:
i) Find the name of the sailors who have not reserved a red boat

Select sname from sailors,boats,reserves where sailors.sid=reserves.sid AND


boats.bid=reserves.bid AND boats.colour != “RED”;

ii) Find the name of the sailors with the highest rating.

Select sname from sailors s where 1>(select count(*) from sailors where
s.rating<rating);

iii) For each red boat find the number of reservations for this boat.

5. Consider the following database through approp. SQL commands:


Classes (Class, type, country, numguns, bore, displacement)
Ships (name, class, launched)
Battles (ship, battle, result)
Outcomes (ship, battle, result)
Enter the data through appropriate form design and write SQL trigger for the following:
i) When a new class is inserted with a displacement greater than 35000 tons, allow
the insertion but change the displacement to 35,000
ii) When a new class is inserted into classes, also insert a ship with the name of
that class and a NULL launch date.

6. Create the following schema through SQL:


Customer (cust_id, cust_name, annual_revenue, cust_type)
[cust_id must be between 100 and 10,000,annual_revenue defaults to 1 lakh,
cust_type must be manufacturer, retailer, wholeseller]
shipment (shipment_no,cust_id,weight,truck,destination,ship_date)
truck (truck_no,driver_name)
city (city_name, population)
Define integrities and enter data within the table through an efficient form design and
express the following queries in SQL:
i) List the cities that have received shipments from every customer.
ii) For each customer, what is the average weight of a package (shipment) sent by
that customer?
iii) Give a list of customers in alphabetical order, who sent all their shipments to a
single city.
iv) List those cities that receive the second largest number of shipments.

7. Consider the relational schema defined in Q6.Create relations, define constraints and enter
data through a user-friendly form.
a) Create Views for:
i) Customers with annual revenue between 10 lakh and 100 lakh
ii) Customers with annual revenue over 100 lakh

b) Create a cursor declaration, which will identify all information about


customers
who have sent a shipment to a city with more than 500,000 population.

You might also like