Streamhub's engineering team is auditing session data quality.
Write a query to return the total number of sessions and the number of completed sessions as a single row.
Assumptions:
- The
sessionstable has one row per session with anended_atvalue. - Sessions still in progress have a missing
ended_at; completed sessions have a recordedended_at. - The total number of sessions covers every session record. The number of completed sessions covers only sessions with a recorded
ended_at.
Output:
- A single row with columns
total_sessionsandcompleted_sessions.
Schema · analytics 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
COUNT(*) AS total_sessions,
COUNT(ended_at) AS completed_sessions
FROM
sessions The shape
COUNT(*) counts every session record, and COUNT(ended_at) counts only the records whose ended_at is recorded. Running them side by side in the same SELECT returns the two figures the audit needs from a single pass over sessions.
Clause by clause
SELECT COUNT(*) AS total_sessions, COUNT(ended_at) AS completed_sessionsreturns the two figures as a single row.COUNT(*)counts rows and ignores nullability entirely, so it returns the full180session records.COUNT(ended_at)only counts rows whereended_atis non-missing, so the in-progress sessions (whoseended_atis missing) are skipped and the result is168completed.FROM sessionsreads every session record. There is noWHEREbecause the audit covers the full population; the two aggregates do the work of separating completed from total inside theSELECTlist itself.
Why this and not COUNT(ended_at) for both
COUNT(ended_at) would silently undercount the total, because the in-progress sessions have a missing ended_at and COUNT(column) skips missing values by design. The total figure has to be a count of rows, not of recorded values. COUNT(*) is the only form that ignores column nullability and returns the row count outright.
You practiced COUNT(*) vs COUNT(column) — the first counts every record, the second counts only records where the column is recorded.