I have found a similar question: What is the order of execution for this SQL statement. The accepted answer specifies that FROM is executed prior to any other clauses.
SELECT StudentName, Students.StudentID, Cnt
FROM (
SELECT Students.StudentID, count(StudentCourses.CourselD) as [Cnt]
FROM Students LEFT JOIN StudentCourses
ON Students.StudentID = StudentCourses.StudentID GROUP BY Students.StudentID
) T INNER JOIN Students on T.StudentID = Students.StudentID
How does count() work in this case?
I am confused because it does not seem to follow my logic. Assuming that LEFT JOIN in nested FROM is executed first (please let me know if this is wrong), We will have at least one row for each student as an intermediate output, possibly with a value of null if a student is not taking any courses. If count() is applied to that output, such students will have 1 counted due to null valued row, which does not happen in the actual result. Could anyone explain how this query gets executed under the hood?
Thanks in advance