Find Top 10 Customers by Total Purchase Amount


Find Top 10 Customers by Total Purchase Amount

To find the top 10 customers by total purchase amount, write a SQL query that orders the customers by purchase_amount in descending order.

Problem

Given a table of data with customer information, write a SQL query to find the top 10 customers by total purchase amount.
Example input:
| customer_id | name       | purchase_amount |
|--------------|------------|-----------------|
| 1            | John Doe   | 100             |
| 2            | Jane Smith | 200             |
| 3            | Joe Smith  | 300             |
Example output:
| customer_id | name       | purchase_amount |
|--------------|------------|-----------------|
| 3            | Joe Smith  | 300             |
| 2            | Jane Smith | 200             |
| 1            | John Doe   | 100             |

Solution

by AskAI
SELECT customer_id, name, purchase_amount
FROM customers
ORDER BY purchase_amount DESC
LIMIT 10;

/*

The solution is optimal because it uses the ORDER BY clause to sort the table by purchase_amount in descending order, and then uses the LIMIT clause to return the first 10 rows.

*/

A.I. Evaluation of the Solution

The solution is optimal because it uses the ORDER BY clause to sort the table by purchase_amount in descending order, and then uses the LIMIT clause to return the first 10 rows.

Evaluated at: 2022-10-10 00:50:17