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

Testo completo

(1)

DB M G

SQL language

Queries in SQL

Tania Cerquitelli

(2)

DB M G

Exercise n. 1

Find the rooms in which none of the first-year courses has ever been given COURSE (CourseCode, CourseName, Year, Semester)

COURSE-SCHEDULE (CourseCode, DayOfWeek, StartTime, EndTime, Room)

Set of data to be excluded: Set of ROOMS used for a lecture related to the first year course

SELECT DISTINCT Room FROM COUSE-SCHEDULE CS

WHERE Room NOT IN (SELECT Room

FROM COURSE-SCHEDULE CS1, COURSE C

WHERE CS1.CourseCode=C.CourseCode AND Year =1)

(3)

DB M G

Exercise n. 1

Find the rooms in which none of the first-year courses has ever been given COURSE (CourseCode, CourseName, Year, Semester)

COURSE-SCHEDULE (CourseCode, DayOfWeek, StartTime, EndTime, Room)

Find the rooms for which do not exist a tuple in table course schedule used for the same room and related to a lecture for the first year course

SELECT DISTINCT Room FROM COUSE-SCHEDULE CS WHERE NOT EXISTS (SELECT *

FROM COURSE-SCHEDULE CS1, COURSE C

WHERE CS1.CourseCode=C.CourseCode AND Year =1 AND CS.Room=CS1.Room)

(4)

DB M G

Exercise n. 2

Find the codes, the names and the total number of weekly hours of the third- year courses whose total number of weekly hours is greater than 10 and whose schedule spans three different days of the week.

COURSE (CourseCode, CourseName, Year, Semester)

COURSE-SCHEDULE (CourseCode, DayOfWeek, StartTime, EndTime, Room)

SELECT C.CourseCode, CourseName, SUM(EndTime-StartTime) FROM COURSE C, COURSE-SCHEDULE CS

WHERE C.CourseCode=CS.CourseCode AND Year = 3 GROUP BY C.CourseCode, CourseName

HAVING SUM(EndTime-StartTime)>10 AND COUNT(DISTINCT DayOfWeek)=3

(5)

DB M G

Exercise n. 3

FLAT (FCode, Address, City, Surface)

LEASING-CONTRACT (LCCode, StartDate, EndDate, PersonName, MonthlyPrice, FCode)

For the cities in which at least 100 contracts have been signed, find the city, the maximum monthly price, the average monthly price, the maximum

duration of the leasing contracts, the average duration of the leasing contracts and the total number of signed contracts.

SELECT F.City, MAX(L.MonthlyPrice), AVG(L.MonthlyPrice),

MAX(L.EndDate-L.StartDate), AVG(L.EndDate-L.StartDate), COUNT(*) FROM FLAT F, LEASING-CONTRACT L

WHERE F.FCode=L.Fcode [AND EndDate IS NOT NULL]

GROUP BY F.City

HAVING COUNT (*)>=100

(6)

DB M G

Exercise n. 4

FLAT (FCode, Address, City, Surface)

LEASING-CONTRACT (LCCode, StartDate, EndDate, PersonName, MonthlyPrice, Fcode)

Find the names of the people who have never rented any flat with a surface greater than 80 square meters

SELECT DISTINCT PersonName FROM LEASING_CONTRACT LC WHERE PersonName NOT IN (

SELECT PersonName

FROM LEASING_CONTRACT LC1, FLAT F

WHERE F.Surface>80 AND LC1.FCode=F.Fcode) SELECT DISTINCT PersonName

FROM LEASING_CONTRACT LC WHERE NOT EXIST (

SELECT *

FROM LEASING_CONTRACT LC1, FLAT F

WHERE F.Surface>80 AND LC1.FCode=F.Fcode AND LC.PersonName=LC1.PersonName)

(7)

DB M G

Exercise n. 5

FLAT (FCode, Address, City, Surface)

LEASING-CONTRACT (LCCode, StartDate, EndDate, PersonName, MonthlyPrice, Fcode)

Find the names of the people who have signed more than two leasing contracts for the same flat (in different periods).

