Latest SQL scenario based Interview Questions and Answers

12
25296

Hello visitor, Are you preparing for an Interview and wants to hit scenario based SQL Interview questions? You are at the right ground.

” In your IT life, Interviews are like doing Toothbrush each day “  by Chander Sharma.

Agree? Like you choose best toothbrush for your teeth, similarly you need best material to prepare for your Interviews. With this post, I will try to walk you through some scenario based SQL Interview Questions and Answers. One more point i would like to add in here is – Like we have different brands of toothbrush in market, similarly to achieve one output there are many possible ways in SQL. All suggestions and answers are welcome below as Comments !!!

Scenario based SQL Interview Questions and Answers

Question #1 –  How will you identify duplicate records in below SQL table?

How will you identify duplicate records in below SQL table?
How will you identify duplicate records in a SQL table?

Answer #1 –  Solution to identify duplicate records in a SQL table

Solution to identify duplicate records in a SQL table
Solution to identify duplicate records in a SQL table

SQL scenario based Interview QuestionTry SQL code Yourself
Question –
DECLARE @Employee as table
(
Eidint,
Enamevarchar(50)
)
insert into @Employee values
(1,’john’),
(2,’james’),
(2,’james’),
(3,’krish’),
(4,’robert’),
(4,’robert’),
(5,’clave’)
Select * from @Employee
Answer –
SELECT Eid, Ename, COUNT(Eid) AS NumOccurrences
FROM @Employee
GROUP BY Eid,Ename
HAVING (COUNT(Eid)>1)

Question #2 –  How to get distinct values from a SQL table without using DISTINCT keyword?

How to get distinct values from a SQL table without using DISTINCT keyword?
How to get distinct values from a SQL table without using DISTINCT keyword?

Answer #2 –  Solution to get distinct values from a SQL table without using DISTINCT keyword

Solution to get distinct values from a SQL table without using DISTINCT keyword
Solution to get distinct values from a SQL table without using DISTINCT keyword

SQL scenario based Interview Question – Try SQL code Yourself
Answer –
selecteid,ename from emp
group by eid,ename

Question #3 –  How to get alternate rows from a SQL table?

How to get alternate rows from a SQL table?
How to get alternate rows from a SQL table?

Answer #3 A –  1st Solution to get  alternate rows from a table in SQL – using Row Number

1st Solution to get alternate rows from a SQL table - using Row Number
1st Solution to get alternate rows from a SQL table – using Row Number

SQL scenario based Interview Question – Try SQL code Yourself
Answer –
select row_number() over(order by eid)as rowid,* into #temptab FROM dbo.Emp
select * from #temptab where (rowid%2)<>0

Answer #3 B –  2nd Solution to get  alternate rows from a table in SQL – without using Row Number

2nd Solution to get  alternate rows from a SQL table - without using Row Number
2nd Solution to get alternate rows from a SQL table – without using Row Number

SQL scenario based Interview Question – Try SQL code Yourself
Answer –
select identity(int,1,1)as rowid,* into #Temptab from dbo.Emp
select * from #Temptab where (rowid%2)<>0

VN:F [1.9.22_1171]
Your Feedback hels to Improve
Rating: 9.1/10 (94 votes cast)
VN:F [1.9.22_1171]
Rating: +47 (from 51 votes)
Latest SQL scenario based Interview Questions and Answers, 9.1 out of 10 based on 94 ratings
  • Great job venkat!

    Very useful article

    Thanks,
    Avi

  • Venkat

    Thanks a lot Avinash 🙂

  • Hi Venkat,
    Great start bro !!! Really interesting SQL Interview questions and Scenarios.
    Interview questions always help you in clearing your concept more. Keep posting 🙂
    Thanks,
    Chander Sharma

  • kumar

    Hi,
    Really great job.
    Send me sql queries related to realtime.
    Send me to mail id

  • Praveen

    Hi,

    Can you please tell me what is #temptab in the Below query.
    Is it some temporary table we need to create to do this???

    select row_number() over(order by eid)as rowid,* into #temptab FROM dbo.Emp
    select * from #temptab where (rowid%2)0

  • softinx

    Nice interview questions.
    Keep posting and learn more.

  • sholak

    You have posted Great real interview questions.

  • ItPro

    This post is more than an year old, and you call them latest. come’on buddy… Update it and make relavent..
    Thanks

  • Md Rafi

    Great job Venkat !!! Really interesting SQL Interview questions and Scenarios.

  • Bibhuti

    Hi, Friends
    Nice job Please add more Scenario

  • Giribabu K

    hi bro…excellent job and interesting..

  • prasanthreddy

    HI VENKAT,
    PLEASE SEND ME THE REAL TIME SCENARIOS.

    SQLSERVVER,SSIS AND SSRS –REAL -SCENARIO BASED QUESTIONS
    INETRVIEW QUESTIONS — SQLSERVER,SSIS,SSRS