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…
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.
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.
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.
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.
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.
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.
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.
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.