• Non ci sono risultati.

SQL language Queries in SQL Tania Cerquitelli

N/A
N/A
Protected

Academic year: 2021

Condividi "SQL language Queries in SQL Tania Cerquitelli"

Copied!
16
0
0

Testo completo

(1)

DB M G

SQL language

Queries in SQL

Tania Cerquitelli

(2)

DB M G

Exercise n. 1

Find the names of the magazines that have never published any article about motorcycles

MAGAZINE (MId, MName, Publisher) ARTICLE (AId, Title, Topic, MId)

SELECT MName FROM MAGAZINE

WHERE MID NOT IN (SELECT MID FROM ARTICLE

WHERE Topic= ‘Motorcycle’)

Set of data to be excluded: Set of magazines that published at least an article on Motorcycle topic

(3)

DB M G

Exercise n. 1

Find the names of the magazines that have never published any article about motorcycles

MAGAZINE (MId, MName, Publisher) ARTICLE (AId, Title, Topic, MId)

SELECT Mname FROM MAGAZINE M

WHERE NOT EXISTS (SELECT * FROM ARTICLE A

WHERE Topic =‘Moto’ AND M.MID=A.MID) Find the names of the magazines for which do

not exist articles about motorcycles

(4)

DB M G

Exercise n. 2

Find the names of the magazines that have only ever published articles about motorcycles

MAGAZINE (MId, MName, Publisher) ARTICLE (AId, Title, Topic, MId)

SELECT MName

FROM MAGAZINE M, ARTICLE A

WHERE M.MID=A.MID and M.MID NOT IN (SELECT MID FROM ARTICLE

WHERE TOPIC <> ‘Motorcycle’) Set to be excluded: Magazines with at least an article on a topic different than Motorcycle

(5)

DB M G

Exercise n. 3

Find the names of the magazines that publish both articles about motorcycles and articles about cars.

MAGAZINE (MId, MName, Publisher) ARTICLE (AId, Title, Topic, MId)

SELECT MName FROM MAGAZINE M

WHERE MID IN (SELECT MID FROM ARTICLE

WHERE Topic =‘Motorcycle’) AND MID IN (SELECT MID

FROM ARTICLE

WHERE Topic =‘Car’)

(6)

DB M G

Exercise n. 3

Find the names of the magazines that publish both articles about motorcycles and articles about cars.

MAGAZINE (MId, MName, Publisher) ARTICLE (AId, Title, Topic, MId)

SELECT MName FROM MAGAZINE M

WHERE EXISTS (SELECT *

FROM ARTICLE A1

WHERE Topic =‘Motorcycle’

AND A1.MID=M.MID) AND EXISTS (SELECT *

FROM ARTICLE A2 WHERE Topic =‘Car’

AND A2.MID=M.MID)

Find the names of the magazines for which exist both articles about motorcycle and articles about cars

(7)

DB M G

Exercise n. 3 –Alternative solution

Find the names of the magazines that publish both articles about motorcycles and articles about cars.

MAGAZINE (MId, MName, Publisher) ARTICLE (AId, Title, Topic, MId)

SELECT MName

FROM MAGAZINE M, ARTICLE A

WHERE M.MID=A.MID AND Topic=‘Motorcycle’

AND M.MID IN (SELECT MID FROM ARTICLE

WHERE Topic =‘Car’)

(8)

DB M G

Exercise n. 3 –Alternative solution

Find the names of the magazines that publish both articles about motorcycles and articles about cars.

MAGAZINE (MId, MName, Publisher) ARTICLE (AId, Title, Topic, MId)

SELECT MName FROM MAGAZINE M

WHERE MID IN (SELECT MID FROM ARTICLE A

WHERE Topic=‘Motorcycle’) AND M.MID IN (SELECT MID

FROM ARTICLE A1 WHERE Topic =‘Car’

CORRELATION CONDITION)

(9)

DB M G

Exercise n. 4

Find the codes of the sailors who have never booked a red boat SAILOR (SId, SName, Expertise, DateofBirth)

BOOKING (SId, BId, Date) BOAT(Bid, BName, Color)

SELECT SID FROM SAILOR

WHERE SID NOT IN (SELECT SID

FROM BOOKING BK, BOAT BT

WHERE BK.BID= BT.BID AND Color =‘Red’) Set to be excluded: set of sailors who booked a red boat

(10)

DB M G

Exercise n. 4 – Alternative solution

Find the codes of the sailors who have never booked a red boat SAILOR (SId, SName, Expertise, DateofBirth)

BOOKING (SId, BId, Date) BOAT(Bid, BName, Color)

SELECT SID FROM SAILOR

