What are the top 5 common SQL interview questions and answers?

Interviewers often focus on practical SQL interview questions and answers that assess your understanding of queries, joins, and database logic. Some frequently asked questions include:

  1. How do you find the second highest salary of an employee using SQL?
  2. How can you retrieve the maximum salary from each department?
  3. What is the SQL command to display the current date?
  4. How do you check if a given value is a valid date in SQL?
  5. How can you select distinct employees whose DOB falls between two given dates?

These core SQL concepts test your ability to write optimized queries and demonstrate knowledge of SQL functions, joins, and subqueries.

To find the second highest salary, use:

SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

This query filters out the top salary and retrieves the next one.

To get the maximum salary per department:

SELECT department_id, MAX(salary) AS max_salary
FROM employees
GROUP BY department_id;

GROUP BY helps calculate maximum salary within each department

To find employees born between two specific dates:

SELECT DISTINCT employee_name, dob
FROM employees
WHERE dob BETWEEN '1990-01-01' AND '2000-12-31';

BETWEEN checks if a date lies within the given range.