59
INFO0009 - Bases de donn´ ees ep´ etition 7 : Le langage SQL (2) Maxime Meurisse (slides de Remy Vandaele) [email protected] 23 mars 2020

INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

INFO0009 - Bases de donnees

Repetition 7 : Le langage SQL (2)

Maxime Meurisse (slides de Remy Vandaele)[email protected]

23 mars 2020

Page 2: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

2/59

Creation de tables

Format general :

CREATE TABLE IF NOT EXISTS <NOM_REL.>(

<nom_att1> <type_att1> <option_att1>,

...

<nom_attN> <type_attN> <option_attN>,

PRIMARY KEY(<nom_attI>),

FOREIGN KEY(<nom_attJ>) REFERENCES <REL.>(<nom_attFJ>),

...

FOREIGN KEY(<nom_attK>) REFERENCES <REL.>(<nom_attFK>)

)ENGINE=InnoDB;

Type : INT,BIGINT,VARCHAR,DATE,...Options : NOT NULL, PRIMARY KEY, AUTO INCREMENT,...

Page 3: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

3/59

Exercices

Creer les tables suivantes :

I Client(id client, prenom client, nom client, nationalite client)

I Commande(id commande,#id client, date commande)

I Produit(id produit, nom produit)

I Employe(id employe, nom employe, prenom employe)

I DetailsCommande(#id commande,# id produit,#id employe, quantite, prix piece)

I Paiement(#id commande, montant)

Page 4: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

4/59

Creation de tables

Client(id client, prenom client, nom client, nationalite client)

CREATE TABLE IF NOT EXISTS Client(

Page 5: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

5/59

Creation de tables

Client(id client, prenom client, nom client, nationalite client)

CREATE TABLE IF NOT EXISTS Client(

id_client INT AUTO_INCREMENT,

Page 6: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

6/59

Creation de tables

Client(id client, prenom client, nom client, nationalite client)

CREATE TABLE IF NOT EXISTS Client(

id_client INT AUTO_INCREMENT,

prenom_client VARCHAR(50) NOT NULL,

nom_client VARCHAR(50) NOT NULL,

nationalite_client VARCHAR(50) NOT NULL,

Page 7: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

7/59

Creation de tables

Client(id client, prenom client, nom client, nationalite client)

CREATE TABLE IF NOT EXISTS Client(

id_client INT AUTO_INCREMENT,

prenom_client VARCHAR(50) NOT NULL,

nom_client VARCHAR(50) NOT NULL,

nationalite_client VARCHAR(50) NOT NULL,

PRIMARY KEY(id_client)

Page 8: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

8/59

Creation de tables

Client(id client, prenom client, nom client, nationalite client)

CREATE TABLE IF NOT EXISTS Client(

id_client INT AUTO_INCREMENT,

prenom_client VARCHAR(50) NOT NULL,

nom_client VARCHAR(50) NOT NULL,

nationalite_client VARCHAR(50) NOT NULL,

PRIMARY KEY(id_client)

)ENGINE=InnoDB;

Page 9: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

9/59

Exercices

Creer les tables suivantes :

I Client(id client, prenom client, nom client, nationalite client)

I Commande(id commande,#id client, date commande)

I Produit(id produit, nom produit)

I Employe(id employe, nom employe, prenom employe)

I DetailsCommande(#id commande,# id produit, #id employe, quantite,prix piece)

I Paiement(#id commande, montant)

CREATE TABLE IF NOT EXISTS <NOM_REL.>(

<nom_att1> <type_att1> <option_att1>,

...

<nom_attN> <type_attN> <option_attN>,

PRIMARY KEY(nom_attI),

FOREIGN KEY(nom_attJ) REFERENCES <REL.>(<nom_attJ>),

...

FOREIGN KEY(nom_attK) REFERENCES <REL.>(<nom_attK>)

)ENGINE=InnoDB;

Page 10: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

10/59

Commande(id commande, #id client, date commande)

CREATE TABLE IF NOT EXISTS Commande(

id_commande INT AUTO_INCREMENT,

id_client INT NOT NULL,

date_commande DATE NOT NULL,

PRIMARY KEY(id_commande),

FOREIGN KEY(id_client) REFERENCES Client(id_client)

)ENGINE=InnoDB;

Page 11: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

11/59

Produit(id produit, nom produit)

CREATE TABLE IF NOT EXISTS Produit(

id_produit INT AUTO_INCREMENT,

nom_produit VARCHAR(50) NOT NULL,

PRIMARY KEY(id_produit)

)ENGINE=InnoDB;

Page 12: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

12/59

Employe(id employe, nom employe, prenom employe)

CREATE TABLE IF NOT EXISTS Employe(

id_employe INT AUTO_INCREMENT,

nom_employe VARCHAR(50) NOT NULL,

prenom_employe VARCHAR(50) NOT NULL,

PRIMARY KEY(id_employe)

)ENGINE=InnoDB;

Page 13: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

13/59

DetailsCommande(#id commande,#id produit,#id employe,quantite,prix piece)

CREATE TABLE IF NOT EXISTS DetailsCommande(

id_commande INT NOT NULL,

id_produit INT NOT NULL,

id_employe INT NOT NULL,

quantite INT NOT NULL,

prix_piece FLOAT NOT NULL,

PRIMARY KEY (id_commande,id_produit),

FOREIGN KEY(id_commande)

REFERENCES Commande(id_commande),

FOREIGN KEY(id_produit)

REFERENCES Produit(id_produit),

FOREIGN KEY(id_employe)

REFERENCES Employe(id_employe)

)ENGINE=InnoDB;

Page 14: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

14/59

Paiement(#id commande , montant)

CREATE TABLE IF NOT EXISTS Paiement(

id_commande INT NOT NULL,

montant FLOAT NOT NULL,

PRIMARY KEY id_commande,

FOREIGN KEY(id_commande)

REFERENCES Commande(id_commande)

)ENGINE=InnoDB;

Page 15: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

15/59

Exercice 12. Requetes

Page 16: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

16/59

Exercice 12. Requetes

Format de base d’une requete :

SELECT <att_i>,...,<att_k> | *

FROM <nom_table>

I Selectionne tous les tuples de la tableI Les tuples sont composes des attributs (noms de colonnes)

I Tous si *

I Possible d’utiliser une fonction dans le select (DISTINCT,COUNT, SUM, AVG,...)

I Le resultat d’une requete sera une table

Page 17: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

17/59

Exercice 12. Requetes

Table ETUDIANT :

Nom Prenom AgeDupont Georges 17Dupont Henri 16Durant Francis 17Durant Gustave 17

Leclercq Alain 14

SELECT DISTINCT Nom

FROM ETUDIANT

NomDupontDurant

Leclercq

Page 18: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

18/59

Exercice 12. Requetes

Table ETUDIANT :

Nom Prenom AgeDupont Georges 17Dupont Henri 16Durant Francis 17Durant Gustave 17

Leclercq Alain 14

SELECT AVG(Age)

FROM ETUDIANT

AVG(Age)16.2

Page 19: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

19/59

Exercice 12. Requetes

AS :

SELECT <att_i> AS nouveau_nom_colonne

FROM <nom_table>

I AS permet de renommer une colonne

I Utile pour les jointures et les unions

Page 20: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

20/59

Exercice 12. Requetes

Exemple :

Table ETUDIANT :

Nom Prenom AgeDupont Georges 17Dupont Henri 16Durant Francis 17Durant Gustave 17

Leclercq Alain 14

SELECT DISTINCT(Nom) AS NOMDIFF

FROM ETUDIANT

NOMDIFFDupontDurant

Leclercq

Page 21: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

21/59

Exercice 12. Requetes

WHERE :

SELECT *

FROM <nom_table>

WHERE CSTR(column)

WHERE impose des contraintes sur les tuples en fonction desvaleurs des attributs

I >,<,=,(NOT ) IN, (NOT ) EXISTS

Page 22: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

22/59

Exercice 12. Requetes

Table ETUDIANT :

Nom Prenom AgeDupont Georges 17Dupont Henri 16Durant Francis 17Durant Gustave 17

Leclercq Alain 14

SELECT *

FROM ETUDIANT

WHERE Nom="Dupont"

Nom Prenom AgeDupont Georges 17Dupont Henri 16

Page 23: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

23/59

Exercice 12. Requetes

TABLE ETUDIANT : TABLE INTERRO :

ID Nom Prenom1 Leclercq Alain2 Durant Georges3 Vermeulen Eddy4 Delbecq Nadine

ID Cours Points1 Math 162 Francais 123 Geographie 174 Francais 143 Math 11

SELECT Nom,Prenom

FROM ETUDIANT

WHERE ID IN

SELECT ID FROM INTERRO WHERE Points>15

Nom PrenomLeclercq Alain

Vermeulen Eddy

Page 24: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

24/59

Exercice 12. Requetes

TABLE ETUDIANT : TABLE INTERRO :

ID Nom Prenom1 Leclercq Alain2 Durant Georges3 Vermeulen Eddy4 Delbecq Nadine

ID Cours Points1 Math 162 Francais 123 Geographie 174 Francais 143 Math 11

SELECT Nom,Prenom

FROM ETUDIANT e

WHERE NOT EXISTS

SELECT * FROM INTERRO WHERE (Points<15 AND e.ID=ID)

Nom PrenomLeclercq Alain

Page 25: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

25/59

Exercice 12. Requetes

SELECT *

FROM <nom_table_1> NATURAL JOIN <nom_table_2>

NATURAL JOIN fait le produit cartesien des entrees de deuxtables en se limitant aux tuples dont les entrees ont la memevaleur pour les attributs de meme nom.

Page 26: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

26/59

Exercice 12. Requetes

TABLE ETUDIANT : TABLE INTERROS :

ID Nom Prenom1 Leclercq Alain2 Durant Georges

ID Cours Points1 Math 171 Francais 102 Geographie 142 Francais 12

SELECT *

FROM ETUDIANT NATURAL JOIN INTERROS

ID Nom Prenom Cours Points1 Leclercq Alain Math 171 Leclercq Alain Francais 122 Durant Georges Geographie 142 Durant Georges Francais 12

Page 27: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

27/59

Exercice 12. Requetes

SELECT *

FROM <NOM_TABLE>

GROUP BY <colonne>

GROUP BY regroupe les resultats par un ou plusieurs attributs dememe valeur

Page 28: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

28/59

Requetes

TABLE ETUDIANT :

Nom Prenom Age PointsDupont Georges 17 12Dupont Henri 16 13Durant Francis 17 15Durant Gustave 17 14

Leclercq Alain 14 12

SELECT Nom, SUM(Age), AVG(Points)

FROM ETUDIANT

GROUP BY Nom

Nom SUM(Age) AVG(Points)Dupont 33 12.5Durant 34 14.5

Leclercq 14 12

Page 29: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

29/59

Exercice 12. Requetes

SELECT *

FROM <NOM_TABLE>

GROUP BY <colonne>

HAVING contrainte_groupe

HAVING permet de restreindre les groupes en leur imposant descontraintes

Page 30: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

30/59

Requetes

TABLE ETUDIANT :

Nom Prenom Age PointsDupont Georges 17 12Dupont Henri 16 13Durant Francis 17 15Durant Gustave 17 14

Leclercq Alain 14 12

SELECT Nom, SUM(Age), AVG(Points)

FROM ETUDIANT

GROUP BY Nom

HAVING AVG(Points)<13

Nom SUM(Age) AVG(Points)Dupont 33 12.5Leclercq 14 12

Page 31: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

31/59

Exercice 12. Requetes

TABLE_1 UNION TABLE_2

UNION permet de regrouper deux tables en une seule (si lescolonnes sont de meme nom)

Page 32: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

32/59

Exercice 12. Requetes

TABLE Etudiant : TABLE Professeur :

Nom Prenom Grade

Dupont Henri PGDVermeiren Nadine S

Nom Prenom CoursDelobelle Peter Informatique-1

Deroy Marc Analyse numerique

(SELECT Nom,Prenom

FROM Etudiant)

UNION

(SELECT Nom,Prenom

FROM Professeur)

Nom PrenomDupont Henri

Vermeiren NadineDelobelle Peter

Deroy Marc

Page 33: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

33/59

Exercices

I Client(id client, prenom client, nom client, nationalite client)

I Commande(id commande,id client, date commande)

I Produit(id produit, nom produit)

I Employe(id employe, nom employe, prenom employe)

I DetailsCommande(id commande, id produit, id employe, quantite,prix piece)

I Paiement(id commande, montant)

a) Pour chaque client : nom, prenom et nombre de commandes passeesb) Chercher les commandes passees par des belges ou francaisc) Donner le cout total pour chaque commanded) Pour chaque client : nom, prenom, cout total de ses commandese) Pour chaque client : nom, prenom, cout moyen de ses commandes

Page 34: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

34/59

a) Pour chaque client : nom, prenom et nombre decommandes passees

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)

1. Les clients qui ont passe au moins une commande

SELECT prenom_client, nom_client, cnt

FROM (Client NATURAL JOIN (SELECT id_client, COUNT(*) AS cnt

FROM Commande

GROUP BY id_client));

2. Les clients qui n’ont pas passe de commande

SELECT prenom_client, nom_client, 0 AS cnt

FROM Client

WHERE id_client NOT IN

(SELECT DISTINCT id_client

FROM Commande)

Page 35: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

35/59

a) Pour chaque client : nom, prenom et nombre decommandes passees

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)

3. Union

(SELECT prenom_client, nom_client, cnt

FROM (Client NATURAL JOIN (SELECT id_client, COUNT(*) AS cnt

FROM Commande

GROUP BY id_client))

UNION

(SELECT prenom_client, nom_client, 0 AS cnt

FROM Client

WHERE id_client NOT IN

(SELECT DISTINCT id_client

FROM Commande));

Page 36: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

36/59

Exercices

I Client(id client, prenom client, nom client, nationalite client)

I Commande(id commande,id client, date commande)

I Produit(id produit, nom produit)

I Employe(id employe, nom employe, prenom employe)

I DetailsCommande(id commande, id produit, id employe, quantite,prix piece)

I Paiement(id commande, montant)

a) Pour chaque client : nom, prenom et nombre de commandes passeesb) Chercher les commandes passees par des belges ou francaisc) Donner le cout total pour chaque commanded) Pour chaque client : nom, prenom, cout total de ses commandese) Pour chaque client : nom, prenom, cout moyen de ses commandes