SELECT DISTINCT PersonName FROM LEASING_CONTRACT LC GROUP BY PersonNamem, FCode HAVING COUNT(*)>2

(8)

DB M G

Exercise n. 6

FLAT (FCode, Address, City, Surface)

LEASING-CONTRACT (LCCode, StartDate, EndDate, PersonName, MonthlyPrice, Fcode)

Find the codes and the addresses of flats in Turin whose monthly leasing price has always been greater than 500 Euro and for which more than 5 contracts have been signed.

SELECT FCode, Address FROM FLAT F

WHERE City=‘Turin’ AND Fcode NOT IN (SELECT FCode

FROM LEASING-CONTRACT WHERE MonthlyPrice<=500) AND Fcode IN (SELECT FCode

FROM LEASING-CONTRACT GROUP BY Fcode

HAVING COUNT(*)>5)

(9)

DB M G

Exercise n. 6 - Alternative

FLAT (FCode, Address, City, Surface)

LEASING-CONTRACT (LCCode, StartDate, EndDate, PersonName, MonthlyPrice, Fcode)

Find the codes and the addresses of flats in Turin whose monthly leasing price has always been greater than 500 Euro and for which more than 5 contracts have been signed.

SELECT F.FCode, Address

FROM FLAT F, LEASING-CONTRACT LC

WHERE City=‘Turin’ AND F.FCode NOT IN (SELECT FCode

FROM LEASING-CONTRACT WHERE MonthlyPrice<=500) AND LC.Fcode=F.Fcode

GROUP BY F.Fcode, Address HAVING COUNT(*)>5

(10)

DB M G

Exercise n. 6 - Alternative

FLAT (FCode, Address, City, Surface)

LEASING-CONTRACT (LCCode, StartDate, EndDate, PersonName, MonthlyPrice, Fcode)

Find the codes and the addresses of flats in Turin whose monthly leasing price has always been greater than 500 Euro and for which more than 5 contracts have been signed.

SELECT F.FCode, Address

FROM FLAT F, LEASING-CONTRACT LC WHERE City=‘Turin’ AND LC.Fcode=F.Fcode GROUP BY F.Fcode, Address

HAVING COUNT(*)>5 AND MIN(MonthlyPrice)>500

(11)

DB M G

Exercise n. 7

PERSON (Name, Sex, Age)

PARENT (ParentName, ChildName)

Find the name of each person younger than 10 years old who is an only child

SELECT DISTINCT Name FROM PERSON P, PARENT PA

WHERE PA.ChildName=P.Name AND Age<10

AND ParentName IN (SELECT ParentName FROM PARENT

GROUP BY ParentName Having Count(*)=1)

(12)

DB M G

Exercise n. 7 - Alternative

PERSON (Name, Sex, Age)

PARENT (ParentName, ChildName)

Find the name of each person younger than 10 years old who is an only child

SELECT DISTINCT Name FROM PERSON P, PARENT PA

WHERE PA.ChildName=P.Name AND Age<10

AND ParentName NOT IN (SELECT ParentName FROM PARENT

GROUP BY ParentName Having Count(*)>1)

(13)

DB M G

Exercise n. 7 - Alternative

PERSON (Name, Sex, Age)

PARENT (ParentName, ChildName)

Find the name of each person younger than 10 years old who is an only child

SELECT DISTINCT Name FROM PERSON P, PARENT PA

WHERE PA.ChildName=P.Name AND Age<10 AND NOT EXISTS (SELECT *

FROM PARENT PA1

WHERE PA1.ParentName=PA.ParentName AND PA1.ChildName<> PA.ChildName)

Riferimenti

Documenti correlati

Find the names of the suppliers that supply at least one product supplied by suppliers of red products. D B

it is sufficient to specify in which host language variable the result of the command shall be stored If an SQL command returns a table (i.e., a set of tuples). a method is

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&gt;=5,000). AIRCRAFT (AId,

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

’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

SQL MongoDB WHERE $match GROUP BY $group HAVING $match SELECT $project ORDER BY $sort. LIMIT $limit SUM $sum

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

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