Skip to main content

Top 15 Real SQL Queries Asked in Backend Developer Interviews

Top 15 Real SQL Queries Asked in Backend Developer Interviews




If you're preparing for a backend developer interview, SQL is one skill you can’t afford to ignore. Whether it's a startup or a product-based company like Amazon, Swiggy, or Paytm, SQL queries are commonly asked to test your data handling and logic-building ability. Here's a list of 15 real-world SQL queries that interviewers ask again and again — with sample table references and expected outputs.

1. Find the second highest salary from the Employee table.

SELECT MAX(salary) AS Second_Highest_Salary
FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee);

2. Retrieve employee details who have the highest salary in each department.

SELECT *
FROM employee e
WHERE salary = (
  SELECT MAX(salary)
  FROM employee
  WHERE department_id = e.department_id
);

3. Write a query to fetch duplicate records from a table.

SELECT name, COUNT(*)
FROM employee
GROUP BY name
HAVING COUNT(*) > 1;

4. Get employee names who joined in the last 3 months.

SELECT name
FROM employee
WHERE joining_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH);

5. Show department-wise average salary and count of employees.

SELECT department_id, COUNT(*) AS emp_count, AVG(salary) AS avg_salary
FROM employee
GROUP BY department_id;

6. Write a query to fetch top 3 highest-paid employees.

SELECT *
FROM employee
ORDER BY salary DESC
LIMIT 3;

7. Get all employees who don’t have managers.

SELECT *
FROM employee
WHERE manager_id IS NULL;

8. Fetch all employees who belong to departments that have more than 5 employees.

SELECT *
FROM employee
WHERE department_id IN (
  SELECT department_id
  FROM employee
  GROUP BY department_id
  HAVING COUNT(*) > 5
);

9. Find all employees whose name starts and ends with a vowel.

SELECT name
FROM employee
WHERE name REGEXP '^[aeiouAEIOU].*[aeiouAEIOU]$';

10. Get employee(s) with salary more than the average salary.

SELECT *
FROM employee
WHERE salary > (SELECT AVG(salary) FROM employee);

11. Fetch all employees and their department names using JOIN.

SELECT e.name, d.department_name
FROM employee e
JOIN department d ON e.department_id = d.id;

12. Write a query to find employees with the same salary.

SELECT salary
FROM employee
GROUP BY salary
HAVING COUNT(*) > 1;

13. Get the latest joining employee in each department.

SELECT *
FROM employee e
WHERE joining_date = (
  SELECT MAX(joining_date)
  FROM employee
  WHERE department_id = e.department_id
);

14. Find the total salary paid in each department including department name.

SELECT d.department_name, SUM(e.salary) AS total_salary
FROM employee e
JOIN department d ON e.department_id = d.id
GROUP BY d.department_name;

15. Get employees whose salaries are within the top 10% of all salaries.

SELECT *
FROM employee
WHERE salary >= (
  SELECT PERCENTILE_CONT(0.90) WITHIN GROUP (ORDER BY salary)
  FROM employee
);

*Note: The above syntax is for PostgreSQL. For MySQL, use window functions or percentile calculation logic manually.

Conclusion

Mastering these SQL queries will not only boost your confidence but also help you crack backend interviews easily. Practice them with mock tables and explain the logic during interviews — that’s what interviewers love.

Follow InterviewYatra.com for more backend interview content, system design questions, and real company interview experiences.

Comments

Popular posts from this blog

TCS Java Interview | Java | Spring Boot | Microservices | Database

TCS Java Interview | Java | Spring Boot | Microservices | Database This post is based on a Java backend developer interview simulation and provides direct transcript-based answers to help you prepare for similar interviews. 📺 This interview scenario was inspired by a video from the  YouTube channel:  CloudTech . 🔗 Watch the full video here:  https://www.youtube.com/watch?v=yVj2EgwZxk4 1. Can you introduce yourself and talk about your tech stack and domains? I have around 4.5 years of experience working as a Java developer. My core expertise is in Java and developing REST APIs using Spring Boot. I also have basic knowledge of microservices architecture. On the database side, I work with SQL and Oracle. Additionally, I have exposure to UI technologies like Angular, HTML, and CSS, but they are secondary skills. My primary focus is backend Java development. 2. Which version of Java are you currently working on? We are currently using Java 8 but are in the process of upgr...

Top 20 Spring Boot Microservices Architecture Interview Questions (With Speakable Answers)

Top 20 Spring Boot Microservices Architecture Interview Questions (With Speakable Answers) * * * 1. What is Microservices Architecture?   → Microservices architecture breaks an application into small, independent services. Each one handles a specific business function and can be developed, deployed, and scaled independently. They usually communicate over REST APIs or messaging queues. * * * 2. How does Spring Boot help in building microservices?   → Spring Boot offers embedded servers, auto-configuration, starter templates, and production-ready tools. It works well with Spring Cloud to provide service discovery, config management, load balancing, and more. * * * 3. What are the core components in a microservices setup?   → Key components include:   ✓ API Gateway   ✓ Eureka (Service Discovery)   ✓ Config Server   ✓ Kafka or RabbitMQ   ✓ Circuit Breaker (like Resilience4j)   ✓ Centralized logging and monitoring (ELK, Prometheus) * * * 4. What i...

Top 15 Spring Boot Interview Questions and Answers – Real Examples (2025)

Top 15 Spring Boot Interview Questions – 2025 Spring Boot is one of the most demanded frameworks for Java backend development. Whether you're interviewing for TCS, Infosys, or a product-based company, these Spring Boot questions will help you prepare like a pro. Here are 15 questions with detailed explanations for developers with 1–2 years of experience. Q1. What is Spring Boot? Answer: Spring Boot is a Java-based open-source framework built on top of the Spring Framework. It helps developers create stand-alone, production-ready Spring applications with minimal configuration. Its key features include: Auto-configuration Embedded servers (Tomcat, Jetty) Starter dependencies Production-ready tools (Actuator, Metrics, etc.) Example: You can create a REST API within minutes by using @RestController and spring-boot-starter-web — no need for external web server deployment. Q2. What is the role of @SpringBootApplication annotation? Answer: This annotation i...