Verwerkingsvolgorde van queries

Introductie tot Oracle SQL

Hadrien Lacroix

Content Developer

Waarom is de verwerkingsvolgorde belangrijk?

  • Optimaliseer je queries
    • Geen ongewenste resultaten
    • Snellere uitvoering
Introductie tot Oracle SQL

Voorbeeld

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

Voorbeeld

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

Voorbeeld

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

Voorbeeld

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

Voorbeeld

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

Voorbeeld

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

Voorbeeld

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

Wat kan er misgaan?

SELECT BillingCountry, 
       AVG(Total) > 100) AS Average
FROM Invoice
WHERE BillingCity <> 'Paris' 
GROUP BY BillingCountry
HAVING AVG(Total) > 100
ORDER BY Average DESC
  • Aliassen kun je NIET gebruiken in WHERE, GROUP BY en HAVING
  • Aliassen kun je WEL gebruiken in ORDER BY
Introductie tot Oracle SQL

Wat kan er misgaan?

SELECT BillingCountry, 
       AVG(Total) > 100) AS Average
FROM Invoice
WHERE BillingCity <> 'Paris' 
GROUP BY BillingCountry
HAVING AVG(Total) > 100
ORDER BY Average DESC
  • Geaggregeerde waarden kun je NIET filteren in WHERE
  • Geaggregeerde waarden kun je WEL filteren in HAVING
Introductie tot Oracle SQL

Wat kan er misgaan?

SELECT BillingCountry, 
       AVG(Total) > 100) AS Average
FROM Invoice
WHERE BillingCity <> 'Paris' 
GROUP BY BillingCountry
HAVING AVG(Total) > 100)
ORDER BY Average DESC
  • Individuele rijen kun je NIET filteren in HAVING
  • Individuele rijen kun je WEL filteren in WHERE
Introductie tot Oracle SQL

Uitvoervolgorde van een query

  1. FROM en JOINs: bepaal welke data je opvraagt

  2. WHERE: filter individuele rijen

  3. GROUP BY: groepeer rijen

  4. HAVING: filter groepen

  5. SELECT: kies kolommen en pas functies toe

  6. DISTINCT: verwijder dubbelen

  7. UNION, UNION ALL, INTERSECT, MINUS: set-operatoren

  8. ORDER BY: sorteer rijen

Introductie tot Oracle SQL

Laten we oefenen!

Introductie tot Oracle SQL

Preparing Video For Download...