70 likes | 93 Views
SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1. Step 1: Evaluate FROM subqueries. Helps to think of it like: FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating SELECT S.rating, MIN (S.age) AS minage HAVING COUNT (*) > 1.
E N D
SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1
Step 1: Evaluate FROM subqueries Helps to think of it like: FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating SELECT S.rating, MIN (S.age) AS minage HAVING COUNT (*) > 1 Nothing to see here!
Step 2: evaluate WHERE FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating SELECT S.rating, MIN (S.age) AS minage HAVING COUNT (*) > 1
Step 4: SELECT S.rating, MIN (S.age) AS minage Keeping the original rows around…
Step 5: HAVING COUNT (*) > 1 … for the HAVING clause!
Result Takeaway: since groups are not formed yet, trying to do an aggregate expression in the WHERE clause doesn’t make sense in this model of SQL evaluation.