You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Suggestion: Correct SQL Example to Avoid Alias in HAVING Clause
Issue:
The SQL snippet on the page currently uses a column alias (posts_written) in the HAVING clause:
SELECTusers.id, users.name, COUNT(posts.id) AS posts_written
FROM users
JOIN posts ONusers.id=posts.user_idGROUP BYusers.idHAVING posts_written >=10;
Using a column alias in HAVING is not supported in standard SQL due to the query execution order. HAVING is processed before the SELECT statement assigns any aliases, so the alias posts_written does not yet exist at that point in the query execution. This can lead to an error in many SQL databases.
Proposed Solution:
To ensure compatibility across SQL databases and adhere to SQL standards, it would be better to use the full expression (COUNT(posts.id)) in the HAVING clause:
SELECTusers.id, users.name, COUNT(posts.id) AS posts_written
FROM users
JOIN posts ONusers.id=posts.user_idGROUP BYusers.idHAVINGCOUNT(posts.id) >=10;
Explanation:
This adjustment will ensure that the query is compatible with SQL databases that strictly follow standard SQL, improving the reliability of the example for learners.
Checks
Describe your suggestion
Suggestion: Correct SQL Example to Avoid Alias in HAVING Clause
Issue:
The SQL snippet on the page currently uses a column alias (
posts_written
) in theHAVING
clause:Using a column alias in
HAVING
is not supported in standard SQL due to the query execution order.HAVING
is processed before theSELECT
statement assigns any aliases, so the aliasposts_written
does not yet exist at that point in the query execution. This can lead to an error in many SQL databases.Proposed Solution:
To ensure compatibility across SQL databases and adhere to SQL standards, it would be better to use the full expression (
COUNT(posts.id)
) in theHAVING
clause:Explanation:
This adjustment will ensure that the query is compatible with SQL databases that strictly follow standard SQL, improving the reliability of the example for learners.
Path
Node / JS
Lesson Url
https://www.theodinproject.com/lessons/node-path-databases-databases-and-sql
(Optional) Discord Name
de_kr1sh
(Optional) Additional Comments
Looking forward to contributing to this issue as it will be my first contribution to an open-source project.
The text was updated successfully, but these errors were encountered: