Anda di halaman 1dari 50

SQL Queries

Reference Text Book:


Fundamentals of DBMS 5e.
Chapter name: The Relational Algebra and
Relational Calculus.

Company RDBMS
Pno: 6.19, Page no: 214-215
Reference Schema: Fig 5.5

6.19a. Retrieve the names of all employees in department 5 who work more than 10
hrs per week on the productX project.

select fname,minit,lname
from employee
where dno=5 and ssn in ( select essn
from project JOIN works_on ON
pname = productX and hours > 10);

6.19.b. List the names of all employees who have a dependent with the same first
name as themselves.

select fname , minit , lname


from employee JOIN dependent ON
where fname = dependent_name;

6.19.c. Find the names of all employees who are directly supervised by Franklin
Wong

Select e.fname,e.minit,e.lname
from employee e
where e.super_ssn = (select ssn from employee where fname=Franklin and
lname = Wong);

6.19.d. For each Project list the Project name and the total hour per week ( by all
employees ) spend on that project.

select pname,SUM(hours) as Total_no_of_hours


from project JOIN works_on ON
pnumber = pno
group by pname;

6.19.e. Retrieve the names of all employees who works on every project.

select e.fname,e.minit,e.lname
from employee e
where not exists ( (select pnumber from project ) MINUS
( select pno from works_on where e.ssn = essn));

6.19.f. Retrieve the names of all employees who do not work on any project.

select fname,minit,lname
from employee
where ssn not in ( select essn from works_on );

6.19.g. For each department, retrieve the department name and the average salary of
all employees working in that department.

select dname,AVERAGE(salary) as Average_sal


from ( select dname,salary from employee JOIN department ON dno = dnumber )
group by dname;

6.19.h. Retrieve the average salary of all female employees.

select AVERAGE(salary) as Avg_salary


from employee
where sex = F;

10

6.19.i. Find the names and address of all employees who work on at least one project
located in houston but whose department has no location in houston.

select fname,address
from employee
where dno in ( (select dnum from project where plocation = Houston ) MINUS
( select dnumber from dept_location where dlocation = Houston ));

11

6.19.j. List the names of all department managers who have no dependent.

select fname,minit,lname
from employee
where ssn not in ( select essn from dependent ) and
ssn in ( select mgr_ssn from department);

12

Airline RDBMS
Pno: 5.16, Page no:215
Reference Schema :Fig 5.16

13

6.20.a. For each flight list the flight number, the departure airport for first leg of the
flight and arrival airport for the last leg of the flight.

Create view flight_min_leg as


select * from
flight_leg f1 JOIN (select flight_number,MINIMUM(leg_number)) ON
f1.flight_number = flight_number and f1.leg_number = MINIMUM(leg_number)
Create view flight_max_leg as
select * from
flight_leg f1 JOIN (select flight_number,MAXIMUM(leg_number)) ON
f1.flight_number = flight_number and f1.leg_number = MAXIMUM(leg_number)
Select * from
(select flight_no,dname dep from airport a JOIN (select * from flight_min_leg) b
ON a.airport_code = b.departure_airport_code) NATURAL JOIN
( select flight_no,dname arr from airport a1 JOIN (select * from flight_max_leg)
b1 ON a1.airport_code = b1.arrival_airport_code) ;

14

6.20.b. List the flight numbers and weekdays of all flight or flight legs that depart from
Houston international airport ( IAH ) and arrive at Los Angeles international airport
( LAX )

select flight_number,weekdays
from flight NATURAL JOIN flight_leg
where departure_airport_code = IAH and arrival+airport_code = LAX;

15

6.20.c. List the number ,departure airport code, scheduled departure time, arrival airport code,
scheduled arrival time, and weekdays of all flights or flight legs that depart from some airport in
the city of Houston and arrive at some airport in the city of Los Angeles.

