SQL-Engineering-Handbook

04 · Employee Labelling

Difficulty: Intermediate · Estimated time: 15 min Schema: 00_Sample_Schema.sql

ELSE branch absorbing a new category

Introduction

A three-table join (employees → departments → locations) used to build a human-readable label column. This lesson’s real lesson is scalability of WHEN branches — the original two-city version doesn’t generalize, and that’s the point.

Learning Objectives

Business Context

HR dashboards and workforce reports are read by people, not systems — “Nagpur Employee” is a label a regional VP can scan in a table of 500 rows; location_id = 1 is not.

SQL

See 04_Employee_Labelling.sql.

Engineering Notes

Best Practices

Common Mistakes

Mistake Consequence
ELSE re-using a real value (‘Pune Employee’) as the fallback Every new/unanticipated category is silently mislabeled as an existing one — the most dangerous kind of CASE bug because it produces no error, just wrong data
Hardcoding every known value as a WHEN branch Doesn’t scale; breaks the moment a new value is added upstream

Interview Questions

  1. What’s wrong with using a real category name as the ELSE fallback?
  2. When should you prefer string concatenation over CASE for a labelling task?
Answers 1. It makes every future, unanticipated category silently indistinguishable from that one hardcoded category — a mislabeling bug that produces no error and is easy to miss in QA. 2. When the output is a pure, mechanical function of the input column (e.g. appending a fixed suffix) with no actual business rule or exception — CASE adds no value there and actively hides a bug for new inputs.

Cross References