Q: Three top customers (names) in terms of total purchase made by them. List is to be in descending order in terms of total purchase amount?

 Q: Three top customers (names) in terms of total purchase made by them. List is to be in descending order in terms of total purchase amount?

Ans: Here is an example of a SQL query that can be used to retrieve the top three customers (names) in terms of total purchase amount, in descending order:

SELECT c.name, SUM(o.total_amount) as total_purchase FROM customers c JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.customer_id ORDER BY total_purchase DESC LIMIT 3;

This query retrieves the name column from the customers table and join it with the orders table using customer_id. Then it groups the results by customer_id and sum the total_amount from the orders table to calculate total purchase by each customer. After that, it orders the results by total_purchase in descending order, and limits the number of returned rows to 3, which will give you the top three customers in terms of total purchase amount.

This query assumes that there are two tables: "customers" which contains columns customer_id, name and "orders" which contains columns customer_id, total_amount.

Please keep in mind that table names, column names and the actual query may vary depending on the database structure and naming conventions used.

No comments:

Post a Comment