0% found this document useful (0 votes)
13 views

SQL interview questions with answers

The document provides a comprehensive guide for learning SQL, including sample data for practice and a series of 50 SQL query questions with their respective answers. It covers various SQL operations such as data retrieval, filtering, and manipulation using a sample 'Worker' table and related tables for bonuses and titles. Additionally, it includes SQL scripts to create and populate the sample tables for practical application.

Uploaded by

Vivek Pathade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

SQL interview questions with answers

The document provides a comprehensive guide for learning SQL, including sample data for practice and a series of 50 SQL query questions with their respective answers. It covers various SQL operations such as data retrieval, filtering, and manipulation using a sample 'Worker' table and related tables for bonuses and titles. Additionally, it includes SQL scripts to create and populate the sample tables for practical application.

Uploaded by

Vivek Pathade
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Most of these SQL query questions are filtered out of interviews held

by top IT MNCs like Flipkart and Amazon.

Let’s Begin Learning SQL!

Prepare Sample Data To Practice SQL Skills.

Sample Table – Worker


WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT
001 Monika Arora 100000 2014-02-20 09:00:00 HR
002 Niharika Verma 80000 2014-06-11 09:00:00 Admin
003 Vishal Singhal 300000 2014-02-20 09:00:00 HR
004 Amitabh Singh 500000 2014-02-20 09:00:00 Admin
005 Vivek Bhati 500000 2014-06-11 09:00:00 Admin
006 Vipul Diwan 200000 2014-06-11 09:00:00 Account
007 Satish Kumar 75000 2014-01-20 09:00:00 Account
008 Geetika Chauhan 90000 2014-04-11 09:00:00 Admin
Sample Table – Bonus
WORKER_REF BONUS_DATE BONUS_AMOU
_ID NT
1 2016-02-20 00:00:00 5000
2 2016-06-11 00:00:00 3000
3 2016-02-20 00:00:00 4000
1 2016-02-20 00:00:00 4500
2 2016-06-11 00:00:00 3500
Sample Table – Title
WORKER_REF WORKER_TITLE AFFECTED_FROM
_ID
1 Manager 2016-02-20 00:00:00
2 Executive 2016-06-11 00:00:00
8 Executive 2016-06-11 00:00:00
5 Manager 2016-06-11 00:00:00
4 Asst. Manager 2016-06-11 00:00:00
7 Executive 2016-06-11 00:00:00
6 Lead 2016-06-11 00:00:00
3 Lead 2016-06-11 00:00:00
To prepare the sample data, you can run the following queries in
your database query executor or on the SQL command line. We’ve
tested them with MySQL Server 5.7 and MySQL Workbench 6.3.8
query browser.
SQL Script to Seed Sample Data.

CREATE DATABASE ORG;

USE ORG;

CREATE TABLE Worker (

WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

FIRST_NAME CHAR(25),

LAST_NAME CHAR(25),

SALARY INT(15),

JOINING_DATE DATETIME,

DEPARTMENT CHAR(25)

);

INSERT INTO Worker

(WORKER_ID, FIRST_NAME, LAST_NAME, SALARY, JOINING_DATE,


DEPARTMENT) VALUES

