Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Consecutive Shifts Over 12 Hours
00:00
5 left

Consecutive Shifts Over 12 Hours

MediumSQL · PostgreSQL

Problem

Write a query to identify employees who have worked consecutive shifts exceeding twelve hours.

Treat two shifts as consecutive when the later shift starts no more than 30 minutes after the earlier shift ends. Return each qualifying pair once, excluding incomplete shifts. Sort the results by employee ID and the first shift's start time.

Output

  1. One row per qualifying consecutive shift pair
  2. Columns: employee_id, employee_name, first_shift_id, first_shift_start, first_shift_end, next_shift_start, next_shift_end, and consecutive_hours
  3. Include only pairs spanning more than 12 hours from the first shift start to the next shift end
  4. Order by employee_id, then first_shift_start

Schema

employees
ColumnTypeDescription
employee_idPKINTUnique employee identifier
employee_nameVARCHAR(100)Employee's full name
departmentVARCHAR(80)Employee's department
shifts
ColumnTypeDescription
shift_idPKINTUnique shift identifier
employee_idINTEmployee assigned to the shift
shift_startTIMESTAMPShift start timestamp
shift_endTIMESTAMPShift end timestamp
Tablesemployeesshifts
Interviewer

Your question is Consecutive Shifts Over 12 Hours. 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.