Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

SQL Server Tricky Query Interview Question

$
0
0

Introduction

Let's write some Sql Server Queries to get the results from a table in a database. I am writing some queries which were asked of me during my interview -- the questions asked in each interview are different but I understand they are related to each other. Every time the person who is doing the interview asks you to do this or that, if things are clear to you, you can easily do what the interviewer asks you to do.

Description

Suppose you have a table like the below image and have the data like this table has,


SQL Server Tricky Query Interview Question

By mistake I wrote the column name wrong (Department), so we will carry on with this, because we just have to clear the concept, so we can ignore the spelling here. Now we have a table with the data, let's play with it.

Let's create different case requirements from this table, for example,

Get the name of employee who having has the highest salary in each department,

selectt.Departmnet,t.name,t.Salaryfrom(selectmax(Salary)asTotalSalary,Departmnetfrom#TempgroupbyDepartmnet)asTempNew InnerJoin#TemptonTempNew.Departmnet=t.Departmnet andTempNew.TotalSalary=t.Salary

You will get the output like this,


SQL Server Tricky Query Interview Question

So we have the output we want, now let's do this some other way, the easy way with the CTE.

withcteas ( selectname,departmnet,salary,ROW_NUMBER()over(PARTITIONBYdepartmnetorderbysalarydesc)asRowNumfrom#Temp ) selectDepartmnet,Name,SalaryfromctewhereRowNum=1orderbyDepartmnetdesc We will get the same output -- the output in the above image with this query too.

Get the Total Salary of each Department.

selectsum(Salary)asTotalSalary,Departmnetfrom#TempgroupbyDepartmnet With this query you will get the output like this-:


SQL Server Tricky Query Interview Question

So this is how we can get the total salary of each department.

Get the 2nd highest salary or nth salary in each department.

withcteas ( selectname,departmnet,salary,ROW_NUMBER()over(PARTITIONBYdepartmnetorderbysalarydesc)asRowNumfrom#Temp ) selectDepartmnet,Name,SalaryfromctewhereRowNum=2orderbyDepartmnetdesc

In the above sql query we write nothing new, just change the Rownum=2, because of (Partition by Department) it will give the row number separately to each department starting from 1, so we can get nth salary in each department. Output will be like this,


SQL Server Tricky Query Interview Question

In the above image you can see the second highest salary of each department.

Now get the third highest salary from the table.

withcteas ( selectname,departmnet,salary,ROW_NUMBER()over(orderbysalarydesc)asRowNumfrom#Temp ) selectDepartmnet,Name,SalaryfromctewhereRowNum=3orderbyDepartmnetdesc
SQL Server Tricky Query Interview Question
Now if we see the above query you learn that we removed the (Partition by Department), so now we will get row number for whole table not only for Department, and we have to get the third highest salary so we will just have to give RowNum=3, and that's it, we are ready to rock ;).

I know there is another way to to do this, but in the end what matter is output, I shared this with you people because there are so many who are just starting in this, so this might be helpful for those. H appy coding!


Viewing all articles
Browse latest Browse all 3160

Trending Articles