Top 20 SQL Query Questions from Programming and Tech Interviews
SQL queries are commonly asked in Programming and Data Science Job Interviews, if you are preparing for Tech interviews then practicing these queries can give you edge.

Hello guys, if you have given any coding or programming job interview then you may know that SQL and Database questions are quite common there but the toughest ones are always the SQL queries which can not only need good understanding of SQL to solve but also creativity.
Like many other things, you can practice SQL queries before interviews so that you can do well when such questions asked in real interviews, but the questions comes which SQL query questions should you solve?
Well, you should solve the common SQL queries ask on Interviews like Google, Amazon, Facebook, JP Morgon, UBS, Citibank etc and where do you find them? Well, you can Google them.
There are also many sites like Leetcode where you can practice SQL questions but at the moment you don’t need to go anywhere as I am going to share 20 frequently asked SQL queries for practice right here
And, if you need more questions not just queries but also database and SQL related questions from other topics like indexes, joins, group by, aggregation, and window functions then you can also checkout Grokking the SQL Interview book by Javin Paul.
This book is one of the specially designed book to prepare you for SQL interviews by answering popular questions. You an also use discount code friends20 to get 20% discount now.
20 Best SQL Queries Beginners and Experienced Developers Can Practice for Coding Interviews
So, here they are, these are the popular SQL queries collected from various interviews, many of them like second highest salary has been asked to myself multiple times and that’s the reason I have put it at the first place.
- Write an SQL query to find the second highest salary from an employee table? (solution)
- Write an SQL query to remove the duplicate rows in a table? (solution)
- How do you find the most common job title in an employee table? (solution)
- Write a SQL query to find the number of employees who have the same salary as the highest paid employee? (solution)
- How do you find all the employees who are also manager? (solution)
- Can you write an SQL query to find all employees who are earning more than managers? (solution)
- Write an SQL query to find the average salary of all employees, grouped by department?
- Can you write a query to find duplicate emails in given table? (solution)
- How do you find the number of employees who were born in the same month and year as the earliest hire date in the employee table?
- Write an SQL query to compare dates? Write query to find all rows between a a given date range (solution)
- How do you find the total salary paid to all employees, grouped by job title and ordered by total salary in descending order.
- Write SQL query to print top 10 most expensive products in an order table, based on the unit price of the product?
- Can you write an SQL query to find all the customers who have never ordered? (solution)
- How do you find the number of orders placed by each customer, ordered by the number of orders in descending order?
- How do you find the total sales for each product, grouped by category and ordered by total sales in descending order.
- Write an SQL query to find the Nth highest salary in SQL? (solution)
- How do you find the average salary of all employees, grouped by job title, and only include job titles that have more than 5 employees.
- Write an SQL query to find all the departments where no one work?
- What is difference between WHERE and HAVING in SQL? (answer)
Solution of SQL queries from Interviews
A lot of you asked me to put solution as well, so here I am giving solution of a couple of SQL query question asked above:
How to find the 2nd highest salary in SQL?
There are multiple ways to find the 2nd highest salary for example, you can use the below query to find the 2nd highest salary in MySQL or any other database which support LIMIT keyword:
SELECT DISTINCT Salary FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1;his query uses a subquery to select all the distinct salaries from the Employee table, orders them in descending order, and then selects the second highest salary by using the LIMIT and OFFSET clauses.
This query uses a subquery to select all the distinct salaries from the Employee table, orders them in descending order, and then selects the second highest salary by using the LIMIT and OFFSET clauses.
Another way to achieve the same result is by using the following query:
SELECT MAX(Salary)
FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee);This query uses a subquery to find the maximum salary from the Employee table, and then finds the second highest salary by selecting the maximum salary from the Employee table that is not equal to the maximum salary found in the subquery. You can use this query to also find the 3rd highest salary in SQL.
This is also a generic SQL query that should work with most relational databases.
Java and Spring Interview Preparation Material
Before any Java and Spring Developer interview, I always use to read the below resources
Grokking the Java Interview
Grokking the Java Interview: click here
I have personally bought these books to speed up my preparation.
You can get your sample copy here, check the content of it and go for it
Grokking the Java Interview [Free Sample Copy]: click here

If you want to prepare for the Spring Boot interview you follow this consolidated ebook, it also contains microservice questions from spring boot interviews.
Grokking the Spring Boot Interview
You can get your copy here — Grokking the Spring Boot Interview

That’s all about the 20 most popular SQL queries from Interviews. These questions covers a wide variety of SQL concepts like Join, Subquery, Aggregation, Window Function and much more and you can practice them not only prepare for SQL interview but also get better at SQL itself.
Thanks for reading this article so far and if you are not a Medium member then I highly recommend you to join Medium and read great stories from great authors from real field. You can join Medium here




