Most Asked SQL Queries In Interviews
Most Asked SQL Queries In Interviews
26 40 280.1k
This article is an attempt to answer the most asked essential queries in SQL Server technical interviews, based on my own
experience. No theoretical questions are included; this article is meant to be helpful in technical coding interviews.
Sample tables are listed to visualize the data and associate with query answers given.
01. ==================
02. Consider below tables
03. ==================
01. EMPLOYEE
02. empid empname managerid deptid salary DOB
03. 1 emp 1 0 1 6000 1982-08-06 00:00:00.000
04. 2 emp 2 0 5 6000 1982-07-11 00:00:00.000
05. 3 emp 3 1 1 2000 1983-11-21 00:00:00.000
06. 13 emp 13 2 5 2000 1984-03-09 00:00:00.000
07. 11 emp 11 2 1 2000 1989-07-23 00:00:00.000
08. 9 emp 9 1 5 3000 1990-09-11 00:00:00.000
09. 8 emp 8 3 1 3500 1990-05-15 00:00:00.000
10. 7 emp 7 2 5 NULL NULL
11. 3 emp 3 1 1 2000 1983-11-21 00:00:00.000
12.
13. --DEPARTMENT TABLE
14. deptid deptname
15. 1 IT
16. 2 Admin
1. Employee and Manager ID are in the same table; can you get manager names
for employees?
Answer:
2. Can you get employee details whose department id is not valid or department id
not present in department table?
Answer
Identifying Department IDs in employee table, which are not available in master.
Using NOT IN
Note
"Not In" is the least recommended, considering performance. Outer join and Not Exists are preferred.
if you want to list Department IDs only. INTERSECT and EXCEPT keywords have rules
Use auto increment primary key "add" if not available in the table, as in given example.
Now, perform query on min of auto pk id, group by duplicate check columns - this will give youlatest duplicate records
Now, delete.
7. Now, can you find 3rd, 5th or 6th i.e. N'th highest Salary?
Answer
8. Can you write a query to find employees with age greater than 30?
Answer
9. Write an SQL Query to print the name of the distinct employees whose DOB is
between 01/01/1960 to 31/12/1987
Answer
Answer
The following query will not fetch record with the salary of 6000 but also will skip the record with NULL.
As per SQL Server logic, it works on 3 values in matching conditions. TRUE or FALSE and UNKNOWN. Here, NULL
implies UNKNOWN.
to fix this:
12. Can you show one row twice in results from a table?
Answer
13. Could you tell the output or result of the following SQL statements?
Answer
#1
-- Alternative - ROWCOUNT function
NO error.
16. Can you write a query to get employee names starting with a vowel?
Answer
01. Select empid, empname from employee where empname like '[aeiou]%'
17. Can you write a query to get employee names ending with a vowel?
Answer
01. Select empid, empname from employee where empname like '%[aeiou]'
18. Can you write a query to get employee names starting and ending with a
vowel?
Answer
01. select empid, empname from employee where empname like '[aeiou]%[aeiou]'
21. How can you get random employee record from the table?
Answer
01. select top 1 * from employee order by newid()
22.(Tricky) Below is the table data which has 1 columns and 7 rows
01. Table -TESTONE
02.
03. DATACOL
04. 10/12
05. 1a/09
06. 20/14
07. 20/1c
08. 3112
09. 11/16
10. mm/pp
Give data in a table is of format 'NN/NN', verify that the first and last two characters are numbers and that the middle
character is '/'.
Answer
This can be done using like operator and expression. Checking numbers and not characters.
Answer- 0 .
24. If all values from tbl2 are deleted. What will be the output of the following
query?
Answer
01. DECLARE
02.
03. @i INT,
04. @a INT,
05. @count INT,
06. @result varchar(Max)
07.
08. SET @i = 1
09. set @result=''
10.
11. WHILE (@i <= 100)
12. BEGIN
13. SET @count = 0
14. SET @a = 1
15.
16. -- logic to check prime number
17. WHILE (@a <= @i)
18. BEGIN
19. IF (@i % @a = 0)
20. SET @count = @count + 1
21.
22. SET @a = @a + 1
23. END
24.
25. IF (@count = 2)
26. set @result = @result+cast(@i as varchar(10))+' , '
27.
28. SET @i = @i + 1
29. END
30.
31. set @result = (select substring(@result, 1, (len(@result) - 1)))
32. print(@result)
26. Write query to print numbers from 1 to 100 without using loops
Answer
This can be done using Common Table Expression without using a loop.
01. Options -
02. 1. TD,
03. 2. Syntax Error,
04. 3. select TD
05.
06. Answer - Syntax Error. (Incorrect syntax near the keyword 'select'. )
07.
29. What will be the outputs in the following SQL queries with aggregate
functions?
01. SELECT SUM (1+4*5)
02.
03. Options - a.21, b.25, c.Error d.10
04.
05. Answer -: 21
06.
07.
08. SELECT MAX (1,3,8)
09.
10. Options - a.8, b. 12, c.Error d.1
11.
12. Answer -: Error. Max function takes only 1 argument.
13.
14.
15. SELECT Max ('TD')
16.
17. Options - a.TD b. Error c. 1 d.0
18.
19. Answer-: TD
20.
21.
22. SELECT Max ('TD'+'AD')
23.
24. Options - a.TDAD b. Error c. T2D d.0
25. Answer-: TDAD
Answer = Error. Operand data type NULL is invalid for MAX operator.
Answer = Error. Operand data type NULL is invalid for Avg operator.
Note
MIN, MAX,SUM,AVG none of these function takes NULL parameter/argument. Also, these functions accept only one
argument.
34. Will the following statements execute? if yes what will be output?
SELECT NULL+1
SELECT NULL+'1'
Answer - Yes, no error. The output will be NULL. Perform any operation on NULL will get the NULL result.
Tushar Dikshit
Building software applications for 12 years. Microsoft Certified in Architecting AZURE solutions. (#70534). Certified Scrum Master
leading agile projects.
703 295.5k
26 40
Type your comment here and press Enter Key (Minimum 10 characters)
Thanks for the tip, I have a great book as well, regarding the SQL coding interviews, Matthew Urban "TOP 30 SQL Interview
Coding Tasks", I bought mine on Amazon in kindle version, they also have paperback. I think it's the best when wanting to quickly
check what may be asked during the interview. https://www.amazon.com/TOP-SQL-Interview-Coding-Tasks-
ebook/dp/B07GC5RS3K
Br Conor Apr 09, 2019
1776 2 0 0 0 Reply
Thanks a lot for the qThanks a lot for the questions, really helpful for interviews and in real-time too.uestions, really helpful for
interviews and in real-time too.
mhamd abd Dec 16, 2018
1595 183 261 1 1 Reply
Thank you!
Tushar Dikshit Mar 01, 2019
703 1.7k 295.5k 0
Very helpful
Ravindar Kumar Oct 22, 2018
1720 58 0 1 1 Reply
Thanks a lot for the questions, really helpful for interviews and in real-time too.
Sanjay Singh Chouhan Sep 06, 2018
1670 108 0 2 1 Reply
sas fds For Question 7 we can write like select * from employee where salary not in (select top 2 * from employee order by salary asc)
sas fds Aug 30, 2018
1774 4 0 2 1 Reply
You can use Top clause and IN clause, also max,min functions. It is previous approach for such queries, specially
second highest, but in case you want to formulate for Nth then above approach is good.
Tushar Dikshit Sep 04, 2018
703 1.7k 295.5k 0
sas fds Will SELF JOIN wont work for your Question 1
sas fds Aug 30, 2018
1774 4 0 0 3 Reply
sas fds Select empid,empname,manageridfrom employee e,employee e1 where e.empid=e1.empid and e.managerid>0
sas fds Aug 30, 2018
1774 4 0 0
I'm just commenting alternate queries. --To find second highest salary SELECT MIN (SALARY) FROM EMPLOYEE WHERE
SALARY IN (SELECT DISTINCT TOP 2 salary FROM employee ORDER BY SALARY DESC) ---To find third highest salary
SELECT TOP 1 SALARY FROM EMPLOYEE WHERE SALARY IN (SELECT DISTINCT TOP 3 salary FROM employee ORDER
BY SALARY DESC) ORDER BY SALARY ASC --To find second highest salary using CTE WITH SecondHighestSalaryCTE AS
(SELECT DISTINCT TOP 2 SALARY FROM employee ORDER BY salary DESC) SELECT MIN(SALARY) AS [Second Highest
Salary] FROM SecondHighestSalaryCTE
Ashok Chawdry Aug 13, 2018
1758 20 0 3 0 Reply
Q1 Employee and Manager ID are in the same table; can you get manager names for employees? Answer: Self Join select
e1.empname as EmployeeName,e2.empname as ManagerName from employee e1 join employee e2 on e1.managerid = e2.empid
order by e1.empid
Ashok Chawdry Aug 10, 2018
1758 20 0 1 0 Reply
About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ Partners C# Tutorials
©2019 C# Corner. All contents are copyright of their authors.