Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Orders Count Per Customer

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Orders Count Per Customer. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Dayforce needs a customer-level order count for a QA validation report. Write a PostgreSQL query that returns every customer and the total number of orders associated with that customer, including customers who have placed no orders.

Requirements

  1. Use a LEFT JOIN from customers to orders so customers without orders remain in the result.
  2. Count orders per customer using the order primary key, not COUNT(*), so unmatched customers receive a count of zero.
  3. Return the customer name and an aliased column named order_count.
  4. Sort the output alphabetically by customer name.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
created_atDATEDate the customer record was created
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer associated with the order
order_dateDATEDate the order was placed
Tablescustomersorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results