Page 37: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

37/59

b) Chercher les commandes passees par des clients belgesou francais

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)

SELECT id_client

FROM Client

WHERE nationalite_client="Belge"

OR nationalite_client="Francais"

Page 38: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

38/59

b) Chercher les commandes passees par des clients belgesou francais

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)

SELECT id_commande

FROM (Commande NATURAL JOIN (SELECT id_client

FROM Client

WHERE nationalite_client="Belge"

OR nationalite_client="Francais"));

Page 39: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

39/59

c) Donner le cout total pour chaque commande

DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

SELECT id_commande, SUM(prix_piece*quantite) AS cout_total

FROM DetailsCommande

GROUP BY id_commande;

Page 40: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

40/59

d) Pour chaque client : nom, prenom, cout total de sescommandes

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

1. Cout de chaque commande

SELECT id_commande, SUM(prix_piece*quantite) AS cout

FROM DetailsCommande

GROUP BY id_commande;

2. Cout total de chaque id client

SELECT id_client, SUM(cout) AS cout_client

FROM Commande NATURAL JOIN (SELECT id_commande, SUM(prix_piece*quant.) AS cout

FROM DetailsCommande

GROUP BY id_commande)

GROUP BY id_client

Page 41: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

41/59

d) Pour chaque client : nom, prenom, cout total de sescommandes

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

