DBMS Sample Questions
DBMS Sample Questions
Students
Snum sname major level age
051135593 Maria White English SR 21
060839453 Charles Harris Architecture SR 22
099354543 Susan Martin Law JR 20
112348546 Joseph Computer SO 19
Thompson Science
115987938 Christopher Computer JR 20
Garcia Science
Class
name meets_at room fid
Data Structures MWF 10 R128 489456522
Database MWF 12:30- 1320 DCL 142519864
Systems 1:45
Operating TuTh 12-1:20 20 AVW 489456522
System Design
Archaeology of MWF 3-4:15 R128 248965255
the Incas
Enrolled
snum cname
112348546 Database Systems
115987938 Database Systems
348121549 Database Systems
322654189 Database Systems
552455318 Database Systems
455798411 Operating System
Design
552455318 Operating System
Design
567354612 Operating System
Design
112348546 Operating System
Design
115987938 Operating System
Design
322654189 Operating System
Design
Faculty
fid: intger fname: string deptid: integer
142519864 Ivana Teach 20
242518965 James Smith 68
141582651 Mary Johnson 20
011564812 John Williams 68
254099823 Patricia Jones 68
356187925 Robert Brown 12
489456522 Linda Davis 20
QT: 0002
Consider the following schema:
Suppliers (sid: integer, sname: string, address: string)
Parts (pid: integer, pname: string, color: string)
Catalog (sid: integer, pid: integer, cost: real)
The Catalog relation lists the prices charged for parts by Suppliers. Write the following
queries in SQL:
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 no one else.
5. 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).
6. For each part, find the sname of the supplier who charges the most for that part.
7. Find the sids of suppliers who supply only red parts.
8. Find the sids of suppliers who supply a red part and a green part.
9. Find the sids of suppliers who supply a red part or a green part.
10. For every supplier that only supplies green parts, print the name of the supplier and the total
number of parts that she supplies.
[2] Write a PLSQL function to generate the Fibbonacci Series upto N th term.
Suppliers
sid sname address
1 Acme Widget "1 Grub St.
Suppliers
2 Big Red Tool "4 My Way
and Die
3 Perfunctory "99999 Short
Parts Pier
4 Alien Aircaft "2 Groom Lake
Inc.
Parts
pid: integer pname: string color: string
1 Left Handed Bacon Red
Stretcher Cover
2 Smoke Shifter End Black
3 Acme Widget Red
Washer
4 Acme Widget Silver
Washer
5 I Brake for Crop Translucent
Circles Sticker
6 Anti-Gravity Turbine Cyan
Generator
7 Anti-Gravity Turbine Magenta
Generator
8 Fire Hydrant Cap Red
9 7 Segment Display Green
Catalog
1 3 0.50
1 4 0.50
1 8 11.70
2 3 0.55
2 8 7.95
2 1 16.50
3 8 12.50
3 9 1.00
4 5 2.20
4 6 124754
8.23
4 7 124754
8.23
QT: 0003
The following relations keep track of airline flight information:
Flights(flno: integer, from: string, to: string, distance: integer,
departs: time, arrives: time, price: real)
Aircraft(aid: integer, aname: string, cruisingrange: integer)
Certified(eid: integer, aid: integer)
Employees(eid: integer, ename: string, salary: integer)
Note that the Employees relation describes pilots and other kinds of employees as well;every pilot is
certified for some aircraft, and only pilots are certified to fly. Write each of the following queries in
SQL.
1. Find the names of aircraft such that all pilots certified to operate them have salaries 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 for which she or he is certified.
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 1000 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. Identify the routes that can be piloted by every pilot who makes more than $100,000.
8. Print the enames of pilots who can operate planes with cruisingrange greater than 3000 miles but
are not certified on any Boeing aircraft.
9 Print the name and salary of every nonpilot whose salary is more than the average salary for pilots.
10 Print the names of employees who are certified only on aircrafts with cruising range longer than
1000 miles.
[2] Write a PLSQL Function to find the sum of N Numbers and to find the sum of N 2 Numbers
Flights
flno from to distance departs arrive price:
99 Los Washington 2308 2005/04/12 2005/04/12 235.98
Angeles D.C. 09:30 21:40
13 Los Chicago 1749 2005/04/12 2005/04/12 220.98
Angeles 08:45 20:45
346 Los Dallas 1251 2005/04/12 2005/04/12 225.43
Angeles 11:50 19:05
387 Los Boston 2606 2005/04/12 2005/04/12 261.56
Angeles 07:03 17:03
7 Los Sydney 7487 2005/04/12 2005/04/14 1278.56
Angeles 22:30 6:10
2 Los Tokyo 5478 2005/04/12 2005/04/13 780.99
Angeles 12:30 15:55
33 Los Honolulu 2551 2005/04/12 2005/04/12 375.23
Angeles 09:15 11:15
34 Los Honolulu 2551 2005/04/12 2005/04/12 425.98
Angeles 12:45 15:18
76 Chicago Los 1749 2005/04/12 2005/04/12 220.98
Angeles 08:32 10:03
Aircraft
Aid Aname Cruisingrange
1 Boeing 747- 8430
400
2 Boeing 737- 3383
800
3 Airbus A340- 7120
300
4 British 1502
Aerospace
Jetstream 41
5 Embraer ERJ- 1530
145
6 SAAB 340 2128
7 Piper Archer 520
III
8 Tupolev 154 4103
16 Schwitzer 2-33 30
9 Lockheed 6900
L1011
certified
eid aid
567354612 1
567354612 2
567354612 10
567354612 11
567354612 12
567354612 15
567354612 7
567354612 9
567354612 3
567354612 4
567354612 5
552455318 2
552455318 14
550156548 1
550156548 12
390487451 3
390487451 13
390487451 14
Employees
QT: 0004
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:
1. Print the names and ages of each employee who works in both the Hardware department and the
Software department.
2. 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.
3. Print the name of each employee whose salary exceeds the budget of all of the departments that he
or she works in.
4. Find the managerids of managers who manage only departments with budgets greater than $1
million.
5. Find the enames of managers who manage the departments with the largest budgets.
6. 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.
7. Find the managerids of managers who control the largest amounts.
8. 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.
[2] Write a PLSQL function to generate the Fibbonacci Series upto N th term.
Emp
eid ename age salary
142519864 Susan 39 56990
Martin
242518965 James 68 27099
Smith
141582651 Mary 44 94011
Johnson
011564812 John 35 74098
Williams
254099823 Patricia 28 42783
Jones
356187925 Robert 28 35431
Brown
489456522 Linda Davis 26 25971
287321212 Michael 62 131072
Miller
248965255 Barbara 48 95021
Wilson
159542516 Matt Nelson 33 48990
090873519 Elizabeth 27 33055
Taylor
486512566 David 20 25199
Anderson
Works
eid did Pct
142519864 2 100
242518965 1 100
141582651 1 50
141582651 5 50
141582657 1 25
141582657 5 75
011564812 3 100
254099823 3 100
356187925 2 100
489456522 7 100
287321212 2 100
248965255 3 100
159542516 4 100
090873519 2 100
Dept
QT: 0005
Consider the following schema:
Sailors(sid : integer, sname : string, rating:integer, age: real)
Reserves(sid : integer, bid : integer, day : date )
Boats(bid : integer ,bname : string, color : string)
1 Find sids of sailors who have reserved a red boat..
2 Find the names of sailors who have reserved a red boat..
3. Find the colors of boats reserved by Lubber.
4. Find the names of Sailors who have reserved at least one boat.
5. Compute increments for the ratings of persons who have sailed two different boats on the same
day.
6. Find the ages of sailors whose name begins and ends with B and has at least three
characters.
7. Find names of sailors who have reserved a red boat or a green boat.
8. Find the names of sailors who have reserved both a red boat and a green boat.
9. Find the sids of all sailors who have reserved red boats but not green boats .
10. Find all sids of sailors who have a rating of 10 or reserved boat 104.
11. Find the names of sailors who have reserved boat 103. Using nested query only.
12. Find the names of sailors who have reserved a red boat. Using IN Operator only
13. Find sailors whose rating is better than some (every) sailor called Horatio.
14. Find the names of sailors who have reserved all boats.
[2] Write a Procedure which computes sum of N terms.
Sailors
sid sname rati age
ng
22 Dustin 7 45.0
29 Brutus 1 33.0
31 Lubber 8 55.5
32 Andy 8 25.5
58 Rusty 10 35.0
64 Horatio 7 35.0
71 Zorba 10 16.0
74 Horatio 9 35.0
85 Art 3 25.5
95 Bob 3 63.5
32 Andy 8 25.5
Reserves
Boats
bid bname color
101 Interlake blue
102 Interlake red
103 Clipper green
104 Marine red
QT : 0006
[1] Consider the following information about a university database:
Professors have an SSN, a name, an age, a rank, and a research specialty.Projects have a project
number, a sponsor name (e.g., NSF), a starting date, an ending date, and a budget.
Graduate students have an SSN, a name, an age, and a degree program (e.g., M.S.or Ph.D.).Each
project is managed by one professor (known as the project’s principal investigator).Each project is
worked on by one or more professors (known as the project’sco-investigators).Professors can manage
and/or work on multiple projects.
Each project is worked on by one or more graduate students (known as the project’s research
assistants).When graduate students work on a project, a professor must supervise their work on the
project. Graduate students can work on multiple projects, in which case they will have a (potentially
different) supervisor for each one.Departments have a department number, a department name, and a
main office.Departments have a professor (known as the chairman) who runs the
department.Professors work in one or more departments, and for each department that they work in, a
time percentage is associated with their job.Graduate students have one major department in which
they are working on theirdegree.Each graduate student has another, more senior graduate student
(known as astudent advisor) who advises him or her on what courses to take.
Design and draw an ER diagram that captures the information about the university. Convert the ER
diagram to Table using SQL . Answer the queries.
[1] List the names of all Chairman.
[2] List the names of professors who are not Project’s principal Investigator.
[3] List the names of Graduate students and their advisors.
[2] Write a Anon.PL/SQL using Cursors to display names of professors who are both Chairman and
Project’s Principal Investigator.
QT: 0007
Consider the following relations.
Employee(Fname,Minit,Lname,SSN,Bdate,Address,Sex,Salary,Superssn,Dno)
Department (Dname, Dnumber, MGRSSN, MGRStartdate)
Dept_Locations (Dnumber, Dlocation)
Works_on (Essn, Pno, Hours)
Project (Pname, Pnumber, Plocation, Dnum)
Dependent (Essn, Dependent_name, sex, Bdate, Relationship)
[1] List the names of each employee who works all the projects controlled by department number 5.
[2] List the names of the employees who have no dependents.
[3] List the name of managers who have at least one dependent.
[4] Find Sum, Maximum, Minimum and the Average salary of all employees of the ‘research’
department.
[5] For each project on which more than two employees work, list the project number, the project
name, and the number of employees who work on the project.
[6] For each project, list the project number, the project name and the number of employees from
department 5 who work on the project.
[7] For each department that has more than five employees, list the department number and thee
number of its employees who are making more than $40,000.
[8] List all employees in the dept 5 whose salary is between $30000 and $40000.
[9] Find all employees who were born during the 1950s.
[10] List the names of all employees with their supervisor name and supervisor number.
[2A] Create view containing Fname, Lname of all the employees and their project name with number
of hours committed towards that project.
[2B] Create view containing Dept.name, Number of Employees and Sum of Salaries of all
Employees who belong to that department.