Unique Key — SQL Technical Interview Questions & Answers

Unique Key

Unique Key

  • UNIQUE means no duplicate values in that column (or column set), but one NULL may be allowed depending on version/SQL mode — in practice treat UNIQUE email as ‘each email once’.
  • PRIMARY KEY is UNIQUE + NOT NULL + the main identifier (only one PK). You can have many UNIQUE keys (email, phone).
  • Trap: UNIQUE vs PRIMARY KEY as identical. Trap: UNIQUE on a column that legitimately repeats (city).
  • On the example next to this theory: Unique Key — keys and constraints protect data integrity; indexes speed lookups.

Technical Interview Tip: UNIQUE email. PRIMARY KEY roll. Many UNIQUE, one PK.

Code Examples for Unique Key

Unique Key — sample query

CREATE TABLE authors (
  id INTEGER PRIMARY KEY,
  email TEXT UNIQUE NOT NULL
);
CREATE TABLE books (
  id INTEGER PRIMARY KEY,
  author_id INTEGER NOT NULL,
  title TEXT NOT NULL,
  FOREIGN KEY (author_id) REFERENCES authors(id)
);
INSERT INTO authors VALUES (1, 'a@x.com');
INSERT INTO books VALUES (1, 1, 'SQL Basics');
SELECT * FROM books;

Unique Key — keys and constraints protect data integrity; indexes speed lookups.

How to Explain Unique Key to an Interviewer

UNIQUE vs PRIMARY KEY?

PK cannot be NULL and there is one Many UNIQUE keys allowed