3. Natural Join avec client pour le prenom

SELECT nom_client, prenom_client, cout_client

FROM Client NATURAL JOIN

(SELECT id_client, SUM(cout) AS cout_client

FROM Commande

NATURAL JOIN (SELECT id_commande, SUM(prix_piece*quant.) AS cout

FROM DetailsCommande

GROUP BY id_commande)

GROUP BY id_client)

Page 42: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

42/59

d) Pour chaque client : nom, prenom, cout total de sescommandes

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

4. Union avec ceux n’ayant rien commande

SELECT nom_client, prenom_client, cout_client

FROM Client NATURAL JOIN

(SELECT id_client, SUM(cout) AS cout_client

FROM Commande

NATURAL JOIN (SELECT id_commande, SUM(prix_piece*quant.) AS cout

FROM DetailsCommande

GROUP BY id_commande)

GROUP BY id_client)

UNION

(SELECT nom_client, prenom_client, 0 as cout_client

FROM Client

WHERE id_client NOT IN (SELECT DISTINCT id_client FROM Commande))

Page 43: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

43/59

e) Pour chaque client : nom, prenom, cout moyen de sescommandes

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

1. Cout total des commandes de chaque client (requete precedente)

SELECT nom_client, prenom_client, cout_client

FROM Client NATURAL JOIN

