← SQL workplace practice

Workforce and service coverage · Original fictional data · Free files and full answers

Flag insufficient rest between consecutive duties

Order shifts by absolute start,id within employee. Between each previous shift and current shift calculate rest_minutes, which may be negative. Return employee,previous_id,shift_id,rest_minutes when rest<660. Sort employee,shift_id.

Use this fixed business case to build a checked CSV and a replayable SQL handoff. No account required. Scenario datasets are shared across related exercises; each exercise has its own decision and output contract.

Your output contract

  1. Order shifts by absolute start,id within employee. Between each previous shift and current shift calculate rest_minutes, which may be negative. Return employee,previous_id,shift_id,rest_minutes when rest<660. Sort employee,shift_id.
  2. Return exactly the declared columns and order. Money uses integer cents; time and date units follow the task and field dictionary. NULL is unknown and differs from numeric zero.
  3. These are original fictional records and exercise policies. Scenario data may be shared with related tasks, whose decisions and output contracts differ.

Return columns exactly as employee, previous_id, shift_id, rest_minutes. Sort order follows the task. Output has 2 rows; an empty CSV field means NULL.

Source data

shifts

10 source rows · Download shifts CSV

id INTEGERemployee TEXTday INTEGERstart INTEGERend INTEGERbreak_minutes INTEGERrole TEXT
1Ann0132036030lead
2Ann140088030lead
3Ann2500110060desk
4Bo0480102030desk
5Bo150098030desk
6Bo190012000desk
7Cy0600120060lead
8Cy2540108030lead
9Dee1480108060desk
10Dee2480108060desk

skills

6 source rows · Download skills CSV

employee TEXTskill TEXT
Annlead
Anndesk
Bodesk
Cylead
Cydesk
Deedesk

leave_events

7 source rows · Download leave_events CSV

id INTEGERemployee TEXTdays INTEGERkind TEXT
1Ann10grant
2Ann3use
3Ann1reverse_use
4Bo8grant
5Bo9use
6Cy12grant
7Cy2use

crew_cost

4 source rows · Download crew_cost CSV

employee TEXThourly_cents INTEGER
Ann500
Bo1500
Cy3000
Dee1000
Field dictionary and schema
shifts:
  id INTEGER — Stable source record identifier; not a row position.
  employee TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  day INTEGER — Integer day index, or ISO YYYY-MM-DD date when stored as text; the task declares cutoff semantics.
  start INTEGER — Beginning boundary; inclusive under the task interval policy.
  end INTEGER — Ending boundary; exclusive unless the goal states otherwise.
  break_minutes INTEGER — Unpaid break duration deducted from the shift elapsed minutes.
  role TEXT — Literal business key or category label. Match exactly and retain NULL where present.

skills:
  employee TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  skill TEXT — Literal business key or category label. Match exactly and retain NULL where present.

leave_events:
  id INTEGER — Stable source record identifier; not a row position.
  employee TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  days INTEGER — Exact integer business measurement. Its specific business rule and eligibility are declared in the task.
  kind TEXT — Literal business key or category label. Match exactly and retain NULL where present.

crew_cost:
  employee TEXT — Literal business key or category label. Match exactly and retain NULL where present.
  hourly_cents INTEGER — Exact integer monetary cents; no implicit currency conversion.

Download complete SQLite setup

A method you can reuse

  1. Order shifts by absolute start,id within employee. Between each previous shift and current shift calculate rest_minutes, which may be negative. Return employee,previous_id,shift_id,rest_minutes when rest<660. Sort employee,shift_id.
  2. For the first output record, employee = Ann; previous_id = 1; shift_id = 2; rest_minutes = 40.
  3. Inspect the source grain, keys and ties. Compare each result column with the full worked SQL and expected CSV.

Watch for this wrong turn

Subtracting only start clock values ignores both the previous end and overnight boundaries.

Run and check in SQLite

Your SQL runs locally in the existing SQLite worker. The checker compares every column, row, type, value and row order with this fixed output. It accepts equivalent SELECT or WITH queries. Browser storage keeps your SQL on this device when available.

Complete worked SQL and expected output
WITH x AS (SELECT *,day*1440+start s,day*1440+CASE WHEN end<start THEN end+1440 ELSE end END e FROM shifts), y AS (SELECT *,LAG(id) OVER(PARTITION BY employee ORDER BY s,id) p,LAG(e) OVER(PARTITION BY employee ORDER BY s,id) pe FROM x) SELECT employee,p previous_id,id shift_id,s-pe rest_minutes FROM y WHERE s-pe<660 ORDER BY employee,id;
"employee","previous_id","shift_id","rest_minutes"
"Ann","1","2","40"
"Bo","5","6","-80"
Reference SQL · Complete expected CSV · Empty answer CSV

Choose another SQL task · Original data exception cases · Larger workplace projects