SQLTeam.com | Weblogs | Forums

Simple SQL question-newbie

sql2008r2

#1

What is the difference in these two following SQL statements? I am told they are different, but I get the same results with both. Any help is appreciated. I am just new to learning SQL, it's exciting, but having hiccups.

SELECT
EVENT_C,
CASE WHEN EVENT_C = 100 THEN MAX (EVENT_DETAIL) ELSE NULL END

FROM
LOGEVENTS
GROUP BY EVENT_C

SELECT
EVENT_C,
MAX (CASE WHEN EVENT_C = 100 THEN EVENT_DETAIL ELSE NULL END)

FROM
LOGEVENTS
GROUP BY EVENT_C


#2

First query: For Events_c = 100 direct you will get maximum event_detail.
Second query: First it will execute the case statement and get the records for Event_c and among those records it will get maximum.
Conclusion:Both will return the same result just the way of execution is different.