Professional Documents
Culture Documents
SQL Query
SQL Query
com
Pages
Home
About Me
Contact Us
Portfolio
SQL Server Interview Questions
Query:
declare @input as table
(
column_name varchar(100)
)
select
[AAA],min([BBB]),min([CCC])
from
@input
pivot
(
max(column_name) for column_name in ([AAA],[BBB],[CCC])
)pv
group by
cube([AAA])
Query:
declare @input as table
(
column_name varchar(100)
)
insert into @input values('AAA')
insert into @input values('BBB')
insert into @input values('CCC')
select
[AAA] FirstColumn,
[BBB] SecondColumn,
[CCC] ThirdColumn
from
@input
pivot
(
max(column_name) for column_name in ([AAA],[BBB],[CCC])
)pv
union
select
null,[AAA],[BBB]
from
@input
pivot
(
max(column_name) for column_name in ([AAA],[BBB],[CCC])
)pv
order by [AAA] desc
Query:
declare @table as table(
Id int,
Name varchar(40),
Salary decimal(18,2),
Age int
)
insert into @table values(1,'asif',12.45,2)
select
variable,value
from
(
select cast(Id as varchar) Id,cast(Name as varchar) as Name,cast(Salary asvarchar) Sa
lary,cast(Age as varchar) Age
from
@table
)t
unpivot(
value FOR variable IN (Id,Name,Salary,Age)
)as unp;
select COALESCE(@var1,@var2,@var3,@var4)Output:
select COALESCE(@var1,@var2,@var3,@var4)
select COALESCE(@var1,@var2,@var3,@var4)
select COALESCE(@var1,@var2,@var3,@var4)
5. There’s a table with below data. Write query for all rank function based on
department_id.
Query:
declare @employee as table(
id int,
department_id int,
name varchar(100),
salary decimal(18,2)
)
select
*,
row_number() over(order by id) rownumber,
dense_rank() over(order by id) denserank,
rank() over(order by id) ranknumber,
ntile(4) over(order by department_id) ntilenumber
from @employee Output:
6. There’s a same table mentioned in above question. Write a query to get 3th and
5th record as below.
Query:
select
*
from
(
select
*,
row_number() over(order by id) rownumber
from @employee
)t
where
t.rownumber = 3 or t.rownumber = 5
OR
How to Include NULL using UNPIVOT.
Below is query using UNPIVOT:
declare @employee as table(
Id int,
Name varchar(40),
Salary decimal(18,2),
Age int,
InsertedBy bigint,
UpdatedBy bigint
)
Select
Id,
ColumnName,
ColumnValue,
UpdatedBy
From
(
Select
Id,
Cast(Name as nvarchar(max)) Name,
Cast(Salary as nvarchar(max)) Salary,
Cast(Age as nvarchar(max)) Age,
isnull(UpdatedBy,InsertedBy) UpdatedBy
From
@employee
)T
Unpivot
(
ColumnValue for ColumnName in (Name,Salary, Age)
) as UNP
Output:
Here, we can see that, it's excluding results for NULL. We will see how can
we achieve it using CROSS JOIN to include NULL column. Below is query for
same.
select
a.ID,
b.column_name,
column_value =
case b.column_name
when 'Name' then a.Name
when 'Salary' then a.Salary
when 'Age' then a.Age
end,
UpdatedBy
from (
Select
Id,
Cast(Name as nvarchar(max)) Name,
Cast(Salary as nvarchar(max)) Salary,
Cast(Age as nvarchar(max)) Age,
isnull(UpdatedBy,InsertedBy) UpdatedBy
From
@employee
)a
cross join (
select 'Name' union all
select 'Salary' union all
select 'Age'
) b (column_name)
select * from @employee E where E.salary > all(select E1.salary from @employeeE1 where
E.id <> E1.id)
select * from @employee E where E.salary < all(select E1.salary from @employee E1 where
E.id <> E1.id)
Query:
declare @product as table(
ProductID int,
Amount int
)insert into @product
values(1,100),
(1,100),
(2,100),
(2,100),
(3,100),
(3,100),
(3,100)
5 comments:
1.
ReplyDelete
Replies
Reply
2.
nice work
ReplyDelete
Replies
Reply
3.
Hello colleagues, how is everything, and what you want to say concerning this post, in my
view its truly
remarkable in favor of me.
ReplyDelete
Replies
Reply
4.
AnonymousAugust 26, 2017 at 11:54 PM
ReplyDelete
Replies
Reply
5.
ReplyDelete
Replies
Reply
Add comment
Load more...
Newer Post Older Post Home
Subscribe to: Post Comments (Atom)
Popular Posts
1. There’s a table with below data Write a query which will give below output. Query: declare @input as table ( ...
Auto Maintain Archive, Purge & Purge on Source Table in SQL Server
We are developing many application where we are auditing, archiving and purging data also for many tables. Most of the app
Below are steps to configure Kerberos Authentication for SQL Server. 1) Open Active Directory 2) Go to the properties of Ser
Get column header list from Excel using Open XML SDK 2.5
There are many applications which are using to insert, update and delete data in database using Excel file. In this type of requ
Change data capture is used to audit insert, update, and delete activity for table in SQL Server table. You can go through belo
Before going ahead, we should be aware about how many ways we can store SSIS packages File system SQL Server (Integr
Error Logging is main part of our database application. I am trying to share some sample code which I am using in my daily lif
The custom tool 'MSLinqToSQLGenerator' failed. Object reference not set to an instance of an object.
Today, I come to this error in Visual studio 2010 when I tried to place procedures in LINQ. I also tried to run Custom tool by rig
Labels
.NET (2)
Algorithm (10)
ASP.NET (4)
Batch File (5)
C# (14)
jQuery (1)
MICROSOFT SQLSERVER (1)
Oracle (5)
SQL Server (26)
SQL Server Interview Question (1)
SQL Server Scenario Based Interview Question (3)
SSIS (3)
Task Scheduler (2)
TFS (1)
Windows Azure SQL Database (1)
Blog Archive
► 2018 (1)
o ► March (1)
► 2016 (10)
o ► March (10)
► 2015 (6)
o ► July (2)
o ► March (2)
o ► February (2)
▼ 2014 (24)
o ► November (4)
o ► June (1)
o ► April (6)
o ▼ March (11)
Schedule Data Backup from Production to Developmen...
Unable to make the session state request to the se...
How to prefix 0 to make same length string for col...
SQL Server Theoretical Interview Question
Script to view Dependencies for Database in SQL Se...
Validation of viewstate MAC failed. If this applic...
The custom tool 'MSLinqToSQLGenerator' failed. Obj...
SQL Server Scenario Based Interview Question
Auto Audit Column Wise Change Data Capture (CDC)
Steps to deploy SSIS package into Integration Serv...
Setup Kerberos Authentication for SQL Server
o ► February (2)
► 2013 (15)
o ► September (1)
o ► August (5)
o ► July (6)
o ► June (3)
► 2012 (3)
o ► October (3)
► 2011 (2)
o ► November (1)
o ► April (1)
► 2010 (6)
o ► November (2)
o ► September (2)
o ► June (2)
► 2009 (1)
o ► December (1)
Follow by Email
Submit
Followers
Total Pageviews