Click here to Skip to main content
15,888,113 members
Articles / Database Development / SQL Server / SQL Server 2014
Tip/Trick

Tricky SQL Questions

Rate me:
Please Sign up or sign in to vote.
3.55/5 (9 votes)
18 Sep 2014CPOL2 min read 105.6K   19   6
Unexpected SQL Interview Questions

Introduction

This tip presents few SQL Tricky Questions and Answers which will be useful for your development as well as Interviews.

Using the Code

1) How to insert into a table with just one IDENTITY column?

Answer

SQL
INSERT INTO tbl_ID DEFAULT VALUES;

2) How to concatenate multiple rows into a single text string without using loop?

Suppose we have a table with the following data:

ClientID    ClientName
3           Sowrabh Malhotra
4           Saji Mon
6           Sajith Kumar
7           Vipin Job
8           Monoj Kumar

We need to concatenate the ClientName column like the following:

Sowrabh Malhotra, Saji Mon, Sajith Kumar, Vipin Job, Monoj Kumar

Answer

Method 1
SQL
SELECT ClientName + ', ' 
From ClientMaster 
For XML PATH('')
Method 2
SQL
DECLARE @ClientNames VARCHAR(MAX);
SET @ClientNames = '';

SELECT @ClientNames = @ClientNames + IsNull(ClientName + ', ', '')
FROM ClientMaster 
Select @ClientNames

3) How to use ORDER BY in VIEWS

SQL
Create View vClientMaster
As
Select TOP 100 PERCENT ClientID, ClientName FROM ClientMaster Order BY ClientID  DESC 

But in the above example, the SQL server will not consider the [TOP 100 PERCENT] statement when executing the query. So we will not get the result in the order described in the view. But if we specify any number less than 100 PERCENT, SQL server will sort the result.

Note: It is not advisable to use ORDER BY in VIEWS. Use order by outside the view like the following:

SQL
Select ClientID, ClientName FROM vClientMaster Order BY ClientID  DESC 

4) How to create a UNIQUE Key on a Column which is having multiple NULL values?

In SQL Server, Unique Key Constraint will allow only one NULL value. But there are situations that are more than one null value in the column, but still have to maintain Uniqueness by ignoring all null values.

Answer

Method 1

Only works on SQL Server 2008 and above

Using Filter Index. Filtered index is used to Index a portion of rows in a table. While creating an index, we can specify conditional statements. The below SQL Query will create a Unique Index on the rows having non null values:

SQL
CREATE UNIQUE INDEX IX_ClientMaster_ClientCode ON ClientMaster(ClientCode)
WHERE ClientCode IS NOT NULL
Method 2

Create a view having the unique fields and create a Unique Clustered Index on it:

SQL
Create View vClientMaster_forIndex
With SchemaBinding
As
Select ClientCode From dbo.ClientMaster Where ClientCode IS NOT NULL;
Go

CREATE Unique Clustered Index UK_vClientMaster_ForIndex
on vClientMaster_forIndex(ClientCode)
Method 3

Create a Computed Column like the following and create a UNIQUE KEY on that:

SQL
[CMP_ClientCode]  AS (case when [ClientCode] IS NULL _
then CONVERT([varchar](10),[ClientID],0) else [ClientCode] end)

4) What is the default size of SQL Server Database

Answer

The default size of user database is based on system database model. When a user creates a new database, SQL server will take the copy of system model database and add user specified settings on it and create new database.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Soft To Rule Solutions
India India
Working as a Freelance application software developer.

Comments and Discussions

 
PraiseMessage Closed Pin
14-Jul-20 19:27
Navi114-Jul-20 19:27 
PraiseMy Vote of 5 Pin
Member 1357337713-Dec-17 9:50
Member 1357337713-Dec-17 9:50 
QuestionSimple answer for 2) How to concatenate multiple rows into a single text string without using loop? Pin
Member 448934428-Nov-17 0:26
Member 448934428-Nov-17 0:26 
PraiseGood questions. Here's some more for your collection! Pin
Member 1024450311-Apr-16 22:16
Member 1024450311-Apr-16 22:16 
QuestionMy vote of 5 Pin
Andrew-CG23-Apr-15 2:11
Andrew-CG23-Apr-15 2:11 
QuestionDefault DB size Pin
KP Lee20-Sep-14 5:51
KP Lee20-Sep-14 5:51 
QuestionIndexing a computed column Pin
KP Lee20-Sep-14 5:09
KP Lee20-Sep-14 5:09 
QuestionTerrible example - method 2 Pin
KP Lee20-Sep-14 4:24
KP Lee20-Sep-14 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.