Count the Number of Experiments - Problem

Table: Experiments

Column NameType
experiment_idint
platformenum
experiment_nameenum
  • experiment_id is the column with unique values for this table.
  • platform is an enum (category) type of values ('Android', 'IOS', 'Web').
  • experiment_name is an enum (category) type of values ('Reading', 'Sports', 'Programming').

This table contains information about the ID of an experiment done with a random person, the platform used to do the experiment, and the name of the experiment.

Write a solution to report the number of experiments done on each of the three platforms for each of the three given experiments. Notice that all the pairs of (platform, experiment) should be included in the output including the pairs with zero experiments.

Return the result table in any order.

Table Schema

Experiments
Column Name Type Description
experiment_id PK int Unique identifier for each experiment
platform enum Platform where experiment was conducted: Android, IOS, Web
experiment_name enum Type of experiment: Reading, Sports, Programming
Primary Key: experiment_id
Note: Contains experiment data with platform and experiment type information

Input & Output

Example 1 — Basic Experiment Count
Input Table:
experiment_id platform experiment_name
1 Android Reading
2 Android Reading
3 Android Reading
4 IOS Programming
5 IOS Programming
6 Web Programming
Output:
platform experiment_name num_experiments
Android Programming 0
Android Reading 3
Android Sports 0
IOS Programming 2
IOS Reading 0
IOS Sports 0
Web Programming 1
Web Reading 0
Web Sports 0
💡 Note:

The query creates all 9 possible combinations of platforms (Android, IOS, Web) and experiments (Programming, Reading, Sports). Then it counts actual occurrences: Android-Reading has 3 experiments, IOS-Programming has 2, Web-Programming has 1, and all other combinations have 0.

Example 2 — All Zero Counts
Input Table:
experiment_id platform experiment_name
Output:
platform experiment_name num_experiments
Android Programming 0
Android Reading 0
Android Sports 0
IOS Programming 0
IOS Reading 0
IOS Sports 0
Web Programming 0
Web Reading 0
Web Sports 0
💡 Note:

When the Experiments table is empty, the CROSS JOIN still generates all 9 possible platform-experiment combinations, but the LEFT JOIN finds no matches, resulting in all counts being 0.

Constraints

  • platform is one of 'Android', 'IOS', 'Web'
  • experiment_name is one of 'Reading', 'Sports', 'Programming'
  • experiment_id is unique for each row

Visualization

Tap to expand
Count the Number of Experiments INPUT Experiments Table platform experiment_name Android Reading Android Sports IOS Programming Web Reading ... more rows Platform Values: Android IOS Web Experiment Values: Reading Sports Programming 3 x 3 = 9 combinations needed in output ALGORITHM STEPS 1 Create Platform CTE Generate all 3 platform values UNION ALL for each value 2 Create Experiment CTE Generate all 3 experiment values UNION ALL for each value 3 CROSS JOIN CTEs Creates all 9 combinations Platforms x Experiments 4 LEFT JOIN + COUNT Join with Experiments table COUNT returns 0 for no match CROSS JOIN Result Android IOS Web Read Read Read Sports Sports Sports Prog Prog Prog = 9 pairs FINAL RESULT Output: All 9 Combinations with Counts platform experiment count Android Reading 2 Android Sports 1 Android Programming 0 IOS Reading 1 IOS Sports 0 IOS Programming 1 Web Reading 1 Web Sports 2 Web Programming 0 OK - All 9 pairs included! Zero counts shown for missing data Zero Count Pairs: Android-Prog, IOS-Sports, Web-Prog Key Insight: CROSS JOIN generates all possible combinations between two sets. Combined with LEFT JOIN and COUNT, this ensures every platform-experiment pair appears in results, even when no matching data exists. CTEs (Common Table Expressions) help organize the query by defining platform and experiment value sets separately. TutorialsPoint - Count the Number of Experiments | Optimal Solution
Asked in
Amazon 15 Microsoft 12
23.0K Views
Medium Frequency
~12 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen