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.
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,

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.
Suppose you have a table like the below image and have the data like this table has,

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,
- select t.Departmnet,t.name,t.Salary from(select max(Salary) as TotalSalary,Departmnet from #Temp group by Departmnet) as TempNew
- Inner Join #Temp t on TempNew.Departmnet=t.Departmnet
- and TempNew.TotalSalary=t.Salary

So we have the output we want, now let's do this some other way, the easy way with the CTE.
- with cte as
- (
- select name,departmnet,salary,ROW_NUMBER() over(PARTITION BY departmnet order by salary desc) as RowNum from #Temp
- )
- select Departmnet,Name,Salary from cte where RowNum=1 order by Departmnet desc
Get the Total Salary of each Department.
- select sum(Salary) as TotalSalary,Departmnet from #Temp group by Departmnet

So this is how we can get the total salary of each department.
Get the 2nd highest salary or nth salary in each department.
- with cte as
- (
- select name,departmnet,salary,ROW_NUMBER() over(PARTITION BY departmnet order by salary desc) as RowNum from #Temp
- )
- select Departmnet,Name,Salary from cte where RowNum=2 order by Departmnet desc

In the above image you can see the second highest salary of each department.
Now get the third highest salary from the table.
- with cte as
- (
- select name,departmnet,salary,ROW_NUMBER() over(order by salary desc) as RowNum from #Temp
- )
- select Departmnet,Name,Salary from cte where RowNum=3 order by Departmnet desc

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. Happy coding!

Bharat SinghPosted Apr 2, 2019, 8:38 AM
Nice Article. Easy to understand. Thanks a lot Sir.
Vaibhav AgarwalPosted Mar 16, 2018, 10:07 AM
In your articles please also generate SQL Insert Sample Data scripts so that we can straight away start querying. Thanks .
Mayur GujrathiPosted Aug 17, 2017, 4:24 AM
Where #Temp is in query
Humayun Kabir MamunPosted Aug 29, 2016, 12:02 AM
Nice, some more tricky query will be better...
Anu VPosted Aug 21, 2016, 11:53 PM
Nice
SubashPosted Aug 12, 2016, 1:09 AM
Nice
Manas MohapatraPosted Aug 11, 2016, 12:15 AM
If you could have added more questions then it would be better
Munesh SharmaPosted Aug 1, 2016, 12:34 PM
Nice
Harinder KumarPosted Jul 31, 2016, 3:49 AM
Very helpful queries !! Thank u so much sir
Vignesh ManiPosted Jul 30, 2016, 8:08 PM
Nice