Skip to content
Onsite

Histogram of orders per user, including users with none

MediumP3Coding

Tables:

users(id int primary key)

orders(id int primary key, user_id int)

Build a histogram of orders per user: how many users placed 0 orders, how many placed 1, and so on. Users with no orders must be included. Only counts that occur are listed.

Output columns: order_count, user_count.

Schema

CREATE TABLE users (
  id int PRIMARY KEY
);
CREATE TABLE orders (
  id int PRIMARY KEY,
  user_id int NOT NULL
);

Example 1

Input
users 1..4; orders: (1, 1), (2, 2), (3, 2), (4, 2)
Output
(0, 2), (1, 1), (3, 1)

Constraints

  • PostgreSQL dialect.

Hints

PostgreSQL
Run checks the 3 visible tests, Submit all 5.