InterviewStack.io LogoInterviewStack.io

Complex Joins and Set Operations Questions

Focuses on mastering joins and set operations for combining and transforming relational data across multiple tables. Candidates should understand all join types including inner, left, right, full outer, cross joins, self joins, and nested joins, and know when to use each for correctness and performance. This topic also covers set operations such as UNION, INTERSECT, and EXCEPT, differences between joins and set operations, handling duplicates and NULL values correctly, choosing between joins, subqueries, and common table expressions for clarity and efficiency, and reasoning about join order and its performance implications on large tables. Interview questions may include multi table join problems, complex business logic across four or more tables, and scenarios that reveal trade offs between approaches.

MediumTechnical
76 practiced
Given the following schema:
customers(customer_id INT PK, name VARCHAR)orders(order_id INT PK, customer_id INT, order_date DATE)order_items(item_id INT PK, order_id INT, sku VARCHAR, amount DECIMAL)products(sku VARCHAR PK, category VARCHAR)
Write an ANSI SQL query to compute total revenue per customer for March 2024 including customers with zero orders. Explain pitfalls that cause double-counting and how to avoid them.
HardTechnical
61 practiced
In a distributed engine (Spark/Presto/BigQuery) you need to join a 500M-row fact table with a 5M-row dimension. Explain strategies to reduce shuffle: broadcast the smaller table, repartition on join key, use bloom filters, and leverage partitioning. For each strategy describe cost/benefit and when it's appropriate.
HardTechnical
72 practiced
You have an expensive daily aggregation that joins orders and order_items by day. Propose a materialized view or pre-aggregation strategy to support KPIs with near-real-time updates. Describe refresh options (complete vs incremental), handling late-arriving data, and trade-offs in maintenance cost vs query speed in systems like Postgres and Snowflake.
EasyTechnical
83 practiced
Explain the difference between UNION and UNION ALL. Given two monthly_sales tables (jan_sales, feb_sales) with identical schema (customer_id, amount), write queries to combine them while (a) preserving duplicates and (b) removing duplicates. Discuss when UNION ALL is preferable in BI pipelines and the cost of deduplication.
MediumTechnical
62 practiced
Compare Common Table Expressions (CTEs) and subqueries for readability and performance. Given a complex query joining four tables to produce cohort metrics, rewrite it using a WITH CTE for clarity and discuss whether CTEs are materialized inline or not in Postgres, SQL Server and BigQuery and how that affects performance.

Unlock Full Question Bank

Get access to hundreds of Complex Joins and Set Operations interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.