Phases of sql query
FROM & JOIN Phase The DBMS starts by looking at the FROM users u table. It then performs a LEFT JOIN with borrows b : For each row in users , it tries to find matching rows in borrows where u.id = b.user_id . If a user has no borrows, that user still appears, but with NULL in b.* . 👉 At this stage, we have a wide table with u.id, u.name, b.id, b.issued_date, b.status . WHERE Filtering If you had a WHERE clause, it would be applied here, filtering individual rows before grouping. Since we’re not using WHERE , all rows pass through. GROUP BY Phase Rows are grouped by u.id, u.name . Each group corresponds to one user . Example: If user #5 has 3 borrow rows, those rows are grouped into a single "bucket." Aggregate Functions (COUNT) For each group (each user), COUNT(b.id) is calculated. Important: COUNT(b.id) counts only non-NULL values . If a user has no borrows, b.id is NULL → count becomes 0 . ...