SQL (Structured Query Language) is one of the most common topics asked in any technical interview — whether you’re a fresher or an experienced professional.
In this post, we’ll go through important SQL program-based questions that are often asked in interviews, along with easy-to-understand answers and examples.
Let’s begin 👇
1. Write an SQL query to display the second highest salary from an Employee table.
Table Name: Employee
Columns: id, name, salary
Query:
SELECT MAX(salary) AS Second_Highest_Salary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);
Explanation:
- First, we find the maximum salary using a subquery.
- Then we select the maximum salary that is less than that maximum — which gives us the second highest salary.
- This is one of the most popular interview questions.
✅ Tip: You can also use LIMIT or TOP depending on your database (MySQL or SQL Server).
2. Write an SQL query to fetch duplicate records from a table.
Table: Students
Columns: id, name, email
Query:
SELECT name, COUNT(*)
FROM Students
GROUP BY name
HAVING COUNT(*) > 1;
Explanation:
GROUP BYgroups rows with the same name.HAVING COUNT(*) > 1filters only those names that appear more than once.
This helps in finding duplicates easily.
3. Write an SQL query to fetch the first three highest salaries.
Query (MySQL):
SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
LIMIT 3;
Explanation:
- The
ORDER BY salary DESCsorts salaries in descending order. LIMIT 3returns the top 3 salaries.- If you want to find the third highest salary only, use
LIMIT 2,1.
4. Write an SQL query to count total employees in each department.
Table: Employee
Columns: id, name, department, salary
Query:
SELECT department, COUNT(*) AS Total_Employees
FROM Employee
GROUP BY department;
Explanation:
GROUP BYis used to group data by department.COUNT(*)counts how many employees are there in each department.
5. Write an SQL query to find employees whose name starts with ‘A’.
Query:
SELECT *
FROM Employee
WHERE name LIKE 'A%';
Explanation:
- The
LIKEoperator is used for pattern matching. 'A%'means any name starting with letter ‘A’.'A_'would mean names starting with A and having only one more character after it.
6. Write an SQL query to get the current date.
Query (MySQL):
SELECT CURDATE();
Query (SQL Server):
SELECT GETDATE();
Explanation:
This is a simple query but often asked to check your basic command of SQL functions.
7. Write an SQL query to show employees who earn more than the average salary.
Query:
SELECT name, salary
FROM Employee
WHERE salary > (SELECT AVG(salary) FROM Employee);
Explanation:
- The subquery
(SELECT AVG(salary) FROM Employee)gives the average salary. - The main query then selects those who earn above average.
8. Write an SQL query to find the nth highest salary.
For MySQL:
SELECT DISTINCT salary
FROM Employee e1
WHERE (n-1) = (
SELECT COUNT(DISTINCT salary)
FROM Employee e2
WHERE e2.salary > e1.salary
);
Explanation:
- Replace
nwith any number (like 3 for third highest). - It compares how many salaries are greater than the current one and selects the required rank.
9. Write an SQL query to join two tables (Employee and Department).
Table 1: Employee(id, name, dept_id)
Table 2: Department(dept_id, dept_name)
Query:
SELECT e.name, d.dept_name
FROM Employee e
INNER JOIN Department d
ON e.dept_id = d.dept_id;
Explanation:
INNER JOINreturns only the matching records from both tables.- It helps combine related data from multiple tables.
10. Write an SQL query to remove duplicate records from a table.
Query (Using ROW_NUMBER):
DELETE FROM Employee
WHERE id NOT IN (
SELECT MIN(id)
FROM Employee
GROUP BY name, salary, department
);
Explanation:
- We keep only the record with the smallest
idfor each unique combination. - The rest are deleted, effectively removing duplicates.
11. Write an SQL query to find employees who do not belong to any department.
Query:
SELECT name
FROM Employee
WHERE dept_id IS NULL;
Explanation:
IS NULLis used to find missing or empty values.- Common question to test your knowledge of null handling.
12. Write an SQL query to get department names and their total salary expense.
Query:
SELECT department, SUM(salary) AS Total_Salary
FROM Employee
GROUP BY department;
Explanation:
- The
SUM()function adds up salaries of each department. - Useful for finance or reporting type questions.
13. Write an SQL query to display employees who joined in the last 3 months.
Query (MySQL):
SELECT *
FROM Employee
WHERE joining_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH);
Explanation:
DATE_SUB()subtracts time from the current date.- It fetches employees who joined recently — very common real-world query.
14. Write an SQL query to find employees whose salary is between 30,000 and 60,000.
Query:
SELECT *
FROM Employee
WHERE salary BETWEEN 30000 AND 60000;
Explanation:
BETWEENis an inclusive range — includes both boundary values.
15. Write an SQL query to fetch top 5 records from a table.
Query (MySQL):
SELECT *
FROM Employee
LIMIT 5;
Query (SQL Server):
SELECT TOP 5 *
FROM Employee;
Explanation:
- The syntax changes slightly based on the database.
- This is used for previewing or testing small datasets.
16. Write an SQL query to find the total number of employees who joined each year.
Query:
SELECT YEAR(joining_date) AS Join_Year, COUNT(*) AS Total_Employees
FROM Employee
GROUP BY YEAR(joining_date)
ORDER BY Join_Year;
Explanation:
- The
YEAR()function extracts the year from the date. - This helps understand hiring trends.
17. Write an SQL query to display employees along with their manager names.
Table: Employee(id, name, manager_id)
Query:
SELECT e.name AS Employee, m.name AS Manager
FROM Employee e
LEFT JOIN Employee m
ON e.manager_id = m.id;
Explanation:
- The self-join helps show hierarchical relationships.
LEFT JOINensures even employees without managers appear.
18. Write an SQL query to count male and female employees.
Query:
SELECT gender, COUNT(*) AS Total
FROM Employee
GROUP BY gender;
Explanation:
- Simple
GROUP BYexample that also checks your understanding of category-based data.
19. Write an SQL query to list employees with the same salary.
Query:
SELECT salary, COUNT(*)
FROM Employee
GROUP BY salary
HAVING COUNT(*) > 1;
Explanation:
- Similar to duplicate check — it shows which salaries appear more than once.
20. Write an SQL query to rank employees based on salary.
Query (Using RANK()):
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS Rank_Position
FROM Employee;
Explanation:
RANK()gives each employee a rank based on their salary.- If two employees have the same salary, they share the same rank.
✅ Final Tips for SQL Program Interviews
- Always understand the logic behind each query — don’t just memorize.
- Practice queries on real sample data.
- Be ready to explain your thought process to the interviewer.
- Know basic SQL functions like
MAX(),MIN(),AVG(),COUNT(), andSUM(). - Learn Joins, Subqueries, and Window Functions — they’re interview favorites.
Conclusion
These SQL program-based interview questions cover everything from basic to advanced levels. If you practice these well, you’ll feel confident in any SQL interview.
SQL is not just about queries — it’s about logic and problem-solving. So focus on understanding the “why” behind every question.
Glimsy.in – Learn Smart, Grow Confident.
Sup folks! jljl533 looks legit enough. Have a gander at jljl533
This site is very user friendly even for beginners. I appreciate the clear layout and the fast loading times. 998jilicom is definitely worth checking out.
Top tier service and very fair odds. I’ve recommended this site to a few friends and we all have had a blast playing together. Visit jilifunregisteronline for a premium experience.
The Asian market options here are fantastic. Very inclusive and the rewards system is quite generous. I’m sticking with phplus777asia from now on.
Super addictive games and the daily rewards keep me coming back. I have told all my friends to join phjilii already!