WHERE SID NOT IN (SELECT SID FROM BOOKING BK

WHERE BK.BID IN (SELECT BID FROM BOAT

WHERE Color =‘Red’) Set to be excluded: set of sailors who booked a red boat

(11)

DB M G

Exercise n. 4 – Alternative solution

Find the codes of the sailors who have never booked a red boat SAILOR (SId, SName, Expertise, DateofBirth)

BOOKING (SId, BId, Date) BOAT(Bid, BName, Color)

SELECT SID FROM SAILOR S

WHERE NOT EXISTS (SELECT *

FROM BOOKING BK, BOAT BT

WHERE BK.BID=BT.BID AND Color=‘Red’

AND BK.SIS=S.SID)

Find the codes of the sailors for which do not exist a red boat booked

(12)

DB M G

Exercise n. 5a

Find the codes and the names of the sailors who have booked a red boat (only one) and a green boat (only one)

SAILOR (SId, SName, Expertise, DateofBirth) BOOKING (SId, BId, Date)

BOAT(Bid, BName, Color)

SELECT S.SID, SName FROM SAILOR

WHERE SID IN (SELECT SID

FROM BOOKING BK, BOAT BT

WHERE BK.BID=BT.BID AND Color =‘Red’

GROUP BY SID

HAVNG COUNT (DISTINCT BID)=1 ) AND S.SID IN (SELECT SID

FROM BOOKING BK1, BOAT BT1

WHERE BK1.BID=BT1.BID and Color = ‘green’

GROUP BY SID

HAVNG COUNT (DISTINCT BID)=1)

(13)

DB M G

Exercise n. 5b

Find the codes and the names of the sailors who have booked at least a red boat and at least a green boat

SAILOR (SId, SName, Expertise, DateofBirth) BOOKING (SId, BId, Date)

BOAT(Bid, BName, Color)

SELECT DISTINCT S.SID, SName

FROM BOOKING BK, SAILOR S, BOAT BT WHERE BK.BID=BT.BID AND S.SID=BK.SID

AND Color =‘Red’

AND S:SID IN (SELECT SID

FROM BOOKING BK1, BOAT BT1

WHERE BK1.BID=BT1.BID and Color = ‘green’)

(14)

DB M G

Exercise n. 5

Find the codes and the names of the sailors who have booked at least a red boat and at least a green boat

SAILOR (SId, SName, Expertise, DateofBirth) BOOKING (SId, BId, Date)

BOAT(Bid, BName, Color)

SELECT S.SID, SName FROM SAILOR

WHERE SID IN (SELECT SID

FROM BOOKING BK, BOAT BT

WHERE BK.BID=BT.BID AND Color =‘Red’ ) AND S.SID IN (SELECT SID

FROM BOOKING BK1, BOAT BT1

WHERE BK1.BID=BT1.BID and Color = ‘green’)

(15)

DB M G

Exercise n. 6

Find the codes and the names of the pilots who are qualified to fly on at least two aircrafts that can cover distances greater than 5,000 km

(MaximumRange>=5,000), and who are qualified to fly on a Boeing AIRCRAFT (AId, AName, MaximumRange)

CERTIFICATE (AId, PId) PILOT(Pid, PName, Salary)

SELECT P.PID, PName

FROM AIRCRAFT A, CERTIFICATE C, PILOT P WHERE A.AID=C.AID AND C.PID=P.PID

AND MaximumRange >= 5,000 AND P.PID IN (SELECT PID

FROM AIRCRAFT A1, CERTIFICATE C1 WHERE A1.Aid=C1.AID

AND AName =‘Boeing’) GROUP BY P.PID, PName

HAVING COUNT(*) >=2

(16)

DB M G

Exercise n. 6 – Alternative solution

Find the codes and the names of the pilots who are qualified to fly on at least two aircrafts that can cover distances greater than 5,000 km

(MaximumRange>=5,000), and who are qualified to fly on a Boeing AIRCRAFT (AId, AName, MaximumRange)

CERTIFICATE (AId, PId) PILOT(Pid, PName, Salary)

SELECT PID, PName FROM PILOT

WHERE PID IN (SELECT PID

FROM AIRCRAFT A, CERTIFICATE C

WHERE A.AID=C.AID AND MaximumRange >= 5,000 GROUP BY P.PID

HAVING COUNT(*) >=2) AND PID IN (SELECT PID

FROM AIRCRAFT A1, CERTIFICATE C1 WHERE A1.Aid=C1.AID

AND AName =‘Boeing’)

Riferimenti

Documenti correlati

SELECT DISTINCT PersonName FROM LEASING_CONTRACT LC GROUP BY PersonNamem, FCode HAVING COUNT(*)&gt;2.. FLAT (FCode, Address,

’Data Mining’, show the code of the author, the surname of the author, her/his university, and the total number of articles presented by the author in each edition of every

TEMPO (CodTempo, Mese, Trimestre, Semestre, Anno) FATTURATO (CodTempo, CodCliente, CodCatArticolo,. CodAgente, TotFatturato,

D102 Smith Computer engineering D105 Jones Computer engineering D104 White Electronics. ProfID PSurname

Fatturato(CodTempo, CodCliente, CodCatArticolo, CodAgente, TotFatturato, NumArticoli, TotSconto) Visualizzare per ogni provincia e mese. •

Scuola internazionale di Dottorato di ricerca in FORMAZIONE DELLA PERSONA E MERCATO DEL LAVORO.. C/o Università degli Studi di Bergamo,

Scuola internazionale di Dottorato di ricerca in FORMAZIONE DELLA PERSONA E MERCATO DEL LAVORO.. C/o Università degli Studi di Bergamo,

I dottorandi sono invitati a iscriversi tramite il modulo di adesione sulla piattaforma