cswork / libraryOpen practice workspace

Coding questions · Akuna Capital

Answer SQL MCQs with reasoning

This question evaluates proficiency in SQL query semantics and reasoning about relational operations—specifically INNER, LEFT, and RIGHT JOIN behavior; GROUP BY and HAVING usage; NULL handling in comparisons and aggregates; window functions such as ROW_NUMBER and RANK; and the logical query processing order—within the Data Manipulation (SQL/Python) domain. It is commonly asked to assess the ability to interpret and justify query outcomes versus alternative answers, testing both conceptual understanding of relational theory and practical application of SQL query behavior.

Answer multiple-choice questions covering SQL topics: INNER vs LEFT vs RIGHT JOIN semantics; GROUP BY and HAVING; NULL handling in comparisons and aggregates; window functions such as ROW_NUMBER and RANK; and logical query processing order. For each selected answer, briefly justify why it is correct and why at least one alternative is not.

Customer Spend, Joins, Aggregates, NULLs, and Window Ranking

Using the tables defined below, write a single SQL query that returns, for each customer located in 'New York' or 'San Francisco' who either (a) has at least 100 in total spend from completed orders, or (b) has never placed any order, the following columns:

  • customer_id
  • customer_name
  • city
  • total_completed_amount: the total revenue from that customer's orders with status = 'COMPLETED'
  • completed_order_count: the number of distinct orders with status = 'COMPLETED'
  • avg_completed_order_value: total_completed_amount divided by completed_order_count (NULL if the customer has no completed orders)
  • spending_rank: the rank of the customer by total_completed_amount in descending order (highest spender has rank 1; ties share the same rank)

Requirements:

  • Start from the customers table and join to orders and order_items so that customers with no orders are still included in the result.
  • Correctly handle NULLs when computing aggregates for customers with no orders or no completed orders.
  • Use GROUP BY and HAVING to filter based on the aggregated total_completed_amount and on whether the customer has any orders.
  • Use a window function (such as RANK) to compute the spending_rank over the final set of customers.

Return the result ordered by total_completed_amount in descending order, and then by customer_id ascending.

Table customers

columntypeconstraints
customer_idINTPRIMARY KEY
customer_nameVARCHAR(50)NOT NULL
cityVARCHAR(50)NOT NULL

Sample data:

citycustomer_idcustomer_name
New York1Alice
San Francisco2Bob
New York3Carol
San Francisco4Dave

Table orders

columntypeconstraints
order_idINTPRIMARY KEY
customer_idINTNOT NULL
order_dateDATENOT NULL
statusVARCHAR(20)NOT NULL

Sample data:

customer_idorder_dateorder_idstatus
12025-05-01101COMPLETED
12025-05-10102CANCELLED
22025-05-02103COMPLETED
22025-05-15104COMPLETED
32025-05-20105PENDING

Table order_items

columntypeconstraints
order_item_idINTPRIMARY KEY
order_idINTNOT NULL
product_nameVARCHAR(100)NOT NULL
quantityINTNOT NULL
unit_priceDECIMAL(10,2)NOT NULL

Sample data:

order_idorder_item_idproduct_namequantityunit_price
1011Widget A250
1012Widget B130
1023Widget A150
1034Widget C320
1045Widget C420
1056Widget B130

Expected output

One row per customer in New York or San Francisco who either has at least 100 total spend from completed orders or has never placed any order. Shows total and count of completed orders, the average completed order value, and the rank by total spend.

avg_completed_order_valuecitycompleted_order_countcustomer_idcustomer_namespending_ranktotal_completed_amount
70San Francisco22Bob1140
130New York11Alice2130
NULLSan Francisco04Dave30

Hints

  1. Start from customers and LEFT JOIN to orders and order_items so that customers without orders are not lost.
  2. Aggregate spend and counts in a GROUP BY, filter with HAVING on the aggregated values, then apply a window RANK() in an outer query.