Join orders with product details and compute each order's total amount

Quick Overview

Write a SQL query that joins an orders table to a products table and returns each order with its product details and a computed total amount rounded to two decimals. It tests inner joins, column qualification and exact decimal arithmetic.

Join orders with product details and compute each order's total amount

Company: MongoDB

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of `Orders` is one order for a single product, and `Products` holds the catalog. Join the two tables and return one row per order with `order_id`, `customer_name`, `quantity`, `order_date`, `product_id`, `product_name`, `category`, `price` and `total_amount`, where `total_amount` is `quantity * price` rounded to 2 decimals. Order the result by `order_id` ascending. ```hint Shared column Both tables have a `product_id` column. Decide which table each output column comes from and qualify every column with a table alias. ``` ### Input Table(s) Table `Products` | Column | Type | Meaning | |---|---|---| | product_id | INT | Product identifier | | product_name | VARCHAR(100) | Product name, never NULL | | category | VARCHAR(50) | Product category, may be NULL | | price | DECIMAL(10,2) | Unit price, never NULL | Primary key: `product_id`. Table `Orders` | Column | Type | Meaning | |---|---|---| | order_id | INT | Order identifier | | customer_name | VARCHAR(100) | Customer who placed the order, never NULL | | product_id | INT | Product ordered | | quantity | INT | Units ordered, never NULL | | order_date | DATE | Date of the order, never NULL | Primary key: `order_id`. Foreign key: `Orders.product_id` references `Products.product_id`. ### Output Contract - Columns, in this order: `order_id`, `customer_name`, `quantity`, `order_date`, `product_id`, `product_name`, `category`, `price`, `total_amount`. - One row per order. - `total_amount` is `quantity * price`, rounded to 2 decimals. - Rows sorted by `order_id` ascending. `order_id` is unique, so the row order is fully determined. ### Example Input `Products`: | product_id | product_name | category | price | |---|---|---|---| | 1 | Wireless Mouse | Electronics | 24.99 | | 2 | Notebook | Stationery | 3.50 | | 3 | Desk Lamp | NULL | 18.00 | | 4 | USB Cable | Electronics | 8.25 | Input `Orders`: | order_id | customer_name | product_id | quantity | order_date | |---|---|---|---|---| | 101 | Alice | 2 | 4 | 2024-03-01 | | 102 | Bob | 1 | 1 | 2024-03-02 | | 103 | Alice | 3 | 2 | 2024-03-02 | | 104 | Chen | 1 | 3 | 2024-03-05 | Expected output: | order_id | customer_name | quantity | order_date | product_id | product_name | category | price | total_amount | |---|---|---|---|---|---|---|---|---| | 101 | Alice | 4 | 2024-03-01 | 2 | Notebook | Stationery | 3.50 | 14.00 | | 102 | Bob | 1 | 2024-03-02 | 1 | Wireless Mouse | Electronics | 24.99 | 24.99 | | 103 | Alice | 2 | 2024-03-02 | 3 | Desk Lamp | NULL | 18.00 | 36.00 | | 104 | Chen | 3 | 2024-03-05 | 1 | Wireless Mouse | Electronics | 24.99 | 74.97 | Order 101 is 4 notebooks at 3.50 each, so its total is 14.00, and order 104 is 3 mice at 24.99, so its total is 74.97. Order 103 keeps the desk lamp's NULL category. Product 4 has no orders and does not appear. ### Constraints and Clarifications - `Products` has at most 100 rows and `Orders` at most 10,000. - Every `Orders.product_id` matches an existing product, so every order appears exactly once in the output. - `quantity` is a positive integer and `price` is a positive decimal. - `category` can be NULL. Return it as NULL. - Products with no orders do not appear. - Several orders from the same customer stay as separate rows. The task has no aggregation.

Overview: Write a SQL query that joins an orders table to a products table and returns each order with its product details and a computed total amount rounded to two decimals. It tests inner joins, column qualification and exact decimal arithmetic.

MongoDB logo
MongoDB
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of Orders is one order for a single product, and Products holds the catalog. Join the two tables and return one row per order with order_id, customer_name, quantity, order_date, product_id, product_name, category, price and total_amount, where total_amount is quantity * price rounded to 2 decimals. Order the result by order_id ascending.

Input Table(s)

Table Products

ColumnTypeMeaning
product_idINTProduct identifier
product_nameVARCHAR(100)Product name, never NULL
categoryVARCHAR(50)Product category, may be NULL
priceDECIMAL(10,2)Unit price, never NULL

Primary key: product_id.

Table Orders

ColumnTypeMeaning
order_idINTOrder identifier
customer_nameVARCHAR(100)Customer who placed the order, never NULL
product_idINTProduct ordered
quantityINTUnits ordered, never NULL
order_dateDATEDate of the order, never NULL

Primary key: order_id. Foreign key: Orders.product_id references Products.product_id.

Output Contract

  • Columns, in this order: order_id , customer_name , quantity , order_date , product_id , product_name , category , price , total_amount .
  • One row per order.
  • total_amount is quantity * price , rounded to 2 decimals.
  • Rows sorted by order_id ascending. order_id is unique, so the row order is fully determined.

Example

Input Products:

product_idproduct_namecategoryprice
1Wireless MouseElectronics24.99
2NotebookStationery3.50
3Desk LampNULL18.00
4USB CableElectronics8.25

Input Orders:

order_idcustomer_nameproduct_idquantityorder_date
101Alice242024-03-01
102Bob112024-03-02
103Alice322024-03-02
104Chen132024-03-05

Expected output:

order_idcustomer_namequantityorder_dateproduct_idproduct_namecategorypricetotal_amount
101Alice42024-03-012NotebookStationery3.5014.00
102Bob12024-03-021Wireless MouseElectronics24.9924.99
103Alice22024-03-023Desk LampNULL18.0036.00
104Chen32024-03-051Wireless MouseElectronics24.9974.97

Order 101 is 4 notebooks at 3.50 each, so its total is 14.00, and order 104 is 3 mice at 24.99, so its total is 74.97. Order 103 keeps the desk lamp's NULL category. Product 4 has no orders and does not appear.

Constraints and Clarifications

  • Products has at most 100 rows and Orders at most 10,000.
  • Every Orders.product_id matches an existing product, so every order appears exactly once in the output.
  • quantity is a positive integer and price is a positive decimal.
  • category can be NULL. Return it as NULL.
  • Products with no orders do not appear.
  • Several orders from the same customer stay as separate rows. The task has no aggregation.