HardBusiness Analytics High Frequency
Querying Nested JSONB Metadata with #>> Path Extraction
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.
Problem Statement
Query `service_incidents` to extract fields using PostgreSQL JSONB path operators:
1. `incident_id`
2. `incident_data#>>'{service, name}'` AS `service_name`
3. `incident_data#>>'{error, code}'` AS `error_code`
4. `(incident_data#>>'{error, http_status}')::int` AS `http_status`
Filter for incidents where `incident_data#>>'{error, severity}' = 'FATAL'`.
Order results by `incident_id` ascending.
Why Technical Interviewers Ask This
Cloud infrastructure error logs store diagnostic traces in deeply nested JSONB structures. SREs need to extract the microservice name and root error code from the JSON path `{error, details, code}`.
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 business analytics constructs.
Canonical Modern SQL Solution
PostgreSQL 16 / ANSI SQLSELECT
incident_id,
incident_data#>>'{service, name}' AS service_name,
incident_data#>>'{error, code}' AS error_code,
(incident_data#>>'{error, http_status}')::int AS http_status
FROM service_incidents
WHERE incident_data#>>'{error, severity}' = 'FATAL'
ORDER BY incident_id ASC;Approach Breakdown
This query addresses the problem requirements by filtering and structuring data in Business Analytics. It meets the target criteria: Extracted service error metrics for all fatal incidents.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 Business Analytics 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.