Insertion
-- Table Client
INSERT INTO Client (id_client, nom_client, prenom_client, adresse_client, ville_client)
VALUES
(1, 'Biden', 'Joe', 'Maarif', 'Casablanca'),
(2, 'Trump', 'Donald', 'Hay Hassani', 'Casablanca'),
(3, 'Obama', 'Barack', 'Agdal', 'Rabat'),
(4, 'Bush', 'George W.', 'Souissi', 'Rabat'),
(5, 'Clinton', 'Bill', 'Riad', 'Rabat');
-- Table Produit
INSERT INTO Produit (code_produit, libelle_produit, prix_unitaire)
VALUES
('A0001', 'Article1', 50),
('A0002', 'Article2', 60),
('A0003', 'Article3', 50),
('A0004', 'Article4', 10),
('A0005', 'Article5', 30);
-- Table Commande
INSERT INTO Commande (num_cmd, date_cmd, id_client)
VALUES
(1, '2021-02-12', 1),
(2, '2021-03-01', 3),
(3, '2021-03-04', 1),
(4, '2021-03-05', 2),
(5, '2021-03-05', 2);
-- Table Ligne_commande
INSERT INTO Ligne_commande (num_commande, code_produit, quantite_commandee)
VALUES
(1, 'A0002', 10),
(1, 'A0003', 10),
(1, 'A0004', 20),
(2, 'A0001', 30),
(2, 'A0003', 40);
-- Table Magasin
INSERT INTO Magasin (id_mag, nom_mag, ville_mag)
VALUES
(1, 'Magasin001', 'Casablanca'),
(2, 'Magasin002', 'Rabat'),
(3, 'Magasin003', 'Tanger'),
(4, 'Magasin004', 'Agadir'),
(5, 'Magasin005', 'Marrakech');
-- Table Stock
INSERT INTO Stock (id_magasin, code_produit, quantite_stock)
VALUES
(1, 'A0001', 70),
(1, 'A0002', 100),
(1, 'A0003', 0),
(1, 'A0004', 90),
(1, 'A0005', 0);
Voici les questions numérotées et les réponses correspondantes :
---
### **Insertion des données dans la base de données**
Cela correspond à la mise en place initiale pour préparer la base, aucun numéro attribué.
---
### **Requêtes SQL numérotées :**
**1. La liste de tous les produits :**
```sql
SELECT * FROM Produit;
```
**2. La liste de tous les clients habitant à Casablanca :**
```sql
SELECT * FROM Client WHERE ville_client = 'Casablanca';
```
**3. Le nom et le prix de tous les produits :**
```sql
SELECT libelle_produit, prix_unitaire FROM Produit;
```
**4. La liste des produits dont le prix est supérieur ou égal à 350 :**
```sql
SELECT * FROM Produit WHERE prix_unitaire >= 350;
```
**5. La liste des produits dont le prix est entre 200 et 400 :**
```sql
SELECT * FROM Produit WHERE prix_unitaire BETWEEN 200 AND 400;
```
**6. Le nombre de produits :**
```sql
SELECT COUNT(*) AS nombre_produits FROM Produit;
```
**7. Le nombre de produits dont le prix est supérieur à 50 :**
```sql
SELECT COUNT(*) AS produits_prix_sup_50 FROM Produit WHERE prix_unitaire > 50;
**8. Le nombre de magasins par ville :**
SELECT ville_mag, COUNT(*) AS nombre_magasins FROM Magasin GROUP BY ville_mag;
**9. Le nombre de magasins à Casablanca :**
SELECT COUNT(*) AS magasins_casablanca FROM Magasin WHERE ville_mag = 'Casablanca';
**10. Le nombre de commandes par client :**
SELECT id_client, COUNT(*) AS nombre_commandes FROM Commande GROUP BY id_client;
**11. Le nombre de commandes par date :**
SELECT date_cmd, COUNT(*) AS nombre_commandes FROM Commande GROUP BY date_cmd;
**12. Le nombre de produits par commande :**
SELECT num_commande, COUNT(*) AS nombre_produits FROM Ligne_commande GROUP BY
num_commande;
**13. Les codes des produits commandés dans la commande 1 :**
SELECT code_produit FROM Ligne_commande WHERE num_commande = 1;
**14. La quantité en stock du produit dont le code est « A005 » :**
SELECT quantite_stock FROM Stock WHERE code_produit = 'A0005';
**15. La quantité en stock par produit et par magasin :**
SELECT id_magasin, code_produit, quantite_stock FROM Stock;
**16. Le nom et le prénom du premier client :**
SELECT prenom_client, nom_client FROM Client ORDER BY id_client LIMIT 1;
**17. Le code du client qui a passé la dernière commande :**
SELECT id_client FROM Commande ORDER BY date_cmd DESC LIMIT 1;
**18. Le nom du produit ayant le prix le plus élevé :**
SELECT libelle_produit FROM Produit WHERE prix_unitaire = (SELECT MAX(prix_unitaire) FROM
Produit);
JOIN
1. Les commandes passées par le client « Biden » :
SELECT * FROM Commande
WHERE id_client = (SELECT id_client FROM Client WHERE nom_client = 'Biden');
SINON
SELECT Commande.*
FROM Commande JOIN Client ON Commande.id_client = Client.id_client
WHERE Client.nom_client = 'Biden';
2. Les commandes passées par les clients « Trump » et « Obama » :
SELECT * FROM Commande
WHERE id_client IN (SELECT id_client FROM Client WHERE nom_client IN ('Trump', 'Obama'));
**3. Les commandes passées en Février 2021 :**
SELECT * FROM Commande
WHERE DATE_FORMAT(date_cmd, '%Y-%m') = '2021-02';
WLA
SELECT * FROM Commande
WHERE date_cmd >= '2021-02-01' AND date_cmd < '2021-03-01';
**4. Les produits commandés dans la commande 1
SELECT P.libelle_produit
FROM Ligne_commande LC
JOIN Produit P ON LC.code_produit = P.code_produit
WHERE LC.num_commande = 1;
5. Les produits commandés en Février 2021 :
SELECT DISTINCT P.libelle_produit
FROM Commande C
JOIN Ligne_commande LC ON C.num_cmd = LC.num_commande
JOIN Produit P ON LC.code_produit = P.code_produit
WHERE DATE_FORMAT(C.date_cmd, '%Y-%m') = '2021-02';
6. Les produits commandés par le client 1 :
SELECT DISTINCT P.libelle_produit
FROM Commande C
JOIN Ligne_commande LC ON C.num_cmd = LC.num_commande
JOIN Produit P ON LC.code_produit = P.code_produit
WHERE C.id_client = 1;
7. Le montant de la commande 1 :
SELECT SUM(LC.quantite_commandee * P.prix_unitaire) AS montant_commande
FROM Ligne_commande LC
JOIN Produit P ON LC.code_produit = P.code_produit
WHERE LC.num_commande = 1;
8. La liste des commandes avec leurs montants triés par ordre décroissant du montant :
SELECT Commande.num_cmd, SUM(LCommande.quantite_commandee * P.prix_unitaire) AS
montant_commande
FROM Commande C
JOIN Ligne_commande LC ON Commande.num_cmd = LCommande.num_commande
JOIN Produit P ON LCommande.code_produit = P.code_produit
GROUP BY Commande.num_cmd
ORDER BY montant_commande DESC;
9. Le montant de la dernière commande :
SELECT SUM(LC.quantite_commandee * P.prix_unitaire) AS montant_commande
FROM Commande C
JOIN Ligne_commande LC ON C.num_cmd = LC.num_commande
JOIN Produit P ON LC.code_produit = P.code_produit
WHERE C.date_cmd = (SELECT MAX(date_cmd) FROM Commande);
10. Le montant moyen des commandes passées :
SELECT AVG(montant_commande) AS montant_moyen
FROM (
SELECT SUM(LC.quantite_commandee * P.prix_unitaire) AS montant_commande
FROM Commande C
JOIN Ligne_commande LC ON C.num_cmd = LC.num_commande
JOIN Produit P ON LC.code_produit = P.code_produit
GROUP BY C.num_cmd
) AS sous_table;
11. La quantité en stock du produit « Article2 » :
SELECT SUM(S.quantite_stock) AS quantite_totale
FROM Stock S
JOIN Produit P ON S.code_produit = P.code_produit
WHERE P.libelle_produit = 'Article2';
12. La quantité en stock du produit « Article2 » par magasin :
SELECT M.nom_mag, S.quantite_stock
FROM Stock S
JOIN Produit P ON S.code_produit = P.code_produit
JOIN Magasin M ON S.id_magasin = M.id_mag
WHERE P.libelle_produit = 'Article2';
13. La quantité en stock du produit « Article4 » à Casablanca :
SELECT S.quantite_stock
FROM Stock S
JOIN Produit P ON S.code_produit = P.code_produit
JOIN Magasin M ON S.id_magasin = M.id_mag
WHERE P.libelle_produit = 'Article4' AND M.ville_mag = 'Casablanca';
14. Les clients qui ont passé les dix premières commandes :
SELECT DISTINCT C.id_client, CL.nom_client, CL.prenom_client
FROM Commande C
JOIN Client CL ON C.id_client = CL.id_client
ORDER BY C.date_cmd ASC
LIMIT 10;
15. Les produits manquants dans le « Magasin001 » :
SELECT P.libelle_produit
FROM Stock S
JOIN Produit P ON S.code_produit = P.code_produit
WHERE S.id_magasin = 1 AND S.quantite_stock = 0;
16. Les produits non disponibles dans les magasins de Rabat :
SELECT P.libelle_produit
FROM Stock S
JOIN Produit P ON S.code_produit = P.code_produit
JOIN Magasin M ON S.id_magasin = M.id_mag
WHERE M.ville_mag = 'Rabat' AND S.quantite_stock = 0;
17. Le client qui a passé le plus de commandes (il peut y avoir plusieurs clients) :
SELECT C.id_client, CL.nom_client, CL.prenom_client, COUNT(*) AS nombre_commandes
FROM Commande C
JOIN Client CL ON C.id_client = CL.id_client
GROUP BY C.id_client
ORDER BY nombre_commandes DESC
LIMIT 1;
18. Les clients qui n’ont pas passé de commandes :
SELECT *
FROM Client CL
WHERE CL.id_client NOT IN (SELECT DISTINCT id_client FROM Commande);
19. Les montants des commandes passées en Mars 2021 groupés par commande :**
SELECT CP.num_cmd, SUM(CP.quantite * P.prix_unitaire) AS montant_total
FROM Commande_Produit CP
JOIN Produit P ON CP.id_produit = P.id_produit
JOIN Commande C ON CP.num_cmd = C.num_cmd
WHERE C.date_cmd >= '2021-03-01' AND C.date_cmd < '2021-04-01'
GROUP BY CP.num_cmd;
Les montants des commandes passées par le client « Obama » groupés par commande
SELECT CP.num_cmd, SUM(CP.quantite * P.prix_unitaire) AS montant_total
FROM Commande_Produit CP
JOIN Produit P ON CP.id_produit = P.id_produit
JOIN Commande C ON CP.num_cmd = C.num_cmd
JOIN Client CL ON C.id_client = CL.id_client
WHERE CL.nom_client = 'Obama'
GROUP BY CP.num_cmd;