You are on page 1of 5

SQL EXERCISE

Practice and practice we can improve our skills.


Retrieve the required information using SQL language.
Part I. Give a database schema for a library management system as the following picture.
1. How many copies of the book titled he Lost ribe are owned by the library branch whose name is
!Sharpstown!"
2. How many copies of the book titled he Lost ribe are owned by each library branch"
3. Retrieve the names of all borrowers who do not have any books checked out .
4. #or each book that is loaned out from the !Sharpstown! branch and whose $ue$ate is today% retrieve the
book title% the borrower&s name% and the borrower&s address.
5. #or each library branch% retrieve the branch name and the total number of books loaned out from that
branch.
6. Retrieve the names% addresses% and number of books checked out for all borrowers who have more than
five books checked out.
7. #or each book authored 'or co(authored) by !Stephen *ing!% retrieve the title and the number of copies
owned by the library branch whose name is !+entral!
Part II Give a database schema of a company as the following picture.
8. Retrieve the names of employees in department , who work more than -. hours per week on the
&/roduct0& pro1ect.
9. #or each pro1ect% list the pro1ect name and the total hours per week 'by all employees) spent on that
pro1ect.
10. Retrieve the names of employees who work on every pro1ect.
11. Retrieve the names of employees who do not work on any pro1ect.
12. #ind the names and addresses of employees who work on at least one pro1ect located in Houston but
whose department has no location in Houston.
13. List the last names of department managers who have no dependents.
14. #ind details of those employees whose salary is 2 the average salary for all employees. 3utput salary in
descending order.
15. #ind details of those employees whose salary is 2 the average salary for all employees in his4her
department. 3utput salary in ascending order.
Answer:
( Should not refer to the answer before trying to write down your solutions
( The answer for each question is only one (or two) of the many other solutions
1.
So!t"on 1:
SELEC# $%.&o'()'Co*"es
+R(, -((. $/ -((.'C(PIES $%/ LI-RAR0'-RA&C1 $
21ERE $.-oo3I4 5 $%.-oo3I4 A&6
$%.-ran%7I4 5 $.-ran%7I4 A&6
#"te58#7e Lost #r"$e8 A&6 -ran%7&a9e58S7ar*stown8:
So!t"on 2:
SELEC# &o'()'Co*"es
+R(, ;;-((. &A#<RAL =(I& -((.'C(PIES > &A#<RAL =(I&
LI-RAR0'-RA&C1 >
21ERE #"te58#7e Lost #r"$e8 A&6 -ran%7&a9e58S7ar*stown8:
2. SELEC# -ran%7&a9e/ &o'()'Co*"es
+R(, ;;-((. &A#<RAL =(I& -((.'C(PIES > &A#<RAL =(I&
LI-RAR0'-RA&C1 >
21ERE #"te58#7e Lost #r"$e8:
3.
So!t"on 1:
SELEC# &a9e
+R(, -(RR(2ER -
21ERE Car4&o &(# I& ;SELEC# Car4&o
+R(, -((.'L(A&S >:
So!t"on 2:
SELEC# &a9e
+R(, -(RR(2ER -
21ERE &(# EXIS#S ;SELEC# ?
+R(, -((.'L(A&S L
21ERE -.Car4&o 5 L.Car4&o >:
4. SELEC# -.#"te/ R.&a9e/ R.A44ress
+R(, -((. -/ -(RR(2ER R/ -((.'L(A&S -L/ LI-RAR0'-RA&C1 L-
21ERE L-.-ran%7&a9e58S7ar*stown8 A&6 L-.-ran%7I45-L.-ran%7I4 A&6
-L.6!e6ate58today8 A&6 -L.Car4&o5R.Car4&o A&6 -L.-oo3I45-.-oo3I4
5. SELEC# L.-ran%7&a9e/ C(<&#;?>
+R(, LI-RAR0'-RA&C1 L/ -((.'L(A&S -L
21ERE -L.-ran%7I4 5 L.-ran%7I4
@R(<P -0 L.-ran%7&a9e:
6. SELEC# -.&a9e/ -.A44ress/ C(<&#;?>
+R(, -(RR(2ER -/ -((.'L(A&S L
21ERE -.Car4&o 5 L.Car4&o
@R(<P -0 -.Car4&o/ -.&a9e/ -.A44ress
1AAI&@ C(<&#;?> B 5:
7.
So!t"on 1:
SELEC# #"te/ &o'()'Co*"es
+R(, ;;;-((.'A<#1(RS &A#<RAL =(I& -((.> &A#<RAL =(I& -((.'C(PIES>
&A#<RAL =(I& LI-RAR0'-RA&C1>21ERE A!t7or'&a9e58Ste*7en ."nC8 A&6
-ran%7&a9e58Centra8:
So!t"on 2: Student should write the another solution not using the natural 1oin.
8. SELEC# L&A,E/ +&A,E
+R(, E,PL(0EE/ 2(R.S'(&/ PR(=EC#
21ERE 6&(55 A&6 SS&5ESS& A&6 P&(5P&<,-ER A&6 P&A,E58Pro4!%tX8 A&6
1(<RSB10:
9. SELEC# P&A,E/ S<, ;1(<RS>
+R(, PR(=EC#/ 2(R.S'(&
21ERE P&<,-ER5P&(
@R(<P -0 P&A,E:
&ote: he Group 5y clause should be replaced as GR36/ 57 '/8695:R% /8;9:) since there
may be some pro1ects have the same name.
10.
So!t"on 1:
SELEC# E.L&A,E/ E.+&A,E
+R(, E,PL(0EE E
21ERE &(# EXIS#S ;SELEC# P&<,-ER
+R(, PR(=EC#
21ERE P&<,-ER &(# I& ;SELEC# P&(
+R(, 2(R.S'(&
21ERE ESS&5E.SS& > >:
So!t"on 2:
SELEC# L&A,E/ +&A,E
+R(, E,PL(0EE
21ERE &(# EXIS#S ;SELEC# P&<,-ER
+R(, PR(=EC#
21ERE &(# EXIS#S ;SELEC# ?
+R(, 2(R.S'(&
21ERE P&<,-ER5P&( A&6 ESS&5SS& > >:
11.
So!t"on 1:
SELEC# L&A,E/ +&A,E
+R(, E,PL(0EE
21ERE SS& &(# I& ; SELEC# ESS&
+R(, 2(R.S'(&>:

So!t"on 2:
SELEC# L&A,E/ +&A,E
+R(, E,PL(0EE
21ERE &(# EXIS#S ; SELEC# ?
+R(, 2(R.S'(&
21ERE ESS&5SS& >:
12.
So!t"on 1:
SELEC# L&A,E/ +&A,E/ A66RESS
+R(, E,PL(0EE
21ERE EXIS#S ; SELEC# ?
+R(, 2(R.S'(& 2/ PR(=EC# P/ 6EP#'L(CA#I(&S 6L
21ERE 2.P&( 5 P.P&<,-ER A&6
P.6&<, 5 6L.6&<, A&6
6L.6L(CA#I(& DB E1o!stonF>:
So!t"on 2:
SELEC# L&A,E/ +&A,E/ A66RESS
+R(, E,PL(0EE
21ERE EXIS#S ; SELEC# ?
+R(, 2(R.S'(&/ PR(=EC#
21ERE SS&5ESS& A&6 P&(5P&<,-ER A&6 PL(CA#I(&581o!ston8 >
A&6
&(# EXIS#S ; SELEC# ?
+R(, 6EP#'L(CA#I(&S
21ERE 6&(56&<,-ER A&6 6L(CA#I(&581o!ston8 >:
13.
So!t"on 1:
SELEC# E.L&A,E/ E.+&A,E
+R(, E,PL(0EE E/ 6EPAR#,E&# 6
21ERE E.SS& 5 6.,R@SS& A&6
&(# EXIS#S ; SELEC# 6EPE&6E&#'&A,E
+R(, 6EPE&6E&#
21ERE ESS&5E.SS& >
So!t"on 2:
SELEC# L&A,E/ +&A,E
+R(, E,PL(0EE
21ERE EXIS#S ; SELEC# ?
+R(, 6EPAR#,E&#
21ERE SS&5,@RSS& >
A&6
&(# EXIS#S ; SELEC# ?
+R(, 6EPE&6E&#
21ERE SS&5ESS& >:
14. SELEC# ?
+R(, E9*oGee
21ERE SaarG B (SELECT AVG (Salary)
FROM Employee )
(R6ER -0 SaarG 6ESC:
15.
So!t"on 1:
SELEC# E.?
+R(, E,PL(0EE E/ ;SELEC# 6&(/ AAERA@E;SALAR0> AS L#-
+R(, E,PL(0EE
@R(<P -0 6&(> AS A
21ERE E.6&( 5 A.6&( A&6
E.SALAR0 B L#-:
So!t"on 2:
SELEC# E.?
+R(, E,PL(0EE E
21ERE E.SALAR0 B ;SELEC# AAERA@E;SALAR0>
+R(, E,PL(0EE
21ERE 6&( 5 E.6&(>:
&ote: if we want to display the average of the salary we should employ the solution - and put the <field
name= L5 in the select clause.

You might also like