(001, 'Monika', 'Arora', 100000, '14-02-20


09.00.00', 'HR'),

(002, 'Niharika', 'Verma', 80000, '14-06-11


09.00.00', 'Admin'),

(003, 'Vishal', 'Singhal', 300000, '14-02-20


09.00.00', 'HR'),

(004, 'Amitabh', 'Singh', 500000, '14-02-20


09.00.00', 'Admin'),

(005, 'Vivek', 'Bhati', 500000, '14-06-11


09.00.00', 'Admin'),

(006, 'Vipul', 'Diwan', 200000, '14-06-11


09.00.00', 'Account'),
(007, 'Satish', 'Kumar', 75000, '14-01-20
09.00.00', 'Account'),

(008, 'Geetika', 'Chauhan', 90000, '14-04-11


09.00.00', 'Admin');

CREATE TABLE Bonus (

WORKER_REF_ID INT,

BONUS_AMOUNT INT(10),

BONUS_DATE DATETIME,

FOREIGN KEY (WORKER_REF_ID)

REFERENCES Worker(WORKER_ID)

ON DELETE CASCADE

);

INSERT INTO Bonus

(WORKER_REF_ID, BONUS_AMOUNT, BONUS_DATE) VALUES

(001, 5000, '16-02-20'),

(002, 3000, '16-06-11'),

(003, 4000, '16-02-20'),

(001, 4500, '16-02-20'),

(002, 3500, '16-06-11');

CREATE TABLE Title (

WORKER_REF_ID INT,

WORKER_TITLE CHAR(25),

AFFECTED_FROM DATETIME,
FOREIGN KEY (WORKER_REF_ID)

REFERENCES Worker(WORKER_ID)

ON DELETE CASCADE

);

INSERT INTO Title

(WORKER_REF_ID, WORKER_TITLE, AFFECTED_FROM) VALUES

(001, 'Manager', '2016-02-20 00:00:00'),

(002, 'Executive', '2016-06-11 00:00:00'),

(008, 'Executive', '2016-06-11 00:00:00'),

(005, 'Manager', '2016-06-11 00:00:00'),

(004, 'Asst. Manager', '2016-06-11 00:00:00'),

(007, 'Executive', '2016-06-11 00:00:00'),

(006, 'Lead', '2016-06-11 00:00:00'),

(003, 'Lead', '2016-06-11 00:00:00');

Practice with 50 challenging SQL query questions.

Q-1. Write an SQL query to fetch “FIRST_NAME” from the


Worker table using the alias name <WORKER_NAME>.

Ans.
The required query is:

Select FIRST_NAME AS WORKER_NAME from Worker;

Copy
Q-2. Write an SQL query to fetch “FIRST_NAME” from the
Worker table in upper case.

Ans.
The required query is:

Select upper(FIRST_NAME) from Worker;

Copy
Q-3. Write an SQL query to fetch unique values of
DEPARTMENT from the Worker table.

Ans.
The required query is:

Select distinct DEPARTMENT from Worker;

Copy
Q-4. Write an SQL query to print the first three characters of
FIRST_NAME from the Worker table.

Ans.
The required query is:

Select substring(FIRST_NAME,1,3) from Worker;

Copy
Q-5. Write an SQL query to find the position of the alphabet
(‘a’) in the first name column ‘Amitabh’ from the Worker
table.

Ans.
The required query is:

Select INSTR(FIRST_NAME, BINARY'a') from Worker where


FIRST_NAME = 'Amitabh';

Copy
Notes.
 The INSTR does a case-insensitive search.


 Using the BINARY operator will make INSTR work as the case-
sensitive function.
Q-6. Write an SQL query to print the FIRST_NAME from the
Worker table after removing white spaces from the right
side.
Ans.
The required query is:

Select RTRIM(FIRST_NAME) from Worker;

Copy
Q-7. Write an SQL query to print the DEPARTMENT from the
Worker table after removing white spaces from the left side.

Ans.
The required query is:

Select LTRIM(DEPARTMENT) from Worker;

Copy
Q-8. Write an SQL query that fetches the unique values of
DEPARTMENT from the Worker table and prints its length.

Ans.
The required query is:

Select distinct length(DEPARTMENT) from Worker;

Copy
Q-9. Write an SQL query to print the FIRST_NAME from the
Worker table after replacing ‘a’ with ‘A’.

Ans.
The required query is:

Select REPLACE(FIRST_NAME,'a','A') from Worker;

Copy
Q-10. Write an SQL query to print the FIRST_NAME and
LAST_NAME from the Worker table into a single column
COMPLETE_NAME. A space char should separate them.

Ans.
The required query is:

Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME'


from Worker;

Copy
Q-11. Write an SQL query to print all Worker details from the
Worker table order by FIRST_NAME Ascending.

Ans.
The required query is:

Select * from Worker order by FIRST_NAME asc;

Copy
Q-12. Write an SQL query to print all Worker details from the
Worker table order by FIRST_NAME Ascending and
DEPARTMENT Descending.

Ans.
The required query is:

Select * from Worker order by FIRST_NAME asc,DEPARTMENT desc;

Copy
Q-13. Write an SQL query to print details for Workers with
the first names “Vipul” and “Satish” from the Worker table.

Ans.
The required query is:

Select * from Worker where FIRST_NAME in ('Vipul','Satish');

Copy
Q-14. Write an SQL query to print details of workers
excluding first names, “Vipul” and “Satish” from the Worker
table.

Ans.
The required query is:

Select * from Worker where FIRST_NAME not in


('Vipul','Satish');

Copy
Q-15. Write an SQL query to print details of Workers with
DEPARTMENT name as “Admin”.

Ans.
The required query is:
Select * from Worker where DEPARTMENT like 'Admin%';

Copy
Q-16. Write an SQL query to print details of the Workers
whose FIRST_NAME contains ‘a’.

Ans.
The required query is:

Select * from Worker where FIRST_NAME like '%a%';

Copy
Q-17. Write an SQL query to print details of the Workers
whose FIRST_NAME ends with ‘a’.

Ans.
The required query is:

Select * from Worker where FIRST_NAME like '%a';

Copy
Q-18. Write an SQL query to print details of the Workers
whose FIRST_NAME ends with ‘h’ and contains six alphabets.

Ans.
The required query is:

Select * from Worker where FIRST_NAME like '_____h';

Copy
Q-19. Write an SQL query to print details of the Workers
whose SALARY lies between 100000 and 500000.

Ans.
The required query is:

Select * from Worker where SALARY between 100000 and 500000;

Copy
Q-20. Write an SQL query to print details of the Workers who
joined in Feb’2014.

Ans.
The required query is:
Select * from Worker where year(JOINING_DATE) = 2014 and
month(JOINING_DATE) = 2;

Copy
Q-21. Write an SQL query to fetch the count of employees
working in the department ‘Admin’.

Ans.
The required query is:

SELECT COUNT(*) FROM worker WHERE DEPARTMENT = 'Admin';

Copy
Q-22. Write an SQL query to fetch worker names with
salaries >= 50000 and <= 100000.

Ans.
The required query is:

SELECT CONCAT(FIRST_NAME, ' ', LAST_NAME) As Worker_Name,


Salary

FROM worker

WHERE WORKER_ID IN

(SELECT WORKER_ID FROM worker

WHERE Salary BETWEEN 50000 AND 100000);

Copy
Q-23. Write an SQL query to fetch the no. of workers for
each department in descending order.

Ans.
The required query is:

SELECT DEPARTMENT, count(WORKER_ID) No_Of_Workers

FROM worker

GROUP BY DEPARTMENT

ORDER BY No_Of_Workers DESC;


Copy
Q-24. Write an SQL query to print details of the Workers who
are also Managers.

Ans.
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');

Copy
Q-25. Write an SQL query to fetch duplicate records having
matching data in some fields of a table.

Ans.
The required query is:

SELECT WORKER_TITLE, AFFECTED_FROM, COUNT(*)

FROM Title

GROUP BY WORKER_TITLE, AFFECTED_FROM

HAVING COUNT(*) > 1;

Copy
Q-26. Write an SQL query to show only odd rows from
a table.

Ans.
The required query is:

SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;

Copy
Q-27. Write an SQL query to show only even rows from
a table.
Ans.
The required query is:

SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) = 0;

Copy
Q-28. Write an SQL query to clone a new table from another
table.

Ans.
The general query to clone a table with data is:

SELECT * INTO WorkerClone FROM Worker;

Copy
The general way to clone a table without information is:

SELECT * INTO WorkerClone FROM Worker WHERE 1 = 0;

Copy
An alternate way to clone a table (for MySQL) without data is:

CREATE TABLE WorkerClone LIKE Worker;

Copy
Q-29. Write an SQL query to fetch intersecting records of
two tables.

Ans.
The required query is:

(SELECT * FROM Worker)

INTERSECT

(SELECT * FROM WorkerClone);

Copy
Q-30. Write an SQL query to show records from one table
that another table does not have.

Ans.
The required query is:
SELECT * FROM Worker

MINUS

SELECT * FROM Title;

Copy
Q-31. Write an SQL query to show the current date and time.

Ans.
The following MySQL query returns the current date:

SELECT CURDATE();

Copy
And the following MySQL query returns the current date and time:

SELECT NOW();

Copy
Here is a SQL Server query that returns the current date and time:

SELECT getdate();

Copy
Find this Oracle query that also returns the current date and time:

SELECT SYSDATE FROM DUAL;

Copy
Q-32. Write an SQL query to show the top n (say 10) records
of a table.

Ans.
MySQL query to return the top n records using the LIMIT method:

SELECT * FROM Worker ORDER BY Salary DESC LIMIT 10;

Copy
SQL Server query to return the top n records using the TOP
command:

SELECT TOP 10 * FROM Worker ORDER BY Salary DESC;


Copy
Oracle query to return the top n records with the help of ROWNUM:

SELECT * FROM (SELECT * FROM Worker ORDER BY Salary DESC)

WHERE ROWNUM <= 10;

Copy
Q-33. Write an SQL query to determine the nth (say n=5)
highest salary from a table.

Ans.
MySQL query to find the nth highest salary:

SELECT Salary FROM Worker ORDER BY Salary DESC LIMIT n-1,1;

Copy
SQL Server query to find the nth highest salary:

SELECT TOP 1 Salary

FROM (

SELECT DISTINCT TOP n Salary

FROM Worker

ORDER BY Salary DESC

ORDER BY Salary ASC;

Copy
Q-34. Write an SQL query to determine the 5th highest
salary without using the TOP or limit method.

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

);

Copy
Use the following generic method to find the 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

);

Copy
Q-35. Write an SQL query to fetch the list of employees with
the same salary.

Ans.
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;

Copy
Q-36. Write an SQL query to show the second-highest salary
from a table.
Ans.
The required query is:

Select max(Salary) from Worker

where Salary not in (Select max(Salary) from Worker);

Copy
Q-37. Write an SQL query to show one row twice in the
results from a table.

Ans.
The required query is:

select FIRST_NAME, DEPARTMENT from worker W where


W.DEPARTMENT='HR'

union all

select FIRST_NAME, DEPARTMENT from Worker W1 where


W1.DEPARTMENT='HR';

Copy
Q-38. Write an SQL query to fetch intersecting records of
two tables.

Ans.
The required query is:

(SELECT * FROM Worker)

INTERSECT

(SELECT * FROM WorkerClone);

Copy
Q-39. Write an SQL query to fetch the first 50% of records
from a table.

Ans.
The required query is:

SELECT *

FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);

Copy
Q-40. Write an SQL query to fetch the departments that have
less than five people in them.

Ans.
The required query is:

SELECT DEPARTMENT, COUNT(WORKER_ID) as 'Number of Workers'


FROM Worker GROUP BY DEPARTMENT HAVING COUNT(WORKER_ID) < 5;

Copy
Q-41. Write an SQL query to show all departments along
with the number of people in there.

Ans.
The following query returns the expected result:

SELECT DEPARTMENT, COUNT(DEPARTMENT) as 'Number of Workers'


FROM Worker GROUP BY DEPARTMENT;

Copy
Q-42. Write an SQL query to show the last record from a
table.

Ans.
The following query will return the last record from the Worker table:

Select * from Worker where WORKER_ID = (SELECT max(WORKER_ID)


from Worker);

Copy
Q-43. Write an SQL query to fetch the first row of a table.

Ans.
The required query is:

Select * from Worker where WORKER_ID = (SELECT min(WORKER_ID)


from Worker);

Copy
Q-44. Write an SQL query to fetch the last five records from
a table.
Ans.
The required query is:

SELECT * FROM Worker WHERE WORKER_ID <=5

UNION

SELECT * FROM (SELECT * FROM Worker W order by W.WORKER_ID


DESC) AS W1 WHERE W1.WORKER_ID <=5;

Copy
Q-45. Write an SQL query to print the name of employees
having the highest salary in each department.

Ans.
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;

Copy
Q-46. Write an SQL query to fetch three max salaries from a
table.

Ans.
The required query is:

SELECT distinct Salary from worker a WHERE 3 >= (SELECT


count(distinct Salary) from worker b WHERE a.Salary <=
b.Salary) order by a.Salary desc;

Copy
Q-47. Write an SQL query to fetch three min salaries from a
table.

Ans.
The required query is:

SELECT distinct Salary from worker a WHERE 3 >= (SELECT


count(distinct Salary) from worker b WHERE a.Salary >=
b.Salary) order by a.Salary desc;
Copy
Q-48. Write an SQL query to fetch nth max salaries from a
table.

Ans.
The required query is:

SELECT distinct Salary from worker a WHERE n >= (SELECT


count(distinct Salary) from worker b WHERE a.Salary <=
b.Salary) order by a.Salary desc;

Copy
Q-49. Write an SQL query to fetch departments along with
the total salaries paid for each of them

You might also like