Interview Question of the Week #019 – How to Reset Identity of Table

Here is the question which I received in email:

Question: I have a table which has an identity column. I have recently deleted old and archive it to another place. Now I want to reset the identity of the table to original value of 1. How should I do that?

Answer: It is very easy to do so – you can use the command DBCC CHECKIDENT to do the same.

If a table has to start with an identity of 1 with the next insert then the table should be reseeded with the identity to 0. If identity seed is set below values that currently are in the table, it will violate the uniqueness constraint as soon as the values start to duplicate and will generate error.

DBCC CHECKIDENT (yourtable, reseed, 0)

Here is the quick video I have created to demonstrate the same example.

Reference: Pinal Dave (

Interview Question of the Week #018 – Script to Remove Special Characters – Script to Parse Alpha Numerics

If you ask me – there are certain questions not appropriate for an interview as they are often very subjective. There are some questions, which really do not help to identify if the candidate has the necessary skills or not. Here is one such question, if you ask me in the interview, I may not get it right too, as this requires some experience in writing scripts as well as trial and error mechanics to get it right.

Question: How to write will remove all the special characters and parses Alpha Numeric values only?

Answer: Honestly, it is not that easy to write this script. However, if you are stars are not bright you may end up with an interviewer who believes writing this script is the ultimate test. Do not get me wrong, writing this script is not a bad test, I believe it is not the best question for the interview.

@string VARCHAR(8000)
@IncorrectCharLoc SMALLINT
SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string)
WHILE @IncorrectCharLoc > 0
@string = STUFF(@string, @IncorrectCharLoc, 1, '')
SET @IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', @string)
@string = @string
RETURN @string

You can test above function with the help of following test:

-- Test
SELECT dbo.UDF_ParseAlphaChars('AB"_I+{D[]}4|:e;"5,<.F>/?6')

The above query will return following result set:


You can read about this function and additional comments in my earlier blog post over here: Function to Parse AlphaNumeric Characters from String

