SQL Unique Index

Does everything a regular index does, plus rejects any row that would duplicate an existing value.

What & Why

A unique index both speeds up lookups on a column AND enforces that no two rows can share the same value in it. Every PRIMARY KEY is backed by a unique index automatically — this lesson is about building one explicitly on a different column.

See How It Works

BUSINESS QUESTION

Protect the business rule that product feature names are unique regardless of letter case.

idnamedescriptionteamreleased_atdeprecated_at
1Activation ChecklistGuides new users through setupgrowth2024-01-20NULL
2CSV ExportExports report dataplatform2024-02-14NULL
3Invite NudgesPrompts workspace collaborationgrowth2024-03-05NULL
4Legacy DashboardOriginal reporting surfaceanalytics2023-08-102024-06-01
EXAMPLE QUERY
SELECT
  LOWER(name) AS normalized_feature_name,
  COUNT(*) AS occurrences
FROM product.features
GROUP BY LOWER(name)
HAVING COUNT(*) > 1
ORDER BY normalized_feature_name;
RESULT — no duplicate normalized feature names
normalized_feature_nameoccurrences

Every feature name remains unique after LOWER normalization, so HAVING returns no rows.

Advanced business practice

Sign up free to try it on a real business scenario