Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Optimize SQL Query Performance
00:00
5 left

Optimize SQL Query Performance

HardSQL · PostgreSQL

Problem

What steps would you take to ensure that your SQL queries are optimized for performance?

Using the provided PostgreSQL schema, write the query that should be optimized and describe the checks, indexing decisions, and validation steps you would use. The result must include active employees and their payroll totals for the first quarter of 2025.

Output

  1. One row per active employee, including employees without qualifying payroll records.
  2. Columns: employee_id, employee_name, total_paid, payment_count.
  3. Sort by total_paid descending, then employee_id ascending.

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee display name
statusVARCHAR(20)Employment status
payroll_records
ColumnTypeDescription
payroll_idPKINTUnique payroll record identifier
employee_idINTEmployee associated with the payroll record
paid_atTIMESTAMPTimestamp when the payroll payment was issued
amountNUMERIC(12,2)Payment amount
Tablesemployeespayroll_records
Interviewer

Your question is Optimize SQL Query Performance. 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.