(SELECT id_client, SUM(cout) AS cout_client

FROM Commande

NATURAL JOIN (SELECT id_commande, SUM(prix_piece*quant.) AS cout

FROM DetailsCommande

GROUP BY id_commande)

GROUP BY id_client)

UNION

(SELECT nom_client, prenom_client, 0 as cout_client

FROM Client

WHERE id_client NOT IN (SELECT DISTINCT id_client FROM Commande))

Page 44: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

44/59

e) Pour chaque client : nom, prenom, cout moyen de sescommandes

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

2. Il suffit de changer le SUM par AVG (moyenne)

SELECT nom_client, prenom_client, cout_client

FROM Client NATURAL JOIN

(SELECT id_client, AVG(cout) AS cout_client

FROM Commande

NATURAL JOIN (SELECT id_commande, SUM(prix_piece*quant.) AS cout

FROM DetailsCommande

GROUP BY id_commande)

GROUP BY id_client)

UNION

(SELECT nom_client, prenom_client, 0 as cout_client

FROM Client

WHERE id_client NOT IN (SELECT DISTINCT id_client FROM Commande))

Page 45: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

45/59

Exercices

I Client(id client, prenom client, nom client, nationalite client)

I Commande(id commande,id client, date commande)

I Produit(id produit, nom produit)

