0

I have a sql which is:

SELECT 
COUNT(CustomerID), Country
FROM 
Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

I dont know the sequence of how execution goes in order from Select to OrderBy.So,i gave number as 1,2,3,4 to get the sequence of execution. So,I think:

SELECT 
(4)COUNT(CustomerID), Country
FROM 
Customers
(1)GROUP BY Country
(2)HAVING COUNT(CustomerID) > 5
(3)ORDER BY COUNT(CustomerID) DESC;

Is,it right or wrong?If,I am wrong please correct it.

Ashwin Karki
  • 249
  • 4
  • 18
  • `select` -> `group by` (sometimes both these steps can happen simultaneously, if MySQL decides to do a loose index scan) -> `having` (filters out the grouped data based on condition) -> `order by` (sorts it) – Madhur Bhaiya Sep 13 '19 at 08:34
  • See the execution plan. (1) Stream aggregate: COUNT(CustomerID) grouped by Country -> maps to SELECTed (specified) columns and GROUP BY clause (2) Filter: COUNT(CustomerID) > 5 -> maps to HAVING (3) Sort: COUNT(CustomerID) -> maps to ORDER BY – OzrenTkalcecKrznaric Sep 13 '19 at 08:44

0 Answers0