SQL Aggregations SQL Topic exerciseMediumVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Unique Business Event Count

Return one company-wide metric counting unique business events.

  • Aggregation
  • Distinct values

Exercise brief

Understand the request

Data reliability analyst A retry produced two warehouse rows for one business event, so ingestion row count overstates activity.

Return one metric for unique business events after duplicate ingestion rows are collapsed.

Return

  • Return unique_business_events in this exact left-to-right order.

Constraints

  • Use business_event_id as the declared entity identity, not event_row_id or ingested row count.
  • COUNT(DISTINCT business_event_id) must count each non-NULL business event once.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

events

  • event_row_idINTEGER
  • business_event_idVARCHAR(50)

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

The business entity is identified by business_event_id; event_row_id identifies an ingestion row.

Hint 2

Place DISTINCT inside COUNT so the duplicated business identity contributes once and NULL would remain excluded.

Hint 3

SELECT COUNT(DISTINCT /* business entity key */) AS unique_business_events FROM events;

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT COUNT(DISTINCT business_event_id) AS unique_business_events FROM events;

Why this works

Correctness: COUNT(DISTINCT business_event_id) measures the declared business-entity grain rather than warehouse row grain. Edge case: duplicate ingestion rows collapse, and a NULL business identity would be ignored rather than counted as an entity. Portability: single-expression COUNT(DISTINCT expression) is portable; multiple-expression DISTINCT support varies.

Success check

Exactly one row reports 15 unique business events from 16 ingested rows.

Expected result

Use this output to verify values, aliases, ordering, and row count.

unique_business_events
15

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.