Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 58rusty1035 64horatio735 71zorba1016 74horatio935 85art325 95bob363 96frodo325

2 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!

3 Step 2: evaluate WHERE sidsnameratingage 22dustin745 29brutus133 31lubber855 32andy825 58rusty1035 64horatio735 71zorba1016 74horatio935 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

4 Step 3: GROUP BY S.rating sidsnameratingage 22dustin745 29brutus133 31lubber855 32andy825 58rusty1035 64horatio735 74horatio935 85art325 95bob363 96frodo325 rat ing Rows 7 1 8 10 9 3 22dustin45 64 horatio35 29brutus33 31lubber55 32andy25 58rusty35 74horatio35 85art25 95bob63 96frodo25

5 Step 4: SELECT S.rating, MIN (S.age) AS minage ratin g Min ageRows 735 133 825 1035 9 325 22dustin45 64horatio35 29brutus33 31lubber55 32andy25 58rusty35 74horatio35 85art25 95bob63 96frodo25 Keeping the original rows around…

6 Step 5: HAVING COUNT (*) > 1 … for the HAVING clause! ratin g Min ageRows 735 133 825 1035 9 325 22dustin45 64horatio35 29brutus33 31lubber55 32andy25 58rusty35 74horatio35 85art25 95bob63 96frodo25

7 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.


Download ppt "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."

Similar presentations


Ads by Google