0% found this document useful (0 votes)
17 views33 pages

SQL Interview Guide

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views33 pages

SQL Interview Guide

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

SQL INTERVIEW

QUESTIONS
SORTING

▸ In order to sort a table, we need to use the ORDER BY Clause


▸ In order to get the results sorted based on certain columns, we need to use this. Usage
of asc or desc has to be defined to have the results in ascending or descending orders.
Default value being asc (ascending)

▸ select field1, field2 from tablename


where <clause>order by field1
▸ select field1, field2 from tablename
where <clause>order by field1 asc
▸ select field1, field2 from tablename
where <clause>order by field1 desc
JOINS
In most of the real world problems, we might need data from multiple tables,
that’s where Joins comes into picture.
List the keywords below in
order of the occurrence in a
query.
1. Group by
2. Order by
3. Select
4. Where
5. From
6. Limit
7. having
Find the first 5 characters from a
column.
LEFT(COLUMN, 5)

Find the 3rd to 6th character of a


column.
SUBSTR() function
Mask a column such that the
last few characters are
converted to *.
CONCAT(LEFT(COLUMN,5),’*****’)

Retrieve name of employees who have


age greater than what Ravi has
NESTED QUERY
Retrieve name of employees
who have age greater than what
Ravi has and store it in a view.
CREATE VIEW AS (QUERY)
JOIN QUESTIONS
There would be multiple join related questions

Let say, you have employee details in employee table, and project details in
project table.
▸ Question: Find the list of employees that are assigned to some projects
▸ Question: Find the total list of employees along with their respective
project names (Employees with no project details should have their project
as null)
FACEBOOK QUESTION
▸ This was asked in one of the facebook interviews for a Sr.Data
Analyst position

Let say you have wifi_id, wifi_speed, wifi_latency,date


▸ Question 1: Find the average wifi speed for eachwifi
▸ Question 2: Find the average wifi speed for eachwifi in the last 2 days
CASE STUDY BASED
INTERVIEW QUESTIONS
Write an SQL query to fetch
“FIRST_NAME” from Worker
table in upper case.

The required query is:


Select upper(FIRST_NAME) from Worker;
Write an SQL query to fetch
unique values of
DEPARTMENT from Worker
table.
The required query is:
Select distinct DEPARTMENT from Worker;
Write an SQL query to print the
first three characters of
FIRST_NAME from Worker
table.
The required query is:
Select substring(FIRST_NAME,1,3) from Worker;
Write an SQL query to find the
position of the alphabet (‘a’) in
the first name column
‘Amitabh’ from Worker table.
The required query is:
Select INSTR(FIRST_NAME, BINARY'a') from
Worker where FIRST_NAME = 'Amitabh';
Write an SQL query that
fetches the unique values of
DEPARTMENT from Worker
table and prints its length.
The required query is:
Select distinct length(DEPARTMENT) from Worker;
Write an SQL query to print
details of the Workers whose
FIRST_NAME contains ‘a’.
The required query is:
Select * from Worker where FIRST_NAME like '%a%';
Write an SQL query to print
details of the Workers whose
FIRST_NAME ends with ‘h’ and
contains six alphabets.
The required query is:
Select * from Worker where FIRST_NAME like '_____h';
Write an SQL query to fetch
the no. of workers for each
department in the descending
order.
The required query is:
SELECT DEPARTMENT, count(WORKER_ID)
No_Of_Workers
FROM worker
GROUP BY DEPARTMENT
ORDER BY No_Of_Workers DESC;
Write an SQL query to print
details of the Workers who are
also Managers.
The required query is:
SELECT DISTINCT W.FIRST_NAME,
T.WORKER_TITLE
FROM Worker W
INNER JOIN Title T
ON W.WORKER_ID = T.WORKER_REF_ID
AND T.WORKER_TITLE in ('Manager');
Write an SQL query to show
only odd rows from a table.
The required query is:
SELECT * FROM Worker WHERE MOD
(WORKER_ID, 2) <> 0;
Write an SQL query to show
only even rows from a table.
The required query is:
SELECT * FROM Worker WHERE MOD
(WORKER_ID, 2) = 0;
Write an SQL query to show
the current date and time.
Following MySQL query returns the current date:
SELECT CURDATE();
Following MySQL query returns the current date
and time:
SELECT NOW();
Following SQL Server query returns the current
date and time:
SELECT getdate();
Following Oracle query returns the current date and
time:
SELECT SYSDATE FROM DUAL
Write an SQL query to show
the top n (say 10) records of a
table.
Following MySQL query will return the top n
records using the LIMIT method:
SELECT * FROM Worker ORDER BY Salary DESC
LIMIT 10;
Following SQL Server query will return the top n
records using the TOP command:
SELECT TOP 10 * FROM Worker ORDER BY
Salary DESC;
Following Oracle query will return the top n records
with the help of ROWNUM:
SELECT * FROM (SELECT * FROM Worker ORDER
BY Salary DESC)
WHERE ROWNUM <= 10;
Write an SQL query to
determine the nth (say n=5)
highest salary from a table.
The following MySQL query returns the nth highest
salary:
SELECT Salary FROM Worker ORDER BY Salary
DESC LIMIT n-1,1;
The following SQL Server query returns the nth
highest salary:
SELECT TOP 1 Salary
FROM (
SELECT DISTINCT TOP n Salary
FROM Worker
ORDER BY Salary DESC
)
ORDER BY Salary ASC;
Write an SQL query to determine the 5th highest
salary without using TOP orlimit method.
The following query is using the correlated subquery to
return the 5th highest salary:
SELECT Salary
FROM Worker W1
WHERE 4 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Use the following generic method to find nth highest
salary without using TOP or limit.
SELECT Salary
FROM Worker W1
WHERE n-1 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Write an SQL query to fetch
the list of employees with the
same salary.
The required query is:
Select distinct W.WORKER_ID, W.FIRST_NAME,
W.Salary
from Worker W, Worker W1
where W.Salary = W1.Salary
and W.WORKER_ID != W1.WORKER_ID;
Write an SQL query to show
the second highest salary from
a table.
The required query is:
Select max(Salary) from Worker
where Salary not in (Select max(Salary) from
Worker);
Write an SQL query to fetch
the first 50% records from a
table.
The required query is:
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT
count(WORKER_ID)/2 from Worker);
Write an SQL query to fetch
the departments that have less
than five people in it.
The required query is:
SELECT DEPARTMENT, COUNT(WORKER_ID) as
'Number of Workers' FROM Worker GROUP BY
DEPARTMENT HAVING COUNT(WORKER_ID) <
5;
Write an SQL query to print the Cname of
employees having the highest salary in each
department.
The required query is:
SELECT t.DEPARTMENT,t.FIRST_NAME,t.Salary
from(SELECT max(Salary) as
TotalSalary,DEPARTMENT from Worker group by
DEPARTMENT) as TempNew
Inner Join Worker t on
TempNew.DEPARTMENT=t.DEPARTMENT
and TempNew.TotalSalary=t.Salary;
Thank You !!

You might also like