• 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!
10
0
0

Testo completo

(1)

DB M G

SQL Language

Queries in SQL

Tania Cerquitelli

(2)

DB M G

Exercise n.1

Find the identifiers and the names of the magazines that have published at least one article about motorcycles

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

SELECT DISTINCT M.MID, MName FROM MAGAZINE M, ARTICLE A

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

-- add comment in a single line /* COMMENTS in MANY LINES */

(3)

DB M G

Exercise n.2

Find the identifiers and the names of the magazines that publish articles about motorcycles or cars

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

SELECT DISTINCT M.MID, MName FROM MAGAZINE M, ARTICLE A WHERE M.MID=A.MID AND

( Topic =‘Cars’ OR Topic =‘Motorcycle’ )

(4)

DB M G

Exercise n.3

Find the identifiers and the names of the magazines that have published at least two articles about motorcycles

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

SELECT M.MID, MName, Publisher FROM MAGAZINE M, ARTICLE A

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

GROUP BY M.MID, Mname, Publisher HAVING COUNT (*) >=2

(5)

DB M G

Exercise n.4

Find the identifiers and the names of the magazines that have published only one article about motorcycles (i.e., they may have published any articles about other topics)

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

SELECT M.MID, MName

FROM MAGAZINE M, ARTICLE A

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

GROUP BY M.MID, Mname HAVING COUNT (*) =1

(6)

DB M G

Exercise n. 5

Given the relational schema including the following tables (primary keys are underlined)

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

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

BOAT(Bid, BName, Color)

SELECT DISTINCT S.SID, SNAme

FROM SAILOR S, BOOKING BK, BOAT B

WHERE B.Bid=BK.Bid AND S.Sid=BK.Sid AND (Color=‘red’ OR Color=‘Green’)

(7)

DB M G

Exercise n. 6

Find the codes and the names of the sailors who have booked at least two boats

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

BOAT(Bid, BName, Color)

SELECT S.SID, SName

FROM SAILOR S, BOOKING BK WHERE S.Sid=BK.Sid

GROUP BY S.SID, SName

HAVING COUNT(DISTINCT Bid)>=2

(8)

DB M G

Exercise n. 7

Find the codes and the names of the sailors who have performed three bookings

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

BOAT(Bid, BName, Color)

SELECT S.SID, SName

FROM SAILOR S, BOOKING BK WHERE S.Sid=BK.Sid

GROUP BY S.SID, SName HAVING COUNT(*) =3

(9)

DB M G

Exercise n. 8

Find the codes and the names of the pilots who are qualified to fly on an aircraft that can cover distances greater than 5,000 km (MaximumRange>=5,000)

AIRCRAFT (AId, AName, MaximumRange) CERTIFICATE (AId, PId)

PILOT(PId, PName, Salary)

SELECT DISTINCT PNAME, P.PID

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

AND MaximumRange >= 5,000

(10)

DB M G

Exercise n.9

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) 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 GROUP BY P.PID, PName

HAVING COUNT(*) >=2

Riferimenti

Documenti correlati

(c) Trovare il nome del meccanico e il numero totale di riparazioni del meccanico che ha effeJuato più riparazioni totali tra quelli che hanno effeJuato almeno una riparazione di

(a) Find the codes and the names of the pilots who are qualified to fly on an aircraft that can cover distances greater than 5,000 km (MaximumRange ≥ 5,000). (b) Find the codes and

Find the codes of the sailors who have never booked a red boat SAILOR (SId, SName, Expertise, DateofBirth). BOOKING (SId, BId, Date) BOAT(Bid,

SELECT DISTINCT PersonName FROM LEASING_CONTRACT LC GROUP BY PersonNamem, FCode HAVING COUNT(*)>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. •