Reference: Pinal Dave (

Interview Question of the Week #017 – Performance Comparison of Union vs Union All

Here is what I just learned from email. One of the very prestigious organization asks one of their candidate following questions –

Question: If you have options to use Union or Union All – which one of the option will use keeping performance as a top most requirement for the query.

Answer: Though many of you may be surprised to know that this kind of questions can exist, I am personally not surprised to see this in an interview. Here is my answer – UNION and UNION ALL can’t be compared as they are absolutely different things – they are like apples and oranges. Just like Apples and Oranges are fruits Union and Union All our operators, but they both are there for very different purposes.

The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type. With UNION, only distinct values are selected.

The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.

You can’t compare their performance as they do an absolutely different task.

Here are articles you can read for further understanding this issue.

Performance comparison: SQL SERVER – Difference Between Union vs. Union All – Optimal Performance Comparison

Different between Union and Union All: SQL SERVER – UNION ALL and UNION are Different Operation

Other relevant articles:

Reference: Pinal Dave (

Interview Question of the Week #016 – How to Take Database Offline

Here is the question, I received in the recent conference where I was presenting on Database Technology.

Question: What does it mean by Taking Database Offline and How to do it?

Answer: Taking database offline means, it will be no more available for database operations. Here are is how you can take your database offline.

-- Take the Database Offline

If you have previously taken database offline, you can once again take it online by running the following command:

-- Take the Database Online

You can read more about it in following blogs:

Reference: Pinal Dave (

Interview Question of the Week #014 – How to DELETE Duplicate Rows

Here is another very common question I often received, which candidates face in the interview.

Question: How to delete duplicate rows in SQL Server (considering there is an identity column in table)?

Answer: Well, there is no better answer than following a script. If you ask me this question in the interview, I will even have to look up online and find following script:

FROM MyTable
GROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)

Here is the quick video I have recorded where I demonstrate the same functionality in simple words.

Reference: Pinal Dave (

Interview Question of the Week #013 – Stored Procedure and Its Advantages – How to Create Stored Procedure

There are some questions which are so old that they should not be asked in the interview and gets old. Here is one of the questions I have spotted so many times in the interview that if co-interviewer asks to the candidate, I often feel bored (well, I was caught yawning last time). Here is the question:

Question: “How do you create stored procedure? and What are the advantages of the stored procedure?”

Answer: Well, as I said, I find this question are age old, so if you search on the internet you will find many different answers. Just make sure you read the answers which are from the latest version of SQL Server as stored procedure have changed a bit since the inception of the same.

Here are few of the advantages of the stored procedure:

  • Execution plan retention and reuse
  • Query auto-parameterization
  • Encapsulation of business rules and policies
  • Application modularization
  • Sharing of application logic between applications
  • Access to database objects that is both secure and uniform
  • Consistent, safe data modification
  • Network bandwidth conservation
  • Support for automatic execution at system start-up
  • Enhanced hardware and software capabilities
  • Improved security
  • Reduced development cost and increased reliability
  • Centralized security, administration, and maintenance for common routines

You can read the original article over here.

If you want to create stored procedure you can create with following simple script:


You can execute above stored procedure by following a script:


The above stored procedure will return results as a current date time.

 spcreate Interview Question of the Week #013   Stored Procedure and Its Advantages   How to Create Stored Procedure

Reference: Pinal Dave (

Interview Question of the Week #012 – Steps to Restore Bak File to Database

Here is the question, I was asked the other day at the conference.

Question: If I have a .bak file and I want to restore it database what are the various steps involved in it. Assume that I do not know much about that backup file.

Answer: I love this question, as it has a clear need, but there is ambiguity in the process.

Step 1: Retrieve the logical file name of the database

Step 2: Bring database to single user

Step 3: Restore database with the help of the logical file name retrieved earlier.

Step 4: Bring database to multi user mode.

Here are all the four steps together:

----Retrieve logical file name
FROM DISK = 'D:\BackUpYourBaackUpFile.bak'
----Make Database to single user Mode
----Restore Database
FROM DISK = 'D:\BackUpYourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:\DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:\DataYourLDFFile.ldf'
----Make Database to multi user Mode

Here is the blog post I wrote earlier, which describes the same process SQL SERVER – Restore Database Backup using SQL Script (T-SQL).

Reference: Pinal Dave (

Interview Question of the Week #011 – Script to Convert List to Table and Table to List

Now today’s interview question is really more of daily routine task for many of the developer. 11 years ago, when I went to my very first interview call. My interview was scheduled at 6 PM in one of the very well known corporation. The corporation was known for its first initiatives for social media. When I reached there at 6 PM there were many candidates and we all got the same task to complete. The task was as follows:

Task 1) Write a script to convert a list to a table

Task 2) Write a script to convert the table to a list

We all had two hours of time to complete the task. There were quite a few developers who had completed the task, but I had failed the interview as I was not good with SQL Scripting. After I return from the interview, I had decided to write scripts for my personal learning. I did not want to fail the interviews in the future if I was ever going to get that task.

Here are the answers for both the tasks.

Answer for Task 1: SQL SERVER – Creating Comma Separate Values List from Table – UDF – SP

Answer for Task 2: SQL SERVER – UDF – Function to Convert List to Table

The irony of the life is that I never got the same task again in future. Here is the question back to you – can you guess the name of the organization which was based out of California and one of the early social media giants?

Reference: Pinal Dave (

Interview Question of the Week #010 – What is the Difference Between Primary Key Constraints and Unique Key Constraints?

I often see people getting confused between Primary Key and Unique Key. I see where people get confused about the same. Let us discuss.

Question: “What is the Difference Between Primary Key Constraints and Unique Key Constraints?”

Answer: “Primary Key (PK) Constraints enforce uniqueness just like Unique Key (UK) Constraints, however, PK does not allow  NULL value where as UK allows one NULL value.”

Well, the answer is as simple as above statement. You can read more about this over here: Primary Key Constraints and Unique Key Constraints.

I am very sure after reading above statement there will be few readers, who is going to comment and write email that when we create Primary Key Constraint it also creates clustered index on that column but Unique Key Constraint does not create clustered index. Well, this is the specific (and default) behavior in SQL Server where clustered index is automatically created, however user can create Primary Key Constraint without creating a clustered index as well and you can read about this over here Primary Key and NonClustered Index in Simple Words.

Reference: Pinal Dave (

Interview Question of the Week #009 – Is GO T-SQL Statement?

I often get this question in the email – more frequently than I would like to get.

Question: “Is GO a T-SQL statement?”

Answer: “No, GO is not a T-SQL statement, it is a command which is recognized by SSMS or sqlcmd as a signal to send the current batch to SQL Server Engine.”

Well, here are two articles I recommend to read for more information – MSDN and Explanation SQL Command GO.

Here are few things I would like to add related to the GO statement.

  • If recommend a statement terminator semicolon after each statement, however, do not use semicolon after the GO statement or it will give an error.
  • ODBC or OLE DB API does not recognize the GO statement and it will give a syntax error.
  • You can use GO in a next line of T-SQL Statement. If you provide an integer after GO statement, it will execute that statement multiple times.
    gostatement1 Interview Question of the Week #009   Is GO T SQL Statement?
  • You can only have comments in the same line as a GO statement, any other statement will error.
  • You can change the command GO to any other value in SSMS by going to Tools > Option
    gostatement2 Interview Question of the Week #009   Is GO T SQL Statement?

Let me know your thoughts about the GO statement.

Reference: Pinal Dave (