0% found this document useful (0 votes)
23 views8 pages

Northwind Correction SQL

Uploaded by

Asmaë Asmaë
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Northwind Correction SQL

Uploaded by

Asmaë Asmaë
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Base de données Northwind

SQL
1. select FirstName, LastName, Address, City, Region
from Employees

2. select distinct FirstName, LastName, Customers.CompanyName


from Employees, Orders, Customers, Shippers
where Employees.EmployeeID = Orders.EmployeeID
and Orders.CustomerID = Customers.CustomerID
and ShipVia = ShipperID and Customers.City = ’Bruxelles’
and Shippers.CompanyName = ’Speedy Express’

3. select distinct Title, FirstName, LastName


from Employees, Orders, [Order Details], Products
where Employees.EmployeeID = Orders.EmployeeID
and Orders.OrderID = [Order Details].OrderID
and [Order Details].ProductID = Products.ProductID
and ( ProductName = ’Gravad Lax’ or ProductName = ’Mishi Kobe Niku’ )

4. select distinct E.Title, E.LastName, M.Title, M.LastName


from Employees E, Employees M
where E.ReportsTo = M.EmployeeID
union
select distinct E.Title, E.LastName, NULL, NULL
from Employees E
where ReportsTo IS NULL
Autre version

select distinct E.Title, E.LastName, M.Title, M.LastName


from Employees E left outer join Employees M on E.ReportsTo = M.EmployeeID

5. select distinct C.CompanyName, ProductName, S.CompanyName


from Customers C, Orders O, [Order Details] D, Products P, Suppliers S
where C.City = ’London’ and C.CustomerID = O.CustomerID
and O.OrderID = D.OrderID and D.ProductID = P.ProductID
and P.SupplierID = S.SupplierID
and ( S.CompanyName = ’Pavlova, Ltd.’ or S.CompanyName = ’Karkki Oy’ )

6. select P.ProductName
from Employees E, Orders O, [Order Details] D, Products P
where E.EmployeeID = O.EmployeeID
and O.OrderID = D.OrderID
and D.ProductID = P.ProductID
and E.City = ’London’
union

1
select P.ProductName
from Customers C, Orders O, [Order Details] D, Products P
where C.CustomerID = O.CustomerID
and O.OrderID = D.OrderID
and D.ProductID = P.ProductID
and C.City = ’London’
Autre version

select P.ProductName
from Products P
where P.ProductID in
( select D.ProductID
from Employees E, Orders O, [Order Details] D
where E.EmployeeID = O.EmployeeID
and O.OrderID = D.OrderID
and E.City = ’London’ )
OR P.ProductID in
( select D.ProductID
from Customers C, Orders O, [Order Details] D
where C.CustomerID = O.CustomerID
and O.OrderID = D.OrderID
and C.City = ’London’ )

Autre version

select distinct P.ProductName


from Employees E, Orders O, [Order Details] D, Products P, Customers C
where E.EmployeeID = O.EmployeeID
and C.CustomerID = O.CustomerID
and O.OrderID = D.OrderID
and D.ProductID = P.ProductID
and (E.City = ’London’ or C.City = ’London’)

7. (a) select E1.FirstName, E1.LastName


from Employees E1
where E1.BirthDate < any
( select E2.BirthDate
from Employees E2
where E2.City = ’London’ )
(b) select E1.FirstName, E1.LastName
from Employees E1
where E1.BirthDate < all
( select E2.BirthDate
from Employees E2
where E2.City = ’London’)

2
8. select E1.FirstName, E1.LastName
from Employees E1
where E1.HireDate < all
( select E2.HireDate
from Employees E2
where E2.City = ’London’ )
9. select distinct E.LastName, E.City
from Employees E, Orders O, Customers C
where E.EmployeeID = O.EmployeeID
and O.CustomerID = C.CustomerID
and E.City = C.City
Autre version avec in

select E1.FirstName, E1.LastName


from Employees E
where E.EmployeeID in
( select O.EmployeeID
from Orders O, Customers C
where E.EmployeeID = O.EmployeeID
and O.CustomerID = C.CustomerID
and E.City = C.City )

Autre version avec exists

select distinct E.LastName, E.City


from Employees E
where exists
( select *
from Orders O, Customers C
where E.EmployeeID = O.EmployeeID
and O.CustomerID = C.CustomerID
and E.City = C.City )

10. select distinct C.CompanyName


from Customers C
where not exists
( select *
from Orders O
where C.CustomerID = O.CustomerID )
Autre version avec not in

select distinct C.CompanyName


from Customers C
where C.CustomerID not in
( select O.CustomerID
from Orders O )

3
11. select C.CompanyName
from Customers C
where not exists
( select *
from Products P
where UnitPrice < 5
and not exists
( select * from
Orders O, [Order Details] D
where C.CustomerID = O.CustomerID
and O.OrderID = D.OrderID
and P.ProductID = D.ProductID ) )
Autre version avec group by et having

select distinct C.CompanyName


from Customers C, Orders O, [Order Details] D, Products P
where C.CustomerID = O.CustomerID
and O.OrderID = D.OrderID
and D.ProductID = P.ProductID
and P.UnitPrice < 5
group by C.CustomerID, C.CompanyName
having count(distinct D.ProductID) =
( select count(*)
from Products P2 where UnitPrice < 5 )

12. select P.ProductName


from Products P
where not exists
( select *
from Employees E
where not exists
( select * from
Orders O, [Order Details] D
where E.EmployeeID = O.EmployeeID
and O.OrderID = D.OrderID
and P.ProductID = D.ProductID ) )
Autre version avec group by et having

select distinct P.ProductName


from Products P
where P.ProductID in
( select D.ProductID
from Orders O, [Order Details] D
where O.OrderID = D.OrderID
group by D.ProductID

4
having count(distinct O.EmployeeID) =
( select count(*)
from Employees ) )

13. select C.CustomerID, C.CompanyName


from Customers C
where not exists
( select *
from Orders O1, [Order Details] D1
where O1.OrderID = D1.OrderID and O1.CustomerID = ’LAZYK’
and not exists
( select *
from Orders O2, [Order Details] D2
where C.CustomerID = O2.CustomerID and O2.OrderID = D2.OrderID
and D1.ProductID = D2.ProductID ) )
order by C.CustomerID
Autre version

select C.CustomerID, C.CompanyName


from Customers C
where CustomerID <> ’LAZYK’
and not exists
( select *
from [Order Details] D1
where D1.ProductID in
( select D2.ProductID
from Orders O2, [Order Details] D2
where O2.OrderID = D2.OrderID
and O2.CustomerID = ’LAZYK’ )
and not exists
( select *
from Orders O3, [Order Details] D3
where C.CustomerID = O3.CustomerID
and O3.OrderID = D3.OrderID
and D1.ProductID = D3.ProductID ) )
order by C.CustomerID

14. select C.CustomerID, C.CompanyName


from Customers C
where CustomerID <> ’LAZYK’
and not exists
( select *
from Orders O1, [Order Details] D1
where O1.OrderID = D1.OrderID and O1.CustomerID = ’LAZYK’
and not exists

5
( select *
from Orders O2, [Order Details] D2
where C.CustomerID = O2.CustomerID and O2.OrderID = D2.OrderID
and D1.ProductID = D2.ProductID ) )
and not exists
( select *
from Orders O3, [Order Details] D3
where C.CustomerID = O3.CustomerID and O3.OrderID = D3.OrderID
and not exists
( select *
from Orders O4, [Order Details] D4
where O4.CustomerID = ’LAZYK’ and O4.OrderID = D4.OrderID
and D3.ProductID = D4.ProductID ) )
order by C.CustomerID

15. select CategoryID, ’Avg’ = avg(UnitPrice)


from Products
group by CategoryID

16. select C.CategoryName, avg(P.UnitPrice)


from Products P, Categories C
where P.CategoryID = C.CategoryID
group by C.CategoryName

17. select S.SupplierID, S.CompanyName


from Suppliers S, Products P
where S.SupplierID = P.SupplierID
group by S.SupplierID, S.CompanyName
having count(*) > 3

18. select E.EmployeeID, E.LastName,


’Sales’ = sum((D.UnitPrice*D.Quantity)*(1-Discount))
from Employees E, Orders O, [Order Details] D
where E.EmployeeID = O.EmployeeID
and O.OrderID = D.OrderID
group by E.EmployeeID, E.LastName
order by E.EmployeeID

19. select E.EmployeeID, E.LastName,


’Sales’ = sum((D.UnitPrice*D.Quantity)*(1-Discount))
from Employees E, Orders O, [Order Details] D
where E.EmployeeID = O.EmployeeID
and O.OrderID = D.OrderID
group by E.EmployeeID, E.LastName
having count(distinct D.ProductID) > 70
order by E.EmployeeID

6
20. select E.FirstName, E.LastName
from Employees E
where E.EmployeeID in
( select distinct O.EmployeeID
from Orders O, [Order Details] D, Products P
where O.OrderID = D.OrderID
and D.ProductID = P.ProductID
group by O.EmployeeID
having count(distinct P.SupplierID)>7 )
Autre version

select E.FirstName, E.LastName


from Employees E, Orders O, [Order Details] D, Products P
where E.EmployeeID = O.EmployeeID
and O.OrderID = D.OrderID
and D.ProductID = P.ProductID
group by O.EmployeeID, E.FirstName, E.LastName
having count(distinct P.SupplierID)>7 )

21. select distinct C.CompanyName, P.ProductName


from Customers C, Orders O, [Order Details] D1, Products P
where C.CustomerID = O.CustomerID
and O.OrderID = D1.OrderID
and D1.ProductID = P.ProductID
and D1.Quantity >
( select 5*avg(Quantity)
from [Order Details] D2
where D1.ProductID = D2.ProductID )
order by C.CompanyName, P.ProductName
Autre version de la requête où la somme totale doit être supérieure à 5 fois la
moyenne

select C.CompanyName, P.ProductName


from Customers C, Orders O, [Order Details] D1, Products P
where C.CustomerID = O.CustomerID
and O.OrderID = D1.OrderID
and D1.ProductID = P.ProductID
group by C.CompanyName, P.ProductID, P.ProductName
having sum(D1.Quantity) >
( select 5*avg(Quantity)
from [Order Details] D2
where P.ProductID = D2.ProductID )
order by C.CompanyName, P.ProductName

7
Requête Nombre Réponses
1 9
2 2
3 6
4 9
5 9
6 76
7 a) 8
b) 3
8 4
9 6
10 2
11 9
12 27
13 10
14 0
15 8
16 8
17 4
18 9
Davolio 192 107
19 3
20 9
21 3

You might also like