Why interviewers love this: it checks whether you know the real relational-model term and the SQL feature that actually enforces it.
Question: What is an alternate key in SQL?
Answer: An alternate key is a column, or group of columns, that can uniquely identify a row, but was not chosen as the primary key. In plain SQL, you usually enforce it with a UNIQUE constraint, and if you want it to behave like a true key, you also make it NOT NULL. A table can have more than one alternate key.
Interview-Ready Answer: I’d say an alternate key is a candidate key that was not selected as the primary key. It still uniquely identifies each row, so in SQL I normally enforce it with a UNIQUE constraint, often together with NOT NULL. A common example is a user table where user_id is the primary key, but email and username are alternate keys.
Detailed Explanation: In relational design, a candidate key is any set of columns that can uniquely identify a row. A primary key is the one candidate key we pick as the main identifier. Every other candidate key becomes an alternate key. The big gotcha is that SQL does not have a separate ALTERNATE KEY keyword; it is a design concept, not a special command.
email, username, or a composite like (country_code, tax_id).user_id.UNIQUE constraint. Most engines back that with a unique index, which is a data structure that keeps values sorted and makes duplicate checks fast.INSERT or UPDATE, the engine looks up the new value in that index. If it finds an existing match, it rejects the change.Use an alternate key when the business has another real-world identifier that must never repeat. Good examples are email addresses, employee numbers, product codes, VINs, or passport numbers. This helps in two ways: it protects data quality, and it gives you a safe lookup path for queries and joins.
| Term | Meaning | SQL enforcement | Can there be many? |
|---|---|---|---|
| Candidate key | Any unique identifier | Usually UNIQUE | Yes |
| Primary key | Main chosen identifier | PRIMARY KEY | No, one per table |
| Alternate key | Unused candidate key | UNIQUE + often NOT NULL | Yes |
Uniqueness checks are fast because the database usually uses a B-tree index, a tree-shaped structure that keeps lookups close to O(log n). In real terms, even millions of rows are manageable because the engine only follows a few index levels, often just a handful of page reads when the index is cached in memory. The trade-off is write cost: every extra unique constraint adds some overhead on insert and update, because the database must check the new value before it can commit it.
NULL, some databases treat that differently under UNIQUE; to make it a true business key, add NOT NULL.Memory trick: think “one passport, many valid IDs” — the primary key is the passport you pick for the table, and alternate keys are the other unique IDs that still prove identity.
Real-World Example: In a checkout or account-service database, a users table often has an internal user_id primary key plus alternate keys like email and username. The app uses user_id for joins because it is short and stable, but customers log in with email, so that email must also stay unique.
What goes wrong when this is misunderstood? Imagine a team only adds a primary key and forgets the alternate key constraint on email. Two accounts can accidentally be created with the same email address. Then password reset emails may point to the wrong person, login lookups can return the wrong row, and support sees complaints like “I can see another user’s profile” or “my reset link stopped working.” In logs, you might notice duplicate email rows, mismatched account IDs, or application errors during account recovery. The bug is subtle because the database looks healthy until a business rule is violated.
-- Demonstration of an alternate key in SQL: a column that is unique, but not the primary key.
-- Here, user_id is the primary key, while email and username are alternate keys.
CREATE TABLE account_users (
user_id INTEGER NOT NULL PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
username VARCHAR(50) NOT NULL UNIQUE,
full_name VARCHAR(100) NOT NULL
);
INSERT INTO account_users (user_id, email, username, full_name) VALUES
(1, 'ada@example.com', 'ada', 'Ada Lovelace'),
(2, 'grace@example.com', 'grace', 'Grace Hopper'),
(3, 'linus@example.com', 'linus', 'Linus Torvalds');
-- This query should return no rows. If it returns rows, the uniqueness rule has been broken.
SELECT email, COUNT(*) AS row_count
FROM account_users
GROUP BY email
HAVING COUNT(*) > 1;
SELECT *
FROM account_users
ORDER BY user_id;
-- Failure path: this would be rejected because email is an alternate key.
-- INSERT INTO account_users (user_id, email, username, full_name)
-- VALUES (4, 'ada@example.com', 'ada2', 'Duplicate Email');
-- Failure path: a real alternate key should not be NULL, so this is blocked by NOT NULL.
-- INSERT INTO account_users (user_id, email, username, full_name)
-- VALUES (5, NULL, 'no_email', 'Null Email');
Follow-up & Tricky Questions:
UNIQUE constraint, and add NOT NULL if the column must never be missing. That makes the rule explicit and protects your data on every insert and update.(country_code, tax_id). The pair together must be unique, even if neither column is unique by itself.UNIQUE automatically make a column a true alternate key? Not always. In many databases, UNIQUE alone may still allow NULL values, so for a real key-like rule you usually want UNIQUE NOT NULL.email and employee_number are common alternate keys when id is the primary key.Common Mistakes:
UNIQUE.NOT NULL. Correction: if the column is a true identity for a row, make it non-null as well as unique.Memory Hook: Primary key = the ID card you choose to show first. Alternate key = another official ID that also proves who the row is.
Cheat Sheet:
UNIQUE to enforce it.NOT NULL for a strict business key.Practice Tasks:
products table with product_id as the primary key and sku as an alternate key.order_items table using (order_id, line_no).