Relational Database Interview Questions With Sections
Relational Database Interview Questions With Sections
Q2: Select employee name and salary from the Employee table.
Q16: Find departments where the average salary is greater than 60000.
Date Functions
Q17: Fetch employees who joined in the last 30 days.
SELECT * FROM Employee WHERE joining_date >= CURRENT_DATE - INTERVAL '30 days';
Joins
Q21: Get a list of all employees with their department names.
SELECT e.name AS employee, m.name AS manager FROM Employee e LEFT JOIN Employee
m ON e.manager_id = m.id;
Q23: Find all employees and their project names.
SELECT o.order_id, c.name FROM Orders o LEFT JOIN Customer c ON o.customer_id = c.id;
Q27: List departments and the number of employees, including empty departments.
Subqueries
Q28: Fetch employees who earn more than the average salary.
SELECT * FROM Employee WHERE salary > (SELECT AVG(salary) FROM Employee);
SELECT * FROM Employee WHERE salary = (SELECT MAX(salary) FROM Employee WHERE
salary < (SELECT MAX(salary) FROM Employee));
SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num FROM
Employee;
Set Operations
Q37: Find common employees in two departments (INTERSECT).
SELECT name FROM Employee WHERE department_id = 1 INTERSECT SELECT name FROM
Employee WHERE department_id = 2;
SELECT name FROM Employee WHERE department_id = 1 EXCEPT SELECT name FROM
Employee WHERE department_id = 2;
Data Manipulation
Q40: Write an INSERT query for adding a new employee.
INSERT INTO Employee (name, salary, department_id) VALUES ('John Doe', 50000, 2);
SELECT name, COUNT(*) FROM Employee GROUP BY name HAVING COUNT(*) > 1;
DELETE FROM Employee WHERE id NOT IN (SELECT MIN(id) FROM Employee GROUP BY
name, salary);
SELECT * FROM (SELECT *, DENSE_RANK() OVER (ORDER BY salary DESC) AS rank FROM
Employee) AS ranked WHERE rank = 3;