Hone logo
Hone
Problems

Analyzing Customer Order Frequency

Businesses often need to understand how frequently customers place orders. This challenge focuses on using the COUNT aggregate function in SQL to determine the number of orders placed by each customer. This is a fundamental query for customer relationship management (CRM) and business intelligence.

Problem Description

You are given a table named Orders with the following schema:

  • OrderID (INTEGER): Unique identifier for each order.
  • CustomerID (INTEGER): Identifier for the customer who placed the order.
  • OrderDate (DATE): Date when the order was placed.

Your task is to write a SQL query that returns a result set containing two columns: CustomerID and OrderCount. The CustomerID column should list each unique customer ID present in the Orders table. The OrderCount column should represent the number of orders placed by each corresponding customer. The result set should be ordered by CustomerID in ascending order.

Key Requirements:

  • Use the COUNT() aggregate function to calculate the number of orders per customer.
  • Use GROUP BY to group the orders by CustomerID.
  • The output must include CustomerID and OrderCount columns.
  • The output must be sorted by CustomerID in ascending order.

Expected Behavior:

The query should accurately count the number of orders for each customer and present the results in a clear and organized manner. If a customer has no orders, they should not appear in the result set.

Edge Cases to Consider:

  • The Orders table might be empty. In this case, the query should return an empty result set.
  • The Orders table might contain duplicate OrderID values (though this is unlikely in a well-designed database). The COUNT() function will correctly handle this by counting each unique order.
  • The CustomerID column might contain NULL values. These should be treated as a single group and counted accordingly (though the problem description implies distinct customers).

Examples

Example 1:

Input:
Orders Table:
| OrderID | CustomerID | OrderDate    |
|---------|------------|--------------|
| 1       | 101        | 2023-01-15   |
| 2       | 102        | 2023-02-20   |
| 3       | 101        | 2023-03-10   |
| 4       | 103        | 2023-04-05   |
| 5       | 101        | 2023-05-12   |

Output:
| CustomerID | OrderCount |
|------------|------------|
| 101        | 3          |
| 102        | 1          |
| 103        | 1          |

Explanation: Customer 101 placed 3 orders, Customer 102 placed 1 order, and Customer 103 placed 1 order.

Example 2:

Input:
Orders Table:
| OrderID | CustomerID | OrderDate    |
|---------|------------|--------------|
| 1       | 101        | 2023-01-15   |
| 2       | 101        | 2023-02-20   |

Output:
| CustomerID | OrderCount |
|------------|------------|
| 101        | 2          |

Explanation: Customer 101 placed 2 orders.

Example 3: (Empty Table)

Input:
Orders Table:
(Empty)

Output:
(Empty)

Explanation: The table is empty, so there are no customers and no orders to count.

Constraints

  • The Orders table will contain at least 0 rows.
  • CustomerID will be an integer.
  • OrderDate will be a valid date.
  • The query should execute within a reasonable time (less than 1 second) for tables with up to 10,000 rows.

Notes

  • The COUNT(*) function counts all rows in a group, including rows with NULL values in other columns.
  • Consider using aliases (e.g., COUNT(*) AS OrderCount) to make the query more readable.
  • The GROUP BY clause is essential for aggregating data based on the CustomerID.
  • The ORDER BY clause ensures the results are presented in a consistent and predictable order.
  • Focus on using the COUNT aggregate function correctly in conjunction with GROUP BY to achieve the desired result. No complex joins or subqueries are required for this problem. Pseudocode:
// Assume a table named Orders with columns OrderID, CustomerID, OrderDate

// 1. Group the rows in the Orders table by CustomerID.
// 2. For each group (i.e., for each CustomerID), count the number of rows.
// 3. Name the count as OrderCount.
// 4. Return the CustomerID and OrderCount for each group.
// 5. Order the results by CustomerID in ascending order.

SELECT CustomerID, COUNT(*) AS OrderCount
FROM Orders
GROUP BY CustomerID
ORDER BY CustomerID ASC;
Loading editor...
plaintext