I Employe(id employe, nom employe, prenom employe)

I DetailsCommande(id commande, id produit, id employe, quantite,prix piece)

I Paiement(id commande, montant)

f) Donner les produits qui n’ont pas encore ete commandesg) Donner les produits commandes par tout le mondeh) Donner les produits commandes par au moins 30 clientsi) Pour les clients qui n’ont pas encore paye toutes leurs commandes :nom, prenom, somme totale des ses commandes, somme qu’ils ont paye,et somme qui reste a etre payeej) Pour chaque produit : nom du produit, nombre total de piecescommandees

Page 46: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

46/59

f) Donner les produits qui n’ont pas encore ete commandes

Produit(id produit, nom produit)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

SELECT id_produit FROM DetailsCommande

Page 47: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

47/59

f) Donner les produits qui n’ont pas encore ete commandes

Produit(id produit, nom produit)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

SELECT id_produit,nom_produit

FROM Produit

WHERE id_produit NOT IN

(SELECT id_produit FROM DetailsCommande)

Page 48: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

48/59

g) Donner les produits commandes par tout le monde

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)Produit(id produit, nom produit)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

Solution 1 : Double negation1. Selection des clients qui n’ont pas commande un produit P

SELECT *

FROM Client

WHERE id_client NOT IN

SELECT id_client

FROM Commande

NATURAL JOIN

(SELECT id_commande

FROM DetailsCommande

WHERE id_produit=P.id_produit)

Page 49: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

49/59

g) Donner les produits commandes par tout le monde

Client(id client, prenom client, nom client, nationalite client)Produit(id produit, nom produit)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

Solution 1 : Double negation2. Selection des produits P qui n’ont pas ete commande par aucun client

SELECT id_produit,nom_produit

FROM Produit P

WHERE NOT EXISTS

SELECT *

FROM Client

WHERE id_client NOT IN

SELECT id_client

FROM Commande

NATURAL JOIN

(SELECT id_commande

FROM DetailsCommande

WHERE id_produit=P.id_produit)

Page 50: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

50/59

g) Donner les produits commandes par tout le monde

Commande(id commande,id client, date commande)Produit(id produit, nom produit)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

Solution 2 : Regroupement par produit, contrainte sur le nombre de clients differentspar groupe.

SELECT nom_produit,id_produit

FROM (DetailsCommande NATURAL JOIN Commande) NATURAL JOIN Produit

GROUP BY id_produit

HAVING COUNT(DISTINCT id_client) IN (SELECT COUNT(*) FROM Client)

Page 51: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

51/59

h) Donner les produits commandes par au moins 30 clients

Commande(id commande,id client, date commande)Produit(id produit, nom produit)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

Semblable a la solution 2 de g)...

SELECT nom_produit

FROM (Commande NATURAL JOIN DetailsCommande) NATURAL JOIN Produit

GROUP BY id_produit

HAVING COUNT(DISTINCT id_client)>=30;

Page 52: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

52/59

i) Pour les clients qui n’ont pas encore paye toutes leurscommandes, donner la somme totale des ses commandes,la somme qu’ils ont paye, et la somme qui reste a etrepayee

Commande(id commande,id client, date commande)Client(id client, prenom client, nom client, nationalite client)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)Paiement(id commande, montant)

1. Regrouper le prix de chaque commande, et ce qui a deja ete paye par le client

SELECT id_commande, montant AS deja_paye, prix_commande

FROM Paiement

