SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 sidsnameratingage 22dustin745 29brutus133 31lubber855 32andy825 58rusty horatio735 71zorba horatio935 85art325 95bob363 96frodo325
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 Step 1: Evaluate FROM subqueries Nothing to see here!
Step 2: evaluate WHERE sidsnameratingage 22dustin745 29brutus133 31lubber855 32andy825 58rusty horatio735 71zorba horatio935 85art325 95bob363 96frodo325 FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating SELECT S.rating, MIN (S.age) AS minage HAVING COUNT (*) > 1
Step 3: GROUP BY S.rating sidsnameratingage 22dustin745 29brutus133 31lubber855 32andy825 58rusty horatio735 74horatio935 85art325 95bob363 96frodo325 rat ing Rows dustin45 64 horatio35 29brutus33 31lubber55 32andy25 58rusty35 74horatio35 85art25 95bob63 96frodo25
Step 4: SELECT S.rating, MIN (S.age) AS minage ratin g Min ageRows dustin45 64horatio35 29brutus33 31lubber55 32andy25 58rusty35 74horatio35 85art25 95bob63 96frodo25 Keeping the original rows around…
Step 5: HAVING COUNT (*) > 1 … for the HAVING clause! ratin g Min ageRows dustin45 64horatio35 29brutus33 31lubber55 32andy25 58rusty35 74horatio35 85art25 95bob63 96frodo25
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.