Click here to Skip to main content
15,879,239 members
Articles / Database Development / SQL Server
Article

Sql Queries most asked in job interviews.

Rate me:
Please Sign up or sign in to vote.
2.37/5 (29 votes)
26 Apr 2008CPOL2 min read 486.4K   57   22
Tricky Sql Queries most asked in SqlServer/.NET Job Interviews.

INTRODUCTION

SQL queries most asked in .NET/SQL Server job interviews. These tricky queries may be required in your day to day database usage.

BACKGROUND

This article demonstrates some commonly asked SQL queries in a job interview. I will be covering some of the common but tricky queries like:-

(i) Finding the nth highest salary of an employee.
(ii) Finding TOP X records from each group.
(iii) Deleting duplicate rows from a table.

NOTE : All the SQL mentioned in this article has been tested under SQL Server 2005.

(i) Finding the nth highest salary of an employee.

Create a table named Employee_Test and insert some test data as:-
CREATE TABLE Employee_Test
(
Emp_ID INT Identity,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)

INSERT INTO Employee_Test VALUES ('Anees',1000);
INSERT INTO Employee_Test VALUES ('Rick',1200);
INSERT INTO Employee_Test VALUES ('John',1100);
INSERT INTO Employee_Test VALUES ('Stephen',1300);
INSERT INTO Employee_Test VALUES ('Maria',1400);
It is very easy to find the highest salary as:-
--Highest Salary
select max(Emp_Sal) from Employee_Test
Now, if you are asked to find the 3rd highest salary, then the query is as:-
--3rd Highest Salary
select min(Emp_Sal) from Employee_Test where Emp_Sal in
(select distinct top 3 Emp_Sal from Employee_Test order by Emp_Sal desc)
The result is as :- 1200
To find the nth highest salary, replace the top 3 with top n (n being an integer 1,2,3 etc.)
--nth Highest Salary
select min(Emp_Sal) from Employee_Test where Emp_Sal in
(select distinct top n Emp_Sal from Employee_Test order by Emp_Sal desc)

(ii) Finding TOP X records from each group

Create a table named photo_test and insert some test data as :-
create table photo_test
(
pgm_main_Category_id int,
pgm_sub_category_id int,
file_path varchar(MAX)
)

insert into photo_test values
(17,15,'photo/bb1.jpg');     
                                                 
insert into photo_test values(17,16,'photo/cricket1.jpg');                                                    
insert into photo_test values(17,17,'photo/base1.jpg');                                                       
insert into photo_test values(18,18,'photo/forest1.jpg');                                                       
insert into photo_test values(18,19,'photo/tree1.jpg');                                                           
insert into photo_test values(18,20,'photo/flower1.jpg');                                                     
insert into photo_test values(19,21,'photo/laptop1.jpg');                                                       
insert into photo_test values(19,22,'photo/camer1.jpg');                                                 

insert into photo_test values(19,23,'photo/cybermbl1.jpg');                                                    
insert into photo_test values
(17,24,'photo/F1.jpg');
There are three groups of pgm_main_category_id each with a value of 17 (group 17 has four records),18 (group 18 has three records) and 19 (group 19 has three records).
Now, if you want to select top 2 records from each group, the query is as follows:-
select pgm_main_category_id,pgm_sub_category_id,file_path from
(
select pgm_main_category_id,pgm_sub_category_id,file_path,
rank() over (partition by pgm_main_category_id order by pgm_sub_category_id asc) as rankid
from photo_test
) photo_test
where rankid < 3 -- replace 3 by any number 2,3 etc for top2 or top3.
order by pgm_main_category_id,pgm_sub_category_id
The result is as:-
pgm_main_category_id	pgm_sub_category_id	file_path
17	                   15	                   photo/bb1.jpg
17	                   16	                   photo/cricket1.jpg
18	                   18	                   photo/forest1.jpg
18	                   19	                   photo/tree1.jpg
19	                   21	                   photo/laptop1.jpg
19	                   22	                   photocamer1.jpg

(iii) Deleting duplicate rows from a table

A table with a primary key doesn’t contain duplicates. But if due to some reason, the keys have to be disabled or when importing data from other sources, duplicates come up in the table data, it is often needed to get rid of such duplicates.
This can be achieved in tow ways :-
(a) Using a temporary table.
(b) Without using a temporary table.

(a) Using a temporary or staging table

Let the table employee_test1 contain some duplicate data like:-
CREATE TABLE Employee_Test1
(
Emp_ID INT,
Emp_name Varchar(100),
Emp_Sal Decimal (10,2)
)

