Sql query

This two query gives the same result .So what’s the difference between two?

/*Next, we need to pinpoint the users who have accumulated a lot of points across their stories.

Find the individual users who have gotten combined scores of more than 200, and their combined scores.*/

SELECT user , SUM(score) AS ‘Total Score’ FROM hacker_news

GROUP BY user HAVING SUM(score) >200 ORDER BY 2 DESC;


SELECT user, score FROM hacker_news WHERE score> 200 ORDER BY 2 DESC;

Hi @iamnai08, welcome back to the forums.

But the question asks you to aggregate all of their scores and then use GROUP BY AND HAVING.

The 2nd query just results in users who have (single) scores of > 200.

WHERE filters rows, HAVING filters groups based on an aggregate function.

2 Likes

Thanks @lisalisaj for clearing my doubt.

1 Like