Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
PostgreSQL Customer Spending Metrics and Revenue Ranks
00:00
5 left

PostgreSQL Customer Spending Metrics and Revenue Ranks

MediumSQL · PostgreSQL

Problem

Bunch of weird stuff about sql. Explain each function, method, etc. Basics for python, what is def, how to write a loop, etc.

For this SQL exercise, write one query that summarizes qualifying customer orders and demonstrates common PostgreSQL query functions.

Output

  1. One row per customer, including customers without qualifying orders.
  2. Return customer_id, customer_name, order_count, total_spend, avg_order_value, categories, customer_tier, and revenue_rank.
  3. Include completed or pending orders from 2024-01-01 through 2024-03-31, exclude cancelled orders, and order by revenue_rank, then customer_id.

Output details

  • Use qualifying order dates from 2024-01-01 inclusive through 2024-04-01 exclusive.
  • Use 0 for total_spend and avg_order_value when no qualifying orders exist.
  • Round avg_order_value to exactly two decimal places.
  • List distinct categories alphabetically, separated by , , and use No category when none exist.
  • Assign Premium for totals at least 400, Standard for totals at least 200, and Starter otherwise.
  • Rank customers by total_spend descending, assigning the same rank to ties.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer's name
regionVARCHAR(50)Customer region
customer_statusVARCHAR(20)Current customer status
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
product_idINTProduct associated with the order
order_dateDATEDate the order was placed
order_statusVARCHAR(20)Order processing status
amountNUMERIC(10,2)Order amount
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product name
categoryVARCHAR(50)Product category
Tablescustomersordersproducts
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.