Quick Overview

Write a SQL query over an Orders table that returns the customer number of the customer who placed the most orders. It tests counting rows per customer, picking the top group, and how a guarantee of a unique winner affects the query you write.

Find the customer who placed the most orders

Company: Databricks

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `Orders` table has one row per order. Write a query that returns the `customer_number` of the customer who placed the largest number of orders. ```hint Start with the counts Work out how many orders each customer has before deciding how to keep only the largest. ``` ### Input Table(s) `Orders` | Column | Type | Meaning | |---|---|---| | order_number | INT | Identifier of the order | | customer_number | INT | Customer who placed the order (NOT NULL) | Primary key: `order_number`. A customer can appear in many rows. ### Output Contract - One column: `customer_number` (INT). - Exactly one row, holding the customer with the most orders. - The result has a single row, so no ordering is required. No rounding applies. ### Example `Orders` | order_number | customer_number | |---|---| | 1 | 1 | | 2 | 2 | | 3 | 3 | | 4 | 3 | Expected result: | customer_number | |---| | 3 | Customer 3 has two orders, while customers 1 and 2 have one each, so customer 3 is returned. ### Constraints and Clarifications - `Orders` contains at least one row. - Exactly one customer has placed more orders than every other customer, so the answer is unique and there is no tie to break. - `customer_number` is never NULL. - `order_number` is unique, so each row counts as one order.

Overview: Write a SQL query over an Orders table that returns the customer number of the customer who placed the most orders. It tests counting rows per customer, picking the top group, and how a guarantee of a unique winner affects the query you write.

The `orders` table has one row per order, with columns `order_number` (INTEGER, primary key, identifier of the order) and `customer_number` (INTEGER, NOT NULL, the customer who placed the order). A customer can appear in many rows. Write a query that returns the `customer_number` of the customer who placed the largest number of orders. Return one column, `customer_number` (INTEGER), in exactly one row holding the customer with the most orders. The result has a single row, so no ordering is required. No rounding applies. You can rely on these facts: `orders` contains at least one row; `order_number` is unique, so each row counts as one order; `customer_number` is never NULL; and exactly one customer has placed more orders than every other customer, so the answer is unique and there is no tie to break.

Tables

orders(order_number INTEGER, customer_number INTEGER)

Hints

  1. Work out how many orders each customer has before deciding how to keep only the largest.
DatabricksData ScientisteasyData Manipulation (SQL/Python)

Customer With the Most Orders

The orders table has one row per order, with columns order_number (INTEGER, primary key, identifier of the order) and customer_number (INTEGER, NOT NULL, the customer who placed the order). A customer can appear in many rows. Write a query that returns the customer_number of the customer who placed the largest number of orders.

Return one column, customer_number (INTEGER), in exactly one row holding the customer with the most orders. The result has a single row, so no ordering is required. No rounding applies.

You can rely on these facts: orders contains at least one row; order_number is unique, so each row counts as one order; customer_number is never NULL; and exactly one customer has placed more orders than every other customer, so the answer is unique and there is no tie to break.

Expected Output

Tables

orders
orders
ColumnType
order_numberINTEGER
customer_numberINTEGER

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions