Ordre de traitement des requêtes

Introduction à Oracle SQL

Hadrien Lacroix

Content Developer

Pourquoi l'ordre de traitement compte-t-il ?

  • Optimisez vos requêtes
    • Pas de résultats indésirables
    • Exécution plus rapide
Introduction à Oracle SQL

Exemple

    SELECT BillingCountry, AVG(Total) > 100) AS Average
    FROM Invoice
    WHERE BillingCity <> 'Paris' 
    GROUP BY BillingCountry
    HAVING AVG(Total) > 100
    ORDER BY Average DESC
Introduction à Oracle SQL

Exemple

    SELECT BillingCountry, AVG(Total) > 100) AS Average
->  FROM Invoice
    WHERE BillingCity <> 'Paris' 
    GROUP BY BillingCountry
    HAVING AVG(Total) > 100
    ORDER BY Average DESC
Introduction à Oracle SQL

Exemple

    SELECT BillingCountry, AVG(Total) > 100) AS Average
    FROM Invoice
->  WHERE BillingCity <> 'Paris' 
    GROUP BY BillingCountry
    HAVING AVG(Total) > 100
    ORDER BY Average DESC
Introduction à Oracle SQL

Exemple

    SELECT BillingCountry, AVG(Total) > 100) AS Average
    FROM Invoice
    WHERE BillingCity <> 'Paris' 
->  GROUP BY BillingCountry
    HAVING AVG(Total) > 100
    ORDER BY Average DESC
Introduction à Oracle SQL

Exemple

    SELECT BillingCountry, AVG(Total) > 100) AS Average
    FROM Invoice
    WHERE BillingCity <> 'Paris' 
    GROUP BY BillingCountry
->  HAVING AVG(Total) > 100
    ORDER BY Average DESC
Introduction à Oracle SQL

Exemple

->  SELECT BillingCountry, AVG(Total) > 100) AS Average
    FROM Invoice
    WHERE BillingCity <> 'Paris' 
    GROUP BY BillingCountry
    HAVING AVG(Total) > 100
    ORDER BY Average DESC
Introduction à Oracle SQL

Exemple

    SELECT BillingCountry, AVG(Total) > 100) AS Average
    FROM Invoice
    WHERE BillingCity <> 'Paris' 
    GROUP BY BillingCountry
    HAVING AVG(Total) > 100
->  ORDER BY Average DESC
Introduction à Oracle SQL

Que peut mal tourner ?

SELECT BillingCountry, 
       AVG(Total) > 100) AS Average
FROM Invoice
WHERE BillingCity <> 'Paris' 
GROUP BY BillingCountry
HAVING AVG(Total) > 100
ORDER BY Average DESC
  • Les alias ne peuvent pas être utilisés dans WHERE, GROUP BY et HAVING
  • Les alias peuvent être utilisés dans ORDER BY
Introduction à Oracle SQL

Que peut mal tourner ?

SELECT BillingCountry, 
       AVG(Total) > 100) AS Average
FROM Invoice
WHERE BillingCity <> 'Paris' 
GROUP BY BillingCountry
HAVING AVG(Total) > 100
ORDER BY Average DESC
  • Les valeurs agrégées ne peuvent pas être filtrées dans WHERE
  • Les valeurs agrégées peuvent être filtrées dans HAVING
Introduction à Oracle SQL

Que peut mal tourner ?

SELECT BillingCountry, 
       AVG(Total) > 100) AS Average
FROM Invoice
WHERE BillingCity <> 'Paris' 
GROUP BY BillingCountry
HAVING AVG(Total) > 100)
ORDER BY Average DESC
  • Les lignes individuelles ne peuvent pas être filtrées dans HAVING
  • Les lignes individuelles peuvent être filtrées dans WHERE
Introduction à Oracle SQL

Ordre d'exécution d'une requête

  1. FROM et JOIN : déterminer quelles données interroger

  2. WHERE : filtrer les lignes individuelles

  3. GROUP BY : regrouper les lignes

  4. HAVING : filtrer les groupes

  5. SELECT : choisir les colonnes et appliquer des fonctions

  6. DISTINCT : retirer les doublons

  7. UNION, UNION ALL, INTERSECT, MINUS : appliquer des opérateurs d'ensembles

  8. ORDER BY : trier les lignes

Introduction à Oracle SQL

Passons à la pratique !

Introduction à Oracle SQL

Preparing Video For Download...