INSERT INTO Employee_Test1 VALUES (1,'Anees',1000);
INSERT INTO Employee_Test1 VALUES (2,'Rick',1200);
INSERT INTO Employee_Test1 VALUES (3,'John',1100);
INSERT INTO Employee_Test1 VALUES (4,'Stephen',1300);
INSERT INTO Employee_Test1 VALUES (5,'Maria',1400);
INSERT INTO Employee_Test1 VALUES (6,'Tim',1150);
INSERT INTO Employee_Test1 VALUES (6,'Tim',1150);
Step 1: Create a temporary table from the main table as:-
select top 0* into employee_test1_temp from employee_test1
Step2 : Insert the result of the GROUP BY query into the temporary table as:-
insert into employee_test1_temp
select Emp_ID,Emp_name,Emp_Sal
from employee_test1
group by Emp_ID,Emp_name,Emp_Sal
Step3: Truncate the original table as:-
truncate table employee_test1
Step4: Fill the original table with the rows of the temporary table as:-
insert into employee_test1
select * from employee_test1_temp
Now, the duplicate rows from the main table have been removed.
select * from employee_test1
gives the result as:-
Emp_ID	Emp_name   Emp_Sal
1	Anees	   1000
2	Rick	   1200
3	John	   1100
4	Stephen	   1300
5	Maria	   1400
6	Tim	   1150

(b) Without using a temporary table

;with T as
(
	select * , row_number() over (partition by Emp_ID order by Emp_ID) as rank
	from employee_test1
)

delete
from T
where rank > 1
The result is as:-
Emp_ID	Emp_name   Emp_Sal
1	Anees	   1000
2	Rick	   1200
3	John	   1100
4	Stephen	   1300
5	Maria	   1400
6	Tim	   1150

CONCLUSION

I hope that these queries will help you for Interviews as well as in your day database activities.

License

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


Written By
India India
Sudipta Chaudhari is having 14+ yrs of professional full stack software design & development experience with multiple onsite/client site visit experiences.

He has extensive experience working on – REACT, C#, .NET Core, Angular, AZURE, ASP.NET Web API, ASP.NET MVC, WPF, SQL Server, JQuery to name a few. He also has experience as a SCRUM Master.

For latest articles and updates, please visit by website/blog : http://sudiptachaudhari.com

Comments and Discussions

 
PraiseMessage Closed Pin
13-Jul-20 21:16
professionalYeke Jom13-Jul-20 21:16 
SuggestionNice Pin
Novakovi24-Oct-14 1:11
Novakovi24-Oct-14 1:11 
GeneralMy vote of 5 Pin
Member 108269096-Jun-14 21:05
Member 108269096-Jun-14 21:05 
QuestionRefer the Following Link ... for most important SQL Queries in various databases Pin
Rajasekhar Burepalli17-Jun-13 21:29
Rajasekhar Burepalli17-Jun-13 21:29 
QuestionVist this website for Top interview questions on SQL Queries Pin
Techie Jhon17-Jun-13 7:38
Techie Jhon17-Jun-13 7:38 
GeneralMy vote of 5 Pin
Dream8Lee17-Feb-13 6:07
Dream8Lee17-Feb-13 6:07 
QuestionSimple way to get 3rd or 9th highest salary with co-related query........ Pin
prvn7720-Oct-12 6:21
prvn7720-Oct-12 6:21 
AnswerRe: Simple way to get 3rd or 9th highest salary with co-related query........ Pin
Sanjay0409s19-Dec-12 0:26
Sanjay0409s19-Dec-12 0:26 
AnswerRe: Simple way to get 3rd or 9th highest salary with co-related query........ Pin
Dream8Lee17-Feb-13 6:08
Dream8Lee17-Feb-13 6:08 
GeneralMy vote of 4 Pin
areshdeep19-Sep-12 9:58
areshdeep19-Sep-12 9:58 
Question3rd highest salary can be made bit more simpler... Pin
here2learn.net27-Aug-12 4:16
here2learn.net27-Aug-12 4:16 
AnswerRe: 3rd highest salary can be made bit more simpler... Pin
Dream8Lee17-Feb-13 6:09
Dream8Lee17-Feb-13 6:09 
QuestionFeedback Pin
sonam_m16-May-12 1:19
sonam_m16-May-12 1:19 
QuestionThanks Pin
Member 799739418-Aug-11 8:18
Member 799739418-Aug-11 8:18 
GeneralMy vote of 5 Pin
Mishra Ashish12-Feb-11 8:40
Mishra Ashish12-Feb-11 8:40 
GeneralMy vote of 1 Pin
Senthil d KING20-Jan-10 19:17
Senthil d KING20-Jan-10 19:17 
GeneralSimple Queries.... Pin
CARPETBURNER27-Apr-08 8:36
CARPETBURNER27-Apr-08 8:36 
GeneralRe: Simple Queries.... Pin
vinay28may13-May-11 21:32
vinay28may13-May-11 21:32 
GeneralThis is wrong! Pin
SimulationofSai27-Apr-08 8:27
SimulationofSai27-Apr-08 8:27 
GeneralRe: This is wrong! Pin
shellymidha28-Apr-09 8:33
shellymidha28-Apr-09 8:33 
GeneralRe: This is wrong! Pin
Sukh Veer Singh20-Mar-13 21:47
Sukh Veer Singh20-Mar-13 21:47 
GeneralThanks Pin
Colin Angus Mackay27-Apr-08 0:46
Colin Angus Mackay27-Apr-08 0:46 
GeneralRe: Thanks Pin
johnson dmonte25-Jul-10 10:30
johnson dmonte25-Jul-10 10:30 

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.