Essential cookies keep authentication working. With your permission, we also use analytics cookies to understand and improve the product. Read our Privacy Policy

DataEngPrep.tech
QuestionsPracticeAI CoachDashboardPricingBlog
ProLogin
Home/Questions/SQL/What is the difference between a primary key and a unique key?

What is the difference between a primary key and a unique key?

SQLhard2 min read

Reviewed by Aditya Kumar · Last reviewed 2026-03-24

A primary key uniquely identifies each row in a table, disallows NULL values, and there can only be one per table. A unique key also enforces uniqueness across specified columns but allows a single…

🤖 Analyze Your Answer
Frequency
Low
Asked at 4 companies
Category
487
questions in SQL
Difficulty Split
130E|271M|86H
in this category
Total Bank
1,863
across 7 categories
Asked at these companies
AccentureCognizantEPAMYash Technologies
Interview Pro Tip

Red Flag: Saying 'unique key allows multiple NULLs' (depends on SQL dialect). Pro-Move: Clarify that in data lakes, PK is a logical concept—Delta Lake enforces via merge; enforcement is eventual.

Key Concepts Tested
sparksql

Why This Question Matters

This hard-level SQL question appears frequently in data engineering interviews at companies like Accenture, Cognizant, EPAM, and 1 others. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (spark, sql) will help you answer variations of this question confidently.

How to Approach This

This is a senior-level question that tests architectural thinking. Lead with the high-level design, then drill into specifics. Discuss trade-offs explicitly - there is rarely one correct answer. Show awareness of scale, fault tolerance, and operational complexity. The expert answer includes a code example that demonstrates the implementation pattern.

Expert Answer
416 wordsIncludes code

A primary key uniquely identifies each row in a table, disallows NULL values, and there can only be one per table. A unique key also enforces uniqueness across specified columns but allows a single NULL value in a column, and multiple unique keys can exist on a table.

Mechanics and Purpose

Primary Key (PK): Its fundamental purpose is to define the identity of a record. It is crucial for establishing referential integrity, as other tables can link to it via foreign keys. A PK inherently implies NOT NULL and UNIQUE constraints. Most relational databases automatically create a unique, non-clustered index for the PK, and often use it as the clustering key* (e.g., in SQL Server, MySQL InnoDB), dictating the physical storage order of data rows. This optimizes range queries and joins. * Unique Key (UK): Its purpose is to ensure that all values in the specified column(s) are distinct, serving as an "alternate key." A UK creates a unique index, enhancing lookup performance. Unlike a PK, a unique key column can contain one NULL value (as NULL is not considered equal to itself in SQL's three-valued logic, thus not violating uniqueness). This is useful for columns like email_address or SSN where uniqueness is required but a value might occasionally be absent.

Trade-offs and Real-world Implications

The choice impacts data integrity, physical storage, and query performance. Only a primary key can be referenced by a foreign key, making it central to relational database design. While both create indexes that add overhead to write operations (inserts, updates, deletes), the PK's role as a clustering key can significantly affect read performance for certain query patterns.

In modern data platforms and distributed systems (e.g., Spark, Delta Lake, Snowflake), the concept of a "primary key" often becomes a logical constraint rather than a physically enforced one. For instance, Delta Lake's transaction log ensures atomicity, but global uniqueness across distributed writes might be enforced at the application layer during ingestion or through batch validation jobs. Similarly, Snowflake's micro-partitions and clustering keys (like Z-ordering) are physical optimizations that can align with a logical PK, but the uniqueness itself might be guaranteed by upstream ETL processes or MERGE statements.

CREATE TABLE Users (
    user_id INT PRIMARY KEY,
    username VARCHAR(50) NOT NULL UNIQUE,
    email VARCHAR(100) UNIQUE, -- Allows one NULL email
    registration_date DATE
);

In the interview, also mention the distinction between logical constraints (for data modeling and application logic) and physical enforcement mechanisms, especially when discussing distributed data systems.

⚡
Pro Tip

Red Flag: Saying 'unique key allows multiple NULLs' (depends on SQL dialect). Pro-Move: Clarify that in data lakes, PK is a logical concept—Delta Lake enforces via merge; enforcement is eventual.

Want all answers as a PDF for offline study?
Seven focused volumes with 750+ in-depth answers — Answer Vault →

Related SQL Questions

mediumWrite an SQL query to find the second-highest salary from an employee table.FreemediumDemonstrate the difference between DENSE_RANK() and RANK()FreemediumDiscuss differences between ROW_NUMBER(), RANK(), and DENSE_RANK(), and provide examples from your projects.FreemediumExplain the differences between Data Warehouse, Data Lake, and Delta LakeFreemediumExplain the differences between Repartition and Coalesce. When would you use each?Free

Level up your prep

Recommended
Educative
Educative Unlimited

800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

Start learning →

Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.

According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 4 companies. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.

← Back to all questionsMore SQL questions →
Categories
All QuestionsSQLSpark / Big DataPython / CodingSystem DesignCloud / ToolsBehavioral
By Company
AmazonGoogleDatabricksSnowflakeAWSAzureMicrosoftNetflixUberTCS
Interview Guides
All GuidesTop SQL QuestionsTop Spark QuestionsPySpark QuestionsTop Python QuestionsTop System DesignKafka QuestionsAirflow QuestionsSQL Window FunctionsETL QuestionsData Modeling
Products
AI Interview CoachAnswer AnalyzerSQL PlaygroundResume AnalyzerAnswer Vault PDFsPricing
Company
About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
© 2026 DataEngPrep.tech. All rights reserved.
AboutBlogContactDisclaimer