- Why can't use SUM() in WHERE?
- Why can't use AS in WHERE or GROUP BY?
๐ You will get the reason why it is like that
๐ Select order
When you see this SQL, what do you think about order?
SELECT * FROM clients
WHERE age >= 30
GROUP BY usa
HAVING salary > 1000
ORDER BY id desc
LIMIT 100
Seems like sql executes from SELECT ?
actually it's not true
-- SQL executes in this order
FROM
|
WHERE
|
GROUP BY
|
HAVING
|
SELECT
|
ORDER BY
|
LIMIT
๐ Question 1
Why SUM() function can't use in WHERE?
FROM
|
WHERE ๐ ๐
|
GROUP BY ๐ ๐
|
HAVING
|
SELECT
|
ORDER BY
|
LIMIT
Before WHERE executes, GROUP BY has not executed yet, so we can't use COUNT() SUM() in WHERE.
In other word, it's possible to use these functions in HAVING.
๐ Question 2
Why can't use AS in WHERE or GROUP BY? Even though in ORDER BY can use it, unfair!!
FROM
|
WHERE ๐ ๐
|
GROUP BY ๐ ๐
|
HAVING
|
SELECT ๐ ๐
|
ORDER BY ๐ ๐
|
LIMIT
take it easy, man. Because ORDER BY executes after SELECT.
You can't read name which is created by "AS" before define it.
Thank you for reading ๐ค
Top comments (0)