select * from
( ( flight_leg NATURAL JOIN
( select departure_airport_code from
airport JOIN ( select departure_airport_code from flight_leg f1 ) ON airport_code
= departure_airport_code
where city = HOUSTON )) NATURAL JOIN select arrival_airport_code from
airport JOIN (select arrival_airport_code from flight_leg f2) ON airport_code =
arrival_airport_code
where city = LOS ANGELES);

16

6.20.d. List the fare information for flight number CO197.

select fare_code,amount,restrictions
from fare
where flight_number = CO197 ;

17

6.20.e. Retrieve the number of available seats for flight number CO197 on
1999-10-09.

select SUM(number_of_available_seats) as Total_no_of_seats


from leg_instance
where flight_number = CO197 and date = 1999-10-09;

18

Library RDBMS
Pno: 6.21, Page no: 215-216
Reference Schema : Fig 6.14

19

6.21.a. How many copies of the book titled The Last Tribe are owned by the library
branch whose name is Sharpstown.

select no_of_copies from


book_identity CROSS JOIN branch_identity
where branch_id = (select branch_id from library_branch where branch_name =
Sharpstown) and book_id = ( select book_id from book where title = The last
Tribe );

20

6.21.b. How many copies of the book titled The Last Tribe are owned by each library
branch.

select branch_name,SUM(no_of_copies) as Total_copies


from book_copies NATURAL JOIN library_branch
where book_id = (select book_id from book where title = The last tribe)
group by branch_name;

21

6.21.c. Retrieve the names of all borrowers who do not have any book checked out.

select name
from borrower NATURAL JOIN (select card_no from book_loans
where due_date < today );

NOTE: Please Replace today by today`s date.

22

6.21.d. For each book that is loaned from the Sharpstown branch and whose
due_date is today , Retrieve the booktitle, the borrowers name and borrowers
address.
select b.title,bw.name,bw.address
from (book b CROSS JOIN borrower bw) JOIN (select book_id,cardno
from book_loans bl
where bl.due_date = today and
bl.branch_id = (select branch_id from
library_branch where branchname =
Sharpstown)) ON
b.book_id = book_id and bw.cardno = cardno;
NOTE: Replace today by today`s date

23

6.21.e. For each library branch retrieve the branch name and the total number of
books loaned from that branch.

select branchname,COUNT(bookid) as Total_no_of_copies


from book_loans NATURAL JOIN library_branch
group by branchname;

24

6.21.f. Retrieve the names, address and number of books checked out for all
borrowers who have more than 5 books checked out.

select name,COUNT(bookid) as Books_checked


from book_loans NATURAL JOIN borrower
where today < due_date
having Books_checked > 5;

