SQL assignment 2

Assignment description.
Language: Access SQL.
Level: Basic.
Price: 80$
Programming Assignment.
You are given the following SLB database, which contains three tables – STUDENT, LOAN,
and BANK. (The SLB database is downloadable from the online syllabus on the blackboard.)
Problem
Do the following problems. Hand in SQL queries as well as their results.
1. (qt-6) List the SNAME of all students with no loans.
2. (qt-7) List SNAME, AGE and SLEVEL of all students in a given SLEVEL only if at least one student in
that slevel has AGE>22.
3. (qt-8) List the SNAME for all students with szip=98101 or who have a loan with a bank that has
bzip=98101. (If necessary, split the query into several queries. Also, if there are names repeated more
than once, ignore it.)
4. *(qt-10) List BID of banks of type "SL" and the BID and AMOUNT of loans which do not belong to
those banks.
5. (qt-11) List SID, SNAME and BID for students sharing their SZIP with a bank.
Bonus Problems
6. (qt-12) List BID of all banks sharing their BZIP with another bank.
7. *(qt-14) List SNAME and AGE of all students of a given SLEVEL only if everyone at his/her SLEVEL
is 20 years of age or younger.
Notes
• For problem 1 you’ll need a LEFT JOIN … and the WHERE clause should contain the condition “lid IS NULL”.
• Problem 4 says "List BID of banks of type ‘SL’ and the BID and AMOUNT of loans which do not belong to those
banks." What does it mean? Well, if there are two SL banks, say X and Y, the query has to list for X all the loans
it didn't issue (including those issued by Y) and list for Y all the loans it didn't issue (including ...X). To solve this
problem, you need a JOIN between two tables (I won't say which) where the condition for match is that the BID in
one table will be ‘not equal’ (“<>”) to the BID in the other table.
• One way to go about solving problem 6 is to join table Bank with itself (on bzip), and then figure out a way to get
read of rows having the same bank twice (because every bank will be in the same zip code of itself).
• One way to solve problem 7 is using a Type II embedded query. The inner query would look for students having
age <> 20 at a given slevel, and if the answer to the inner query is empty (i.e., NOT EXISTS), the outer query
should do the rest ...
Solution.
Task 1
(qt-6) List the SNAME of all students with no loans.
SQL:
SELECT sname
FROM (student S LEFT JOIN loan L ON S.sid=L.sid)
WHERE lid is NULL;
Query Result:
Query1
sname
JONES
SANDS
Task 2
(qt-7) List SNAME, AGE and SLEVEL of all students in a given SLEVEL only if at
least one student in that slevel has AGE>22.
SQL:
SELECT sname, age, slevel
FROM student S1
WHERE EXISTS
(SELECT *
FROM student S2
WHERE S2.slevel=S1.slevel
AND S2.age>22
)
ORDER BY slevel, age;
Query Result:
Query1
sname
age slevel
JONES
23
4
CABEEN
24
5
WATSO
29
5
N
Task 3
(qt-8) List the SNAME for all students with szip=98101 or who have a loan with a bank
that has bzip=98101. (If necessary, split the query into several queries. Also, if there are
names repeated more than once, ignore it.)
SQL:
SELECT DISTINCT(sname)
FROM student S
WHERE szip=98101
OR EXISTS
(SELECT *
FROM loan L
WHERE L.sid = S.sid
AND bid IN
(SELECT bid
FROM bank
WHERE bzip=98101
)
)
ORDER BY sname;
Query Result:
Query1
sname
ALLEN
ALTER
SANDS
Task 4
*(qt-10) List BID of banks of type "SL" and the BID and AMOUNT of loans which do
not belong to those banks.
SQL:
SELECT B.bid, L.bid, L.amount
FROM bank B, loan L
WHERE B.btype="SL"
AND L.bid<>B.bid
ORDER BY B.bid, L.bid, L.amount
Query Result:
Query1
B.bid
CAPITAL
CAPITAL
CAPITAL
CAPITAL
CAPITAL
CAPITAL
CAPITAL
CAPITAL
HOME
HOME
HOME
HOME
HOME
HOME
HOME
L.bid
FIDELITY
FIDELITY
HOME
PEOPLES
PEOPLES
PEOPLES
PEOPLES
SEAFIRST
FIDELITY
FIDELITY
PEOPLES
PEOPLES
PEOPLES
PEOPLES
SEAFIRST
amount
2 100,00р.
2 200,00р.
1 200,00р.
1 000,00р.
1 900,00р.
2 500,00р.
3 000,00р.
2 000,00р.
2 100,00р.
2 200,00р.
1 000,00р.
1 900,00р.
2 500,00р.
3 000,00р.
2 000,00р.
Task 5
(qt-11) List SID, SNAME and BID for students sharing their SZIP with a bank.
SQL:
SELECT sid, sname, bid
FROM student, bank
WHERE szip=bzip
ORDER BY sid, sname, bid
Query Result:
Query1
sid
6793
6793
9735
9735
sname
SANDS
SANDS
ALLEN
ALLEN
bid
FIDELITY
SEAFIRST
FIDELITY
SEAFIRST
Task 6
(qt-12) List BID of all banks sharing their BZIP with another bank.
SQL:
SELECT B1.bid
FROM bank B1, bank B2
WHERE B1.bzip=B2.bzip
AND B1.bid<>B2.bid
Query Result:
Query1
bid
SEAFIRST
FIDELITY
Task 7
*(qt-14) List SNAME and AGE of all students of a given SLEVEL only if everyone at
his/her SLEVEL is 20 years of age or younger.
SQL:
SELECT sname, age
FROM student S1
WHERE NOT EXISTS
(SELECT *
FROM student S2
WHERE S2.slevel=S1.slevel
AND S2.age > 20
)
ORDER BY sname, age;
Query Result:
Query1
sname age
ALTER 19