50 SQL Interview Questions
50 SQL Interview Questions
com/)
Search for
courses for free
Compare courses from
top universities and
Coursary online platforms for free.
Most of the SQL query questions we’ve filtered out of interviews held by top IT MNC like
Flipkart and Amazon. So you’ll gain real-time experience by going through them.
Also, we recommend that you first try to form queries by yourself rather than just reading
them from the post. Try to find answers on your own.
But you can’t start until the required sample data is not in place. You can check out the tables
below that we’ve provided for practice. So first of all, you need to create the test data in your
database software.
By the way, we have a bunch of other posts available for SQL interview preparation. So if you
are interested, then follow the link given below.
▲
50 SQL Query Questions
Prepare Sample Data To Practice SQL Skill.
▲
2 Executive 2016-06-11 00:00:00
8 Executive 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. You can also download these Softwares and install
them to carry on the SQL exercise.
▲
CREATE DATABASE ORG;
SHOW DATABASES;
USE ORG;
▲
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
);
Once above SQL would run, you’ll see a result similar to the one attached below.
Q-1. Write an SQL query to fetch “FIRST_NAME” from Worker table using the
alias name as <WORKER_NAME>.
Ans.
Q-2. Write an SQL query to fetch “FIRST_NAME” from Worker table in upper
case.
Ans.
▲
Q-3. Write an SQL query to fetch unique values of DEPARTMENT from
Worker table.
Ans.
Q-4. Write an SQL query to print the first three characters of FIRST_NAME
from Worker table.
Ans.
Q-5. Write an SQL query to find the position of the alphabet (‘a’) in the first
name column ‘Amitabh’ from Worker table.
Ans.
Notes.
Q-6. Write an SQL query to print the FIRST_NAME from Worker table after
removing white spaces from the right side.
Ans.
Q-7. Write an SQL query to print the DEPARTMENT from Worker table after
removing white spaces from the left side.
Ans.
Q-8. Write an SQL query that fetches the unique values of DEPARTMENT
from Worker table and prints its length.
Ans.
Q-9. Write an SQL query to print the FIRST_NAME from Worker table after
replacing ‘a’ with ‘A’.
Ans.
Q-10. Write an SQL query to print the FIRST_NAME and LAST_NAME from
Worker table into a single column COMPLETE_NAME. A space char should
separate them.
Ans.
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;
Q-12. Write an SQL query to print all Worker details from the Worker table
order by FIRST_NAME Ascending and DEPARTMENT Descending.
Ans.
Q-13. Write an SQL query to print details for Workers with the first name as
“Vipul” and “Satish” from Worker table.
Ans.
Q-14. Write an SQL query to print details of workers excluding first names,
“Vipul” and “Satish” from Worker table.
Ans.
Ans.
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%';
Q-17. Write an SQL query to print details of the Workers whose FIRST_NAME
ends with ‘a’.
Ans.
Q-18. Write an SQL query to print details of the Workers whose FIRST_NAME
ends with ‘h’ and contains six alphabets.
Ans.
Q-19. Write an SQL query to print details of the Workers whose SALARY lies
between 100000 and 500000.
Ans.
Q-20. Write an SQL query to print details of the Workers who have joined in
Feb’2014.
Ans.
Q-21. Write an SQL query to fetch the count of employees working in the
department ‘Admin’.
Ans.
▲
The required query is:
Q-22. Write an SQL query to fetch worker names with salaries >= 50000 and
<= 100000.
Ans.
Q-23. Write an SQL query to fetch the no. of workers for each department in
the descending order.
Ans.
Q-24. Write an SQL query to print details of the Workers who are also
Managers.
Ans.
Q-25. Write an SQL query to fetch duplicate records having matching data in
some fields of a table.
Ans.
▲
SELECT WORKER_TITLE, AFFECTED_FROM, COUNT(*)
FROM Title
GROUP BY WORKER_TITLE, AFFECTED_FROM
HAVING COUNT(*) > 1;
Q-26. Write an SQL query to show only odd rows from a table.
Ans.
Q-27. Write an SQL query to show only even rows from a table.
Ans.
Q-28. Write an SQL query to clone a new table from another table.
Ans.
Ans.
Ans.
Q-31. Write an SQL query to show the current date and time.
Ans.
SELECT CURDATE();
SELECT NOW();
Following SQL Server query returns the current date and time:
SELECT getdate();
Q-32. Write an SQL query to show the top n (say 10) records of a table.
Ans.
Following MySQL query will return the top n records using the LIMIT method:
Following SQL Server query will return the top n records using the TOP command:
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;
Q-33. Write an SQL query to determine the nth (say n=5) highest salary from
a table.
Ans.
The following SQL Server query returns the nth highest salary:
Q-34. Write an SQL query to determine the 5th highest salary without using
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
);
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
);
▲
Q-35. Write an SQL query to fetch the list of employees with the same salary.
Ans.
Q-36. Write an SQL query to show the second highest salary from a table.
Ans.
Q-37. Write an SQL query to show one row twice in results from a table.
Ans.
Ans.
Q-39. Write an SQL query to fetch the first 50% records from a table.
Ans.
▲
The required query is:
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);
Q-40. Write an SQL query to fetch the departments that have less than five
people in it.
Ans.
Q-41. Write an SQL query to show all departments along with the number of
people in there.
Ans.
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:
Ans.
Q-44. Write an SQL query to fetch the last five records from a table.
Ans. ▲
The required query is:
Q-45. Write an SQL query to print the name of employees having the highest
salary in each department.
Ans.
Q-46. Write an SQL query to fetch three max salaries from a table.
Ans.
Q-47. Write an SQL query to fetch three min salaries from a table.
Ans.
Q-48. Write an SQL query to fetch nth max salaries from a table.
Ans.
▲
Q-49. Write an SQL query to fetch departments along with the total salaries
paid for each of them.
Ans.
Q-50. Write an SQL query to fetch the names of workers who earn the
highest salary.
Ans.
Let us take a pause here. But we’ll come back with more challenging questions on SQL
queries in our next post.
If you find something new to learn today, then do share it with others. And, follow us on our
social media (Facebook (https://www.facebook.com/TechBeamersWorld/)/Twitter
(https://twitter.com/TechBeamers)) accounts to see more of this.
Best,
TechBeamers
(https://www.techbeamers.co
m/sql-exercises/)
SQL Exercises –
Complex Queries (https://www.techbeamers.co (https://www.techbeamers.co
(https://www.techbea m/sql-interview-questions- m/sql-performance-interview-
mers.com/sql-
answers-experienced/) questions-answers/)
exercises/)
May 10, 2020 Top SQL Interview 25 SQL Performance
Questions One Interview Questions
Should Know and Answers
Beforehand (https://www.techbea
(https://www.techbea mers.com/sql-
mers.com/sql- performance-
interview-questions- interview-questions-
answers- answers/)
experienced/) December 10, 2016
September 23, 2016
⊙ Python Tutorial for Beginners ⊙ Python Interview Questions ⊙ AngularJS Interview Questions ⊙ About Us
(https://www.techbeamers.com/python- (https://www.techbeamers.com/python- (https://www.techbeamers.com/latest- (https://www.techbeamers.com/about-
tutorial-step-by-step/) interview-questions-programmers/) angularjs-interview-questions- techbeamers/)
(https://www.techbeamers.com/selenium-
⊙ Selenium Interview Questions ⊙ Web Developer Interview ⊙ Privacy Policy
webdriver-tutorial/) (https://www.techbeamers.com/latest- Questions (https://www.techbeamers.com/privacy-
⊙ Selenium Demo Websites selenium-interview-questions-and- (https://www.techbeamers.com/top- policy/)
answers/)
(https://www.techbeamers.com/websites- web-developer-interview-
▲
▲