Practice Free
EasyFundamentals

Clean Marketing Lead Ingestion

Frequently Asked By:AppleGoogleAmazon
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

Query the `leads` table to extract unique combinations of `acquisition_channel` and `country_code`. Requirements: - Exclude records where `acquisition_channel` is `NULL` or marked as `'unknown'`. - Exclude records where `country_code` is `NULL`. - Order the output by `acquisition_channel` ascending, then by `country_code` ascending.

Why Technical Interviewers Ask This

The demand generation team is auditing organic and paid acquisition leads. Some lead records contain missing utm_source values or duplicate records from retargeting pixels. They need a pristine list of unique, valid acquisition channels and countries.

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 fundamentals constructs.

Canonical Modern SQL Solution

PostgreSQL 16 / ANSI SQL
SELECT DISTINCT
        acquisition_channel,
        country_code
      FROM leads
      WHERE acquisition_channel IS NOT NULL 
        AND acquisition_channel != 'unknown'
        AND country_code IS NOT NULL
      ORDER BY acquisition_channel ASC, country_code ASC;
Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Fundamentals. It meets the target criteria: Unique list of acquisition_channel and country_code with no null or unknown values, alphabetically sorted.

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 Apple and Google interviewers?

Interviewers use this problem to test whether candidates write defensive, scalable Fundamentals 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