SQL LEFT JOIN: How to Count Rows and Keep Zero Counts
Your support dashboard needs one row per department, even when a department has no open tickets. Start with the departments table, use a LEFT JOIN , put the ticket-status filter in ON , and count a non-nullable ticket ID rather than using COUNT(*) . 1. Create a small practice dataset Run this in a fresh SQLite database. Support has two open tickets, Billing has only a closed ticket, and Shipping has no tickets at all. All records are fictional. CREATE TABLE departments_demo (department_id INTEGER PRIMARY KEY, name TEXT NOT NULL); CREATE TABLE tickets_demo (ticket_id INTEGER PRIMARY KEY, department_id INTEGER NOT NULL, status TEXT NOT NULL); INSERT INTO departments_demo VALUES (1, 'Support'), (2, 'Billing'), (3, 'Shipping'); INSERT INTO tickets_demo VALUES (101, 1, 'open'), (102, 1, 'open'), (103, 1, 'closed'), (104, 2, 'closed'); 2. Count open tickets without dropping departments SELECT d.name, COUNT(t.ticket_id) AS open_tick...