Practice Free
MediumJoins

Feature Matrix Generation with CROSS JOIN

Frequently Asked By:MicrosoftGoogleStripe
Interactive In-Browser Workspace

Practice this problem on live schema tables in PostgreSQL 16 WASM with instant row verification and visual EXPLAIN plans.

Solve in Workbench

Problem Statement

1. Generate all pairs of plan tiers from `plans` and features from `features` using a `CROSS JOIN`. 2. Connect to `plan_entitlements` using a `LEFT JOIN` on both `plan_id` and `feature_id`. 3. Compute `is_enabled`: `COALESCE(pe.enabled, false)`. Return: 1. `p.plan_name` 2. `f.feature_key` 3. `COALESCE(pe.enabled, false)` AS `is_enabled` Order results by `p.plan_name` ascending, then by `f.feature_key` ascending.

Why Technical Interviewers Ask This

Product operations needs a full Cartesian entitlement grid mapping every subscription plan tier to every available feature toggle, marking whether the feature is currently enabled.

Common Pitfalls & Interview Traps

Forgetting required sorting and deterministic tie-breaking.
Not accounting for NULL or zero-value denominators during calculations.
Using inefficient quadratic subqueries instead of single-pass joins constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT 
        p.plan_name,
        f.feature_key,
        COALESCE(pe.enabled, false) AS is_enabled
      FROM plans p
      CROSS JOIN features f
      LEFT JOIN plan_entitlements pe ON p.plan_id = pe.plan_id AND f.feature_id = pe.feature_id
      ORDER BY p.plan_name ASC, f.feature_key ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Joins. It meets the target criteria: Complete plan-to-feature matrix with enabled status flag.

Edge Cases to Verbally Clarify with Your Interviewer

The dataset contains zero matching records (must return valid empty result without crashing).
Multiple records tie on sorting criteria (must evaluate tie-breaking order deterministically).
Foreign keys or secondary attributes have NULL values.

Frequently Asked Follow-Up Questions

What makes this question a favorite among Microsoft and Google interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable Joins queries that run efficiently on multi-million row production databases without excessive memory usage.

How does the query optimizer execute this query?

Using single-pass table scans with index lookups or sort buffers. Running EXPLAIN (ANALYZE, BUFFERS) in the DB Exercises workbench visualizes the execution nodes and buffer hit cache.

More FAANG SQL Interview Questions

View all questions