Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Total Orders by Client Type

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

Your question is SQL Total Orders by Client Type. 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

PenFed Credit Union's marketing analytics team needs to compare order activity from new and existing clients during January 2025. A client is new if January contains their first recorded order, and existing if they placed an order before January 2025.

Write a PostgreSQL query that calculates the total number of January 2025 orders for each client type.

Requirements

  1. Determine each client's first recorded order date.
  2. Include only orders from 2025-01-01 through 2025-01-31, using an exclusive upper date boundary.
  3. Classify each included order as new or existing with CASE and return both categories, including a zero count if a category has no orders.
  4. Return client_type and total_orders, ordered with new before existing.

Schema

clients
ColumnTypeDescription
client_idPKINTUnique PenFed client identifier
client_nameVARCHAR(100)Client's display name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
client_idINTReferences clients.client_id
order_dateDATEDate the order was placed
Tablesclientsorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results