SQL Interview Questions and Answers
This is a very interesting question. I really do not know why interviews are fascinated with this question. One of the professional services I offer is to help organizations select an ideal employee for their organization and last week, I was on a conference call with a large MNC who was looking for performance tuning expert for their organization. We interviewed around 50 different employees and offered jobs to 2 experts and 1 has so far accepted it. One of the questions which I see my friends from the interview panel asking again and again was about Primary Key. I noticed that most of the candidategot it correct.
Question: Can we have NULL Value in the Primary Key Column?
Answer: No. We can’t have a Primary Key column with a NULLvalue.
The reason for the same is very simple, primary key purpose is to uniquely identify records. If two records of a single column have a NULL value, the column values are not considered equal. In simple words two NULL values are not considered as equal. This is the reason, Primary Key can’t have NULL values as they are not compared with any other value.
Here is a simple script if you execute, it will throw an error because Primary Key can’t have NULL value.
-- Following script will give ERRORCREATE TABLE TestTable([ID] [int] NULL,[Col1] [nvarchar](60) NOT NULL CONSTRAINT [PK_TestTable] PRIMARY KEY CLUSTERED ([ID] ASC))GO
Here is the output, which we receive when we execute above query.
Well, that’s it for today. Do let me know if you have any other interview question, which other users might be interested to read.
Reference: Pinal Dave (https://blog.sqlauthority.com)
SQL Constraint and Keys, SQL NULL, SQL Scripts, SQL Server
SQL SERVER – What is Logical Read?
SQL SERVER – Find Automatically Created Statistics – T-SQL
SQL SERVER – How to Enable Lock Pages in Memory?
8 Comments. Leave new
June 12, 2016 9:10 am
What about unique key. How unique key maintain only one null value.Reply
June 12, 2016 9:12 am
What about unique key. How it maintains only one null values.Reply
June 13, 2016 4:47 pmReply
Pinal, “If two records of a single column have a NULL value, the column values are not considered equal”, then why GROUP BY considers NULLS as same when more than 1 record have nulls and groups by as same. Same case with distinct.
Purpose of Primary Key cannot have Nulls : It should contain a valid atomic value which uniquely identifies record in the table, which can be indexed and further be linked to other tables using Foreign Keys. Nulls are UNKNOWN and unknowns cannot be referred nor is a candidate value and so Primary Key cannot have nulls.
some examples :
create table test_nulls (id int identity(1,1),
insert into test_nulls(col_value)
values(null), (‘NULL’), (‘some value’), (null),(’10’), (null),(‘text’)
select * from test_nulls
select col_value, count(1) from test_nulls
group by col_value
June 16, 2016 10:08 amReply
Purpose of Primary Key cannot have Nulls : It should contain a valid atomic value which uniquely identifies record in the table, “which can be indexed and further be linked to other tables using Foreign Key. Nulls are UNKNOWN and unknowns cannot be referred …”
Not agreed with this statement.
To reference any field as foreign key, its not necessary that it should be primary key. Unique key can also referred as foreign key. Unique key can contain NULL values and so Foreign key can contain NULL values too.
July 18, 2016 7:11 pm
Yes its true that Unique Key can also be referred as foreign keys, Thanks for letting me know this !! But you cannot make Unique keys as potential candidate unless imposing NOT NULL. And its of no value to represent NULL as a candidate value and so Primary Keys cannot have NULLs. On the other hand, attributes with Primary Keys when referred as foreign keys in child tables can have NULLs for which parent value is unknown.Reply
August 31, 2016 12:25 amReply
Hi Pinal.As far as I Know.Primary key is a combination of NOt NULL and Unique .Then according to that concept it will not accept null values.
In concern with UNIQUE constraint it will only accept 1 NULL value
August 31, 2016 9:22 pmReply
A primary key is not only a unique identifier but also a value that can satisfy a predicate such as WHERE = , used to look up a unique row (assuming a simple PK). A NULL cannot directly substitute for a non-NULL value in that role, even if it is unique, because either a different predicate would be required depending on whether or not the key value was NULL or else a non-SARGable expression would have to be used in place of the PK column name in the predicate. NULL is just not on an equal footing with non-NULL values when used to uniquely identify rows.
So maybe it is a useful interview question after all?
August 20, 2021 2:22 pm
I’ve never been able to understand why a SQL NULL doesn’t behave logically, like null in other programming languages. NULL is the absence of a value. There is nothing ‘unknown’ about it. And, as others have stated, SQL is terribly inconsistent with it, with a UNIQUE constraint only allowing a single NULL value, despite it being unequal to NULL, and therefore, according to the rules, never qualifying for uniqueness.Reply
Leave a ReplyCancel reply
Pinal Daveis an SQL Server Performance Tuning Expert and independent consultant with over 17 years of hands-on experience. He holds a Masters of Science degree and numerous database certifications.
Pinal has authored 13 SQL Server database books and 45 Pluralsight courses. To freely share his knowledge and help others build their expertise, Pinal has also written more than 5,500 database tech articles on his blog at https://blog.sqlauthority.com.
Pinal is an experienced and dedicated professional with a deep commitment to flawless customer service. If you need help with any SQL Server Performance Tuning Issues, please feel free to reach out at firstname.lastname@example.org.
Pinal is also a CrossFit Level 1 Trainer (CF-L1) and CrossFit Level 2 Trainer (CF-L2).
Nupur Dave is a social media enthusiast and an independent consultant. She primarily focuses on the database domain, helping clients build short and long term multi-channel campaigns to drive leads for their sales pipeline.
Is your SQL Server running slow and you want to speed it up without sharing server credentials? In my Comprehensive Database Performance Health Check, we can work together remotely and resolve your biggest performance troublemakers in less than 4 hours.
Once you learn my business secrets, you will fix the majority of problems in the future.
Have you ever opened any PowerPoint deck when you face SQL Server Performance Tuning emergencies? SQL Server Performance Tuning Practical Workshop is my MOST popular training with no PowerPoint presentations and 100% practical demonstrations.
Essentially I share my business secrets to optimize SQL Server performance.