ASSIGNMent
ASSIGNMent
1. Answer each of the following questions. The questions are based on the following relational schema:
3. Suppose that we have a ternary relationship R between entity sets A, B, and C such that A has a key
constraint
and total participation and B has a key constraint; these are the only constraints. A has attributes a1
and a2, with
a1 being the key; B and C are similar. R has no descriptive attributes. Write SQL statements that create
tables
corresponding to this information so as to capture as many of the constraints as possible.
10. Consider the following relational schema. An employee can work in more than one department; the pct
time field of the Works relation shows the percentage of time that a given employee works in a given
department.
Emp(eid: integer, ename: string, age: integer, salary: real)
Works(eid: integer, did: integer, pct time: integer)
Dept(did: integer, dname: string, budget: real, managerid: integer)
Write the following queries in SQL:
a. Find the names and ages of each employee who works in both the Hardware department and
the Software department.
b. For each department with more than 20 full-time-equivalent employees (i.e., where the part-
time and full-time employees add up to at least that many full-time employees), print the did
together with the number of employees that work in that department.
c. Print the name of each employee whose salary exceeds the budget of all of the
d. departments that he or she works in.
e. Find the managerids of managers who manage only departments with budgets greater than $1
million.
f. Find the enames of managers who manage the departments with the largest budgets.
g. If a manager manages more than one department, he or she controls the sum of all the budgets
for those departments. Find the managerids of managers who control more than $5 million.
h. Find the managerids of managers who control the largest amounts.
i. Find the enames of managers who manage only departments with budgets larger than $1
million, but at least one department with budget less than $5 million.
11.
Sid Sname Rating age
18 Jones 3 30
41 jonah 6 56
22 Ahab 7 44
63 moby null 15
Figure 2. An Instance of Sailors
Consider the instance of the Sailors relation shown in Figure 2
a. Write SQL queries to compute the average rating, using AVG; the sum of the ratings, using SUM;
and the number of ratings, using COUNT.
b. If you divide the sum just computed by the count, would the result be the same as the average?
How would your answer change if these steps were carried out with respect to the age field
instead of rating?
12. Consider the following query: Find the names of sailors with a higher rating than all sailors with age < 21.
The following two SQL queries attempt to obtain the answer to this question. Do they both compute the
result? If not, explain why.
Under what conditions would they compute the same result?
SELECT S.sname
FROM Sailors S
WHERE NOT EXISTS ( SELECT *
FROM Sailors S2
WHERE S2.age < 21
AND S.rating <= S2.rating )
SELECT *
FROM Sailors S
WHERE S.rating > ANY ( SELECT S2.rating
FROM Sailors S2
WHERE S2.age < 21 )
13. Consider the instance of Sailors shown in Figure 2. Let us define instance S1 of Sailors to consist of the first
two tuples, instance S2 to be the last two tuples, and S to be the given instance.
a. Show the left outer join of S with itself, with the join condition being sid=sid.
b. Show the right outer join of S with itself, with the join condition being sid=sid.
c. Show the full outer join of S with itself, with the join condition being sid=sid.
d. Show the left outer join of S1 with S2, with the join condition being sid=sid.
e. Show the right outer join of S1 with S2, with the join condition being sid=sid.
f. Show the full outer join of S1 with S2, with the join condition being sid=sid.
14. Consider the following relational schema and briefly answer the questions that follow:
Emp(eid: integer, ename: string, age: integer, salary: real)
Works(eid: integer, did: integer, pct time: integer)
Dept(did: integer, budget: real, managerid: integer)
a. Define a table constraint on Emp that will ensure that every employee makes at least $10,000.
b. Define a table constraint on Dept that will ensure that all managers have age > 30.
c. Define an assertion on Dept that will ensure that all managers have age > 30.
d. Compare this assertion with the equivalent table constraint. Explain which is better.
e. Write SQL statements to delete all information about employees whose salaries exceed that of
the manager of one or more departments that they work in. Be sure to ensure that all the
relevant integrity constraints are satisfied after your updates.
Answer each of the following questions briefly. The questions are based on the following relational
schema:
Set 1
1. Write the SQL statements required to create the above relations, including appropriate versions of all
primary and foreign key integrity constraints.
3. Define the Dept relation in SQL so that every department is guaranteed to have a manager.
4. Write an SQL statement to add `John Doe' as an employee with eid = 101, age = 32 and salary =
15000.
5. Write an SQL statement to give every employee a 10% raise in the salary.
6. Write an SQL statement to delete the `Toy' department. Given the referential integrity constraints you
chose for this schema, explain what happens when this statement is executed.
Set 2
1. Define a table constraint on emp such that for any employee salary should not be less than 5000,
age should not be less than 18 years. Also ensure that age should not be less than 30 for any
manager.
2. List the employees which work under the manager who controls the largest amount. [A manager
can be managing more than 2 departments]
3. Display the name & salary of managers who manage the departments with the top 3 largest
budgets.
4. List the employee name, employee salary, manager name and the total working hours of the
employee in the company.
5. List the department id, department name, its manager name and the number of employees
working in that department [excluding manager].
Set 3
Enforce the following constraints and for each constraint, state what operations (inserts, deletes, and
updates on specific relations) must be monitored to enforce the constraint.
1. Every class has a minimum enrollment of 5 students and a maximum enrollment of 30 students.
2. Every student must be enrolled in the course called Math101.
3. No department can have more than 10 faculty members.
4. Every faculty member must teach at least two courses.
5. Two classes cannot meet in the same room at the same time.
Write the following queries in SQL. No duplicates should be printed in any of the answers.
Student(snum: integer, sname: string, major: string, level: string, age: integer)
Class(cname: string, meets at: time, room: string, fid: integer)
Enrolled(snum: integer, cname: string)
Faculty (fid: integer, fname: string, deptid: integer)
Set 1
1. Find the names of all classes that either meet in room R128 or have five or more students enrolled.
2. Find the names of all Juniors (Level = JR) who are enrolled in a class taught by I. Teach.
3. Find the age of the oldest student who is either a History major or is enrolled in a course taught by
I. Teach.
4. Find the names of all students who are enrolled in two classes that meet at the same time.
Set 2
1. Find the names of faculty members who teach in every room in which some class is taught.
2. Find the names of faculty members for whom the combined enrollment of the courses that they
teach is less than five.
3. Print the Level and the average age of students for that Level, for all Levels except JR.
4. Find the names of students who are enrolled in the maximum number of classes.
5. Find the names of students who are not enrolled in any class.
Set 1
1. Find the pnames of parts for which there is some supplier.
2. Find the snames of suppliers who supply every part.
3. Find the snames of suppliers who supply every red part.
4. Find the pnames of parts supplied by Acme Widget Suppliers and by no one else.
5. Find the sids of suppliers who supply a red part and a green part.
Set 2
1. Find the sids of suppliers who charge more for some part than the average cost of that
part (averaged over all the suppliers who supply that part).
2. For each part, find the sname of the supplier who charges the most for that part.
3. Find the sids of suppliers who supply only red parts.
4. Find the sids of suppliers who supply a red part or a green part
Flights(flno: integer, from: string, to: string, distance: integer, departs: time, arrives: time, price: integer)
Aircraft(aid: integer, aname: string, cruisingrange: integer)
Certified(eid: integer, aid: integer)
Employees(eid: integer, ename: string, salary: integer)
Set 1
1. Find the names of aircraft such that all pilots certified to operate them earn more than 80,000.
2. For each pilot who is certified for more than three aircraft, find the eid and the maximum
cruisingrange of the aircraft that he (or she) is certified for.
3. Find the names of pilots whose salary is less than the price of the cheapest route from Los Angeles
to Honolulu.
4. For all aircraft with cruisingrange over 1,000 miles, find the name of the aircraft and the average
salary of all pilots certified for this aircraft.
5. Find the names of pilots certified for some Boeing aircraft.
6. Find the aids of all aircraft that can be used on routes from Los Angeles to Chicago.
7. Print the enames of pilots who can operate planes with cruisingrange greater than 3000 miles, but
are not certified on any Boeing aircraft.
Set 1
1. Find the average speed of PC's.
2. Find the average speed of laptops costing over Rs. 2000.
3. Find the average price of PC's made by manufacturer A.
4. Find the average price of PC's and laptops made by manufacturer D.
5. Find, for each different speed the average price of a PC.
Set 2
1. Find for each manufacturer, the average screen size of its laptops.
2. Find the manufacturers that make at least three different models of PC.
3. Find for each manufacturer who sells PC's the maximum price of a PC.
4. Find, for each speed of PC above 800, the average price.
5. Find the average hard disk size of a PC for all those manufacturers that make printers.
a. Write a CREATE TABLE statement with constraint if every manager is also an employee.
b. Write SELECT statement to print number of employee working directly or indirectly under
each manager.
c. Write SELECT statement to print name of manager having maximum number of employee
working under him/her(directly or indirectly).
d. Write name of top three employees in terms of salary who are having ‘Anil’ as
manager(directly or indirectly).
e. Write UPDATE statement to add Rs. 100 in salary of ‘Anil’ if both ‘Anil’ and ‘Sunil’ are having
same manager (directly or indirectly).
1. Data of sales of cars is given as follows. (Sales is given at district level and covering all districts
and all states)
Sales
Location
Region Pregion
Nagpur Vidharbha
Vidharbha Maharashtra
Maharashtra India
SQL QUESTIONS
4. Consider the above employee database. Give an expression in SQL for each of the following
queries.
a. Modify the database so that Jones now lives in Newtown.
b. Give all employees of First Bank Corporation a 10 percent raise.
c. Give all managers of First Bank Corporation a 10 percent raise.
d. Give all managers of First Bank Corporation a 10 percent raise unless the salary
becomes greater than $100,000; in such cases, give only a 3 percent raise.
e. Delete all tuples in the works relation for employees of Small Bank Corporation.
5. Write an SQL query, without using a with clause, to find all branches where the total account
deposit is less than the average total account deposit at all branches,
a. Using a nested query in the from clauser.
b. Using a nested query in a having clause.
6. Suppose that we have a relation marks(student-id, score) and we wish to assign grades to
students based on the score as follows: grade F if score<40, grade C if 40 ≤score<60, grade B if
60≤score<80, and grade A if 80≤score.Write SQL queries to do the following:
a. Display the grade for each student, based on the marks relation.
b. Find the number of students with each grade.