Repeat Customers

MediumSQLDatabaseSorting

Description

Write a SQL query to return each customer who placed at least 2 orders, along with their order count. Order by customer name ascending. **Output columns (in order):** customer, cnt

Table:orders
ColumnType
idINT
customerTEXT

Examples

Input:CREATE TABLE orders (id INT, customer TEXT); INSERT INTO orders VALUES (1,'Ann'),(2,'Ben'),(3,'Ann'),(4,'Ann'),(5,'Ben');
Output:Ann|3 Ben|2
Explanation:

Orders are grouped by customer and only those with two or more are kept, each shown with how many orders they placed.

Input:CREATE TABLE orders (id INT, customer TEXT); INSERT INTO orders VALUES (1,'X'),(2,'X'),(3,'Y'),(4,'Y'),(5,'Z');
Output:X|2 Y|2
Explanation:

Orders are grouped by customer and only those with two or more are kept, each shown with how many orders they placed.

Input:CREATE TABLE orders (id INT, customer TEXT); INSERT INTO orders VALUES (1,'Z'),(2,'Z'),(3,'Z'),(4,'A'),(5,'A');
Output:A|2 Z|3
Explanation:

Orders are grouped by customer and only those with two or more are kept, each shown with how many orders they placed.

Constraints

  • Use standard SQL (SQLite).

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.