Unique Index — SQL Technical Interview Questions & Answers

Unique Index

Unique Index

  • CREATE UNIQUE INDEX idx_email ON students(email); is UNIQUE constraint + index. Inserting the same email twice fails. Search by email is fast.
  • Same idea as UNIQUE KEY in CREATE TABLE. Pick one style.
  • Trap: UNIQUE index on a column that can legally repeat.
  • On the example next to this theory: Unique Index — keys and constraints protect data integrity; indexes speed lookups.

Technical Interview Tip: CREATE UNIQUE INDEX idx_email ON students(email);

Code Examples for Unique Index

Unique Index — 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 Index — keys and constraints protect data integrity; indexes speed lookups.

How to Explain Unique Index to an Interviewer

UNIQUE INDEX vs INDEX?

UNIQUE rejects duplicates Both speed lookups