Quick Overview

This question evaluates a candidate's ability to debug and validate Hive SQL queries, assessing competencies in identifying logical errors (incorrect join conditions, aggregation and window function misuse, filters and partition predicates), validating results with test cases, and reasoning about performance optimizations.

Debug a Hive query

Company: TikTok

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Technical Screen

You are given a prewritten Hive SQL query that produces incorrect results. Describe how you would debug it: identify logical errors (e.g., join conditions, aggregations, window functions, filters, partition predicates), fix the query, and validate correctness with test cases and data checks. Explain any performance optimizations you would make and why.

Overview: This question evaluates a candidate's ability to debug and validate Hive SQL queries, assessing competencies in identifying logical errors (incorrect join conditions, aggregation and window function misuse, filters and partition predicates), validating results with test cases, and reasoning about performance optimizations.

Read the full TikTok Software Engineer interview experience this question came from

You are computing monthly revenue metrics for an e-commerce platform. Using the `users`, `orders`, and `payments` tables described below, write a **single PostgreSQL query** that produces one row per (calendar month, country) combination, restricted to the first quarter of 2025. **Qualifying-order rules** (an order must satisfy ALL of these to be counted): - The order's `status` is `'completed'`. - The order's `order_date` falls between **2025-01-01** and **2025-03-31** (inclusive). - There exists at least one row in `payments` for that order whose `payment_date` is between the order's `order_date` and **7 days after** `order_date` (inclusive). An order is counted **once** no matter how many payments it has. **Output:** For each (month, country) that has at least one qualifying order, return exactly these columns: - `order_month` — the order's month in `YYYY-MM` format (derived from `order_date`). - `country` — the country of the order's user (from `users.country`). - `total_gmv` — the sum of `orders.total_amount` over the qualifying orders in that (month, country). - `paying_users` — the number of **distinct** users who placed at least one qualifying order in that (month, country). - `avg_order_value` — `total_gmv` divided by the **number of qualifying orders** in that (month, country), rounded to 2 decimal places. - `top_customer_id` — the `user_id` whose summed `total_amount` of qualifying orders is the **highest** within that (month, country). Break ties by choosing the **smallest** `user_id`. Sort the result by `order_month` ascending, then `country` ascending.

Tables

users(user_id INT, country VARCHAR(2), signup_date DATE)

orders(order_id INT, user_id INT, order_date DATE, status VARCHAR(20), total_amount DECIMAL(10,2))

payments(payment_id INT, order_id INT, payment_date DATE, amount DECIMAL(10,2), payment_method VARCHAR(20))

Hints

  1. Filter to completed Q1-2025 orders first, and use an EXISTS subquery against payments (rather than a direct join) so an order with several qualifying payments is still counted only once.
  2. In Postgres, add days to a DATE with `order_date + 7`, and format the month with `TO_CHAR(order_date, 'YYYY-MM')`.

Loading coding console...