NOTE: Replace today by today`s date

25

6.21.g. For each book authored ( or co authored ) by Stephen King , retrieve the title
and the number of copies owned by the library branch whose name is Central.

select title,no_of_copies
from book_loans
where branchid = (select branchid from library_branch where branchname =
central ) and bookid in (select bookid from book_authors where authorname =
Stephen king);

26

Order-Processing RDBMS
Pno: 6.22, Page no: 217
Reference Schema: 5.18
Please Note:# is replaced by _no
27

6.22.a. List the order# and ship_date for all orders shipped from warehouse# w2.

select order_no, shipdate


from shipment
where warehouse_no = W2;

28

6.22.b. List the warehouse information from which the customer named Jose Lopez
was supplied his orders, produce a listing: order#, warehouse#.

select order_no,warehouse_no
from (customers NATURAL JOIN order) NATURAL JOIN shipment
where cname = Jose Lopez;

29

6.22.c. Produce a listing cname, no_of_orders, avg_order_amt where the middle


column is the total number of orders by the customer and the last column is the
average order amount for that customer.

select cname,no_of_orders,avg_order_amt from


(select cust_no,cname,COUNT(order_no) as no_of_orders,AVERAGE(ord_amt) as
avg_order_amt
from customer NATURAL JOIN order
group by cust_no);

30

6.22.d. List the orders that were not shipped within 30 days of ordering.

select order_no
from order NATURAL JOIN shipment
where ship_date odate >=30;

31

6.22.e. List the order# for orders that were shipped from all warehouses that the
company has in New York.

select s.order_no
from shipment s
where not exists ((select warehouse_no from warehouse where city = New
Work)
MINUS (select warehouse_no from shipment where order_no = s.order_no));

32

Salesmen Business Trip RDBMS


Pno: 6.23, Page no: 217
Reference Schema: 5.19

33

6.23.a. Give the details ( of all attributes ) for trips that exceeds $5000 in expences.

select ssn,from_city,to_city,departure_date,return_date
from trip NATURAL JOIN expence
where amount > 5000;

34

6.23.b. Print the ssn and name of the salesman who made trips to SAN FRANSISCO.

select ssn,name
from trip NATURAL JOIN salesperson
where to_city = SAN FRANSISCO;

35

6.23.c Print the total trip expenses incurred by the salesman with the name =
Rajaram.

select SUM(amount) as Total_expence


from (salesperson NATURAL JOIN trips) NATURAL JOIN expences
where name = rajaram;

36

Student Enrollment RDBMS


Pno: 6.24, Page no: 217
Reference Schema: 5.20

37

6.24.a. List the numbers of courses taken up by all students named prakash in winter
2007 ( i.e. quarter = W07 ).

select ssn,COUNT(course_no) as no_of_courses


from enroll NATURAL JOIN student
where name = Prakash
group by ssn;

38

6.24.b. Produce a list of textbooks (include course#, book_ISBN , book_title, ) for


courses offered by the IT department that have used more than 3 books.

select course_no,book_ISBN,book_title
from book_adaoption NATURAL JOIN text
where course_no in ( select course_no from
( select c.course_no,COUNT(ba.book_ISBN) from
course c JOIN book_adaoption ba ON c.course_no = ba.course_no
where c.dept=IT
group by c.course_no
having COUNT(ba.book_ISBN) > 3));

39

6.24.c. Mention any department that has all its adapted books published by Addison
Wesley Publishing.

select c.dept
from course c
where not exists ((select book_ISBN from book where publisher = Addison wesley
Publishing) MINUS (select book_ISBN from book_adaoption where course_no =
c.course_no));

40

Nested Query Examples.


Pno: 6.35, Page no: 219
Reference Schema:

41

6.35.a. List the names of all employees who work in the department that has the
employee with highest salary among all employees.

select e.fname,e.minit,e.lname from employee e


where e.dno in (select e1.dno from employee e1
where e1.salary = (select MAXIMUM(salary) from employee));

42

6.35.b List the names of all employees whose supervisors supervisor has 888665555
for ssn.

select e.fname,e.minit,e.lname from employee e


where e.super_ssn in (select ssn from employee where super_ssn = 888665555);

43

6.35.c. List the names of employees who make at least $10,000 more than the
employee who is paid the least in the company.

select e.employee,e.minit,e.lname from employee e


where e.salary >= (select 10000+MINIMUM(salary) from employee);

44

Book Club RDBMS


Pno: 6.41, Page no: 220-221
Reference Schema: 6.41

45

6.41.a. Find the names of members who are the professors older than 45 years.

Select name from members


where age > 45 and designation = professor;

46

6.41.b. List the titles of books reserved by professors.

Select btitle
from (members NATURAL JOIN reserves) NATURAL JOIN books
where desidnation = Professor;

47

6.41.c. Find IDs of members who have not reserved books that cost more than Rs.500.

Select member_id
from reserves
where member_id in ((select r1.member_id from reserves r1) MINUS (select
r2.member_id from reserves r2 NATURAL JOIN books b
where b.bprice > 500));

48

6.41.d. Find the author and titles of books reserved on 27-may-2007.

Select bauthor,titles
from books NATURAL JOIN reserves
where date = 27-may-2007;

49

6.41.e. Find the names of members who have reserved all books.

Select m.name
from members m
where not exists( (select bid from books) MINUS (select bid from reserves where
member_id = m.member_id));

50

Anda mungkin juga menyukai