NATURAL JOIN (SELECT id_commande, SUM (prix_piece*quantite) AS prix_commande

FROM DetailsCommande

GROUP BY id_commande)

WHERE deja_paye < prix_commande

Page 53: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

53/59

i) Pour les clients qui n’ont pas encore paye toutes leurscommandes : nom, prenom, somme totale des sescommandes, somme qu’ils ont paye, et somme qui reste aetre payee

2. Jointures pour les clients et prenoms, regroupement par id client

SELECT nom, prenom, SUM(deja_paye), SUM(prix_commande),

SUM(prix_commande)-SUM(deja_paye)

FROM (SELECT id_commande, montant AS deja_paye, prix_commande

FROM Paiement

NATURAL JOIN (SELECT id_commande,

SUM (prix_piece*quantite) AS prix_commande

FROM DetailsCommande

GROUP BY id_commande)

WHERE deja_paye < prix_commande)

NATURAL JOIN Commande NATURAL JOIN Client

GROUP BY id_client

Page 54: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

54/59

j) Pour chaque produit : nom du produit, nombre total depieces commandees

Produit(id produit, nom produit)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

(SELECT nom_produit, total

FROM Produit NATURAL JOIN (SELECT id_produit, SUM(quantite) as total

FROM DetailsCommande

GROUP BY id_produit))

UNION

(SELECT nom_produit, 0 as total

FROM Produit

WHERE id_produit NOT IN (SELECT DISTINCT id_produit FROM DetailsCommande))

Page 55: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

55/59

I Client(id client, prenom client, nom client, nationalite client)

I Commande(id commande,id client, date commande)

I Produit(id produit, nom produit)

I Employe(id employe, nom employe, prenom employe)

I DetailsCommande(id commande, id produit, id employe,quantite, prix piece)

I Paiement(id commande, montant)

k) Donner le produit le + commande

I Suggestion : ORDER BY x DESC LIMIT y

l) Pour chaque produit, donner la valeur totale des piecescommandeesm) Donner le nombre de produits par employen) Donner le nombre d’employes travaillant pour chaque client

Page 56: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

56/59

k) Donner le produit le + commande

DetailsCommande(id commande, id produit, id employe, quantite, prix piece)En triant...

SELECT nom_produit,SUM(quantite)

FROM DetailsCommande

GROUP BY id_produit

ORDER BY SUM(quantite) DESC

LIMIT 1

Avec MAX :

SELECT nom_produit,SUM(quantite)

FROM DetailsCommande

GROUP BY id_produit

HAVING SUM(quantite)=MAX(SUM(quantite))

Page 57: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

57/59

l) Pour chaque produit, donner la valeur totale des piecescommandees

Produit(id produit, nom produit)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

(SELECT nom_produit,

FROM Produit

NATURAL JOIN (SELECT id_produit, SUM(quant.*prix_piece) AS valeur_totale

FROM DetailsCommande

GROUP BY id_produit))

UNION

(SELECT nom_produit, 0 AS valeur_totale

FROM Produit

WHERE id_produit NOT IN

(SELECT id_produit FROM DetailsCommande))

Page 58: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

58/59

m) Donner le nombre de produits par employe

Employe(id employe, nom employe, prenom employe)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

(SELECT nom_employe,SUM(quantite) as n_par_employe

FROM Employe NATURAL JOIN DetailsCommande

GROUP BY id_employe)

UNION

(SELECT nom_employe, 0 as n_par_employe

FROM Employe

WHERE id_employe NOT IN

(SELECT id_employe FROM DetailsCommande))

Page 59: INFO0009 - Bases de données · 3 Geographie 17 4 Francais 14 3 Math 11 SELECT Nom,Pr enom FROM ETUDIANT e WHERE NOT EXISTS SELECT * FROM INTERRO WHERE (Points

59/59

n) Donner le nombre d’employes travaillant pour chaqueclient

Client(id client, prenom client, nom client, nationalite client)Commande(id commande,id client, date commande)DetailsCommande(id commande, id produit, id employe, quantite, prix piece)

(SELECT prenom_client,nom_client,COUNT(DISTINCT id_employe) AS ntravailleurs

FROM DetailsCommande NATURAL JOIN Commande NATURAL JOIN Client

GROUP BY id_client)

UNION

(SELECT prenom_client, nom_client, 0 AS ntravailleurs

FROM Client

WHERE id_client NOT IN (SELECT id_client FROM DetailsCommande))