Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for Duplicates

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

Your question is Window Functions for Duplicates. 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

Capgemini delivery teams use worklogs to monitor project effort and billing quality. Write a PostgreSQL query using the consultants and work_logs tables to audit January 2025 worklogs.

Requirements

  1. Join each worklog to its consultant and exclude worklogs whose consultant does not exist.
  2. Identify duplicate business records using consultant_id, project_code, work_date, hours_logged, and activity_code. Return a duplicate count, a duplicate row number, and an is_duplicate flag.
  3. Within each project, calculate DENSE_RANK() by hours_logged descending, a running total of hours by work date and log_id, and the previous and next logged hours using LAG() and LEAD().
  4. Return only worklogs dated from 2025-01-01 through 2025-01-31, ordered by project, date, and log ID.

Schema

consultants
ColumnTypeDescription
consultant_idPKINTUnique consultant identifier
consultant_nameVARCHAR(100)Consultant full name
specialtyVARCHAR(80)Primary delivery specialty
work_logs
ColumnTypeDescription
log_idPKINTUnique worklog identifier
consultant_idINTReferences consultants.consultant_id
project_codeVARCHAR(20)Project identifier
work_dateDATEDate when work was performed
hours_loggedNUMERIC(6,2)Hours recorded for the worklog
activity_codeVARCHAR(20)Activity classification
Tablesconsultantswork_logs
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results