Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Optimize Database Queries
00:00
5 left

Optimize Database Queries

MediumSQL · PostgreSQL

Problem

How would you optimize database queries using indices or denormalization?

Write a PostgreSQL query using the supplied customers and orders tables, then explain which indexes or denormalized structures could improve it.

Output

  1. One row per customer.
  2. Return customer_id, customer_name, completed_order_count, and total_completed_amount.
  3. Include all customers, considering only completed orders placed in January 2025. Sort by total amount descending, order count descending, then customer ID ascending.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer associated with the order
order_dateDATEDate the order was placed
statusVARCHAR(20)Order lifecycle status
total_amountNUMERIC(12,2)Total monetary value of the order
Tablescustomersorders
Interviewer

Your question is Optimize Database Queries. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.