SQL Problems


SQL Query to Find Top 3 Most Purchased Products by Customers

This technical problem involves writing an SQL query that returns the top 3 most purchased products by customers. The example input and output are provided.

Problem

Write an SQL query that returns the top 3 most purchased products by customers.
Example input:
customer_id | product_id
1 | 4
2 | 3
3 | 2
4 | 1
Example output:
customer_id | product_id
4 | 1
3 | 2
2 | 3

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by AskAI
SELECT customer_id, product_id
FROM table
ORDER BY product_id DESC
LIMIT 3;

/*

The solution above is optimal because it uses the ORDER BY and LIMIT clauses to sort the table by product_id in descending order and then limit the results to the top 3.

*/

A.I. Evaluation of the Solution

The solution above is optimal because it uses the ORDER BY and LIMIT clauses to sort the table by product_id in descending order and then limit the results to the top 3.

Evaluated at: 2022-10-31 02:36:36