Percentage of Orders Shipped to the User's Primary Address vs Other Addresses

Quick Overview

Write a SQL query that reports what share of orders shipped to each buyer's primary address and what share went elsewhere, as two percentages that add up to exactly 100.00. It tests joins, conditional counting, numeric division and consistent rounding.

Percentage of Orders Shipped to the User's Primary Address vs Other Addresses

Company: Adobe

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Each row of `transactions` is one order. Compute the percentage of orders shipped to the user's primary address and the percentage shipped to other addresses. An order counts as primary when `transactions.shipping_address` exactly matches that user's `users.primary_address`. Return a single row with `primary_address_pct` and `other_address_pct`, both with 2 decimals and summing to 100.00. ```hint Watch the division In PostgreSQL, dividing one integer count by another truncates the result. Check the type of every value in your percentage expression. ``` ### Input Table(s) Table `users` | Column | Type | Meaning | |---|---|---| | user_id | INT | Id of the user. Primary key. | | name | VARCHAR(100) | The user's name. May be NULL. | | primary_address | VARCHAR(255) | The user's primary address. NOT NULL. | Table `transactions` | Column | Type | Meaning | |---|---|---| | transaction_id | INT | Id of the order. Primary key. | | user_id | INT | User who placed the order. Foreign key to `users.user_id`. | | shipping_address | VARCHAR(255) | Address the order shipped to. NOT NULL. | | order_date | DATE | Date of the order. NOT NULL. | | amount | DECIMAL(10,2) | Order amount. May be NULL. | ### Output Contract - Exactly one row with two columns, in this order: `primary_address_pct`, `other_address_pct`. - `primary_address_pct` is 100 times the number of orders whose `shipping_address` equals the user's `primary_address`, divided by the total number of orders, rounded to 2 decimals with PostgreSQL `ROUND` on a numeric value (halves round away from zero). - `other_address_pct` is 100.00 minus the rounded `primary_address_pct`. Deriving it from the rounded value guarantees the two columns sum to exactly 100.00. - Both columns are numeric with two decimal places. ### Example Input `users`: | user_id | name | primary_address | |---|---|---| | 1 | Ana | 12 Oak St | | 2 | Ben | 9 Elm Ave | | 3 | Cy | 4 Pine Rd | Input `transactions`: | transaction_id | user_id | shipping_address | order_date | amount | |---|---|---|---|---| | 101 | 1 | 12 Oak St | 2024-01-05 | 40.00 | | 102 | 1 | 500 Market St | 2024-01-09 | 25.00 | | 103 | 2 | 9 Elm Ave | 2024-02-01 | 60.00 | | 104 | 2 | 9 Elm Ave | 2024-02-14 | NULL | | 105 | 3 | 4 pine rd | 2024-03-02 | 15.00 | | 106 | 3 | 4 Pine Rd | 2024-03-10 | 30.00 | Expected output: | primary_address_pct | other_address_pct | |---|---| | 66.67 | 33.33 | Orders 101, 103, 104 and 106 match the user's primary address exactly, which is 4 of 6 orders, or 66.666... percent, rounded to 66.67. Order 105 differs only in letter case, so it counts as another address along with order 102. The other share is 100.00 minus 66.67, which is 33.33. ### Constraints and Clarifications - `users` has at most 10,000 rows and `transactions` at most 100,000 rows. - Every `user_id` in `transactions` exists in `users`. - You may assume `transactions` has at least one row. - Every transaction row is one order, whatever its `amount`. Rows with a NULL `amount` still count. - Matching is exact string equality. Case, spacing and punctuation must all match, so `'4 pine rd'` is not `'4 Pine Rd'`. - Rounding each share on its own can break the sum. With 160 orders of which 1 is primary, independent rounding gives 0.63 and 99.38 (total 100.01); the contract above gives 0.63 and 99.37.

Overview: Write a SQL query that reports what share of orders shipped to each buyer's primary address and what share went elsewhere, as two percentages that add up to exactly 100.00. It tests joins, conditional counting, numeric division and consistent rounding.

Adobe logo
Adobe
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of transactions is one order. Compute the percentage of orders shipped to the user's primary address and the percentage shipped to other addresses. An order counts as primary when transactions.shipping_address exactly matches that user's users.primary_address. Return a single row with primary_address_pct and other_address_pct, both with 2 decimals and summing to 100.00.

Input Table(s)

Table users

ColumnTypeMeaning
user_idINTId of the user. Primary key.
nameVARCHAR(100)The user's name. May be NULL.
primary_addressVARCHAR(255)The user's primary address. NOT NULL.

Table transactions

ColumnTypeMeaning
transaction_idINTId of the order. Primary key.
user_idINTUser who placed the order. Foreign key to users.user_id.
shipping_addressVARCHAR(255)Address the order shipped to. NOT NULL.
order_dateDATEDate of the order. NOT NULL.
amountDECIMAL(10,2)Order amount. May be NULL.

Output Contract

  • Exactly one row with two columns, in this order: primary_address_pct , other_address_pct .
  • primary_address_pct is 100 times the number of orders whose shipping_address equals the user's primary_address , divided by the total number of orders, rounded to 2 decimals with PostgreSQL ROUND on a numeric value (halves round away from zero).
  • other_address_pct is 100.00 minus the rounded primary_address_pct . Deriving it from the rounded value guarantees the two columns sum to exactly 100.00.
  • Both columns are numeric with two decimal places.

Example

Input users:

user_idnameprimary_address
1Ana12 Oak St
2Ben9 Elm Ave
3Cy4 Pine Rd

Input transactions:

transaction_iduser_idshipping_addressorder_dateamount
101112 Oak St2024-01-0540.00
1021500 Market St2024-01-0925.00
10329 Elm Ave2024-02-0160.00
10429 Elm Ave2024-02-14NULL
10534 pine rd2024-03-0215.00
10634 Pine Rd2024-03-1030.00

Expected output:

primary_address_pctother_address_pct
66.6733.33

Orders 101, 103, 104 and 106 match the user's primary address exactly, which is 4 of 6 orders, or 66.666... percent, rounded to 66.67. Order 105 differs only in letter case, so it counts as another address along with order 102. The other share is 100.00 minus 66.67, which is 33.33.

Constraints and Clarifications

  • users has at most 10,000 rows and transactions at most 100,000 rows.
  • Every user_id in transactions exists in users .
  • You may assume transactions has at least one row.
  • Every transaction row is one order, whatever its amount . Rows with a NULL amount still count.
  • Matching is exact string equality. Case, spacing and punctuation must all match, so '4 pine rd' is not '4 Pine Rd' .
  • Rounding each share on its own can break the sum. With 160 orders of which 1 is primary, independent rounding gives 0.63 and 99.38 (total 100.01); the contract above gives 0.63 and 99.37.