SQL Real-Time Interview Questions and Answers
1. How to retrieve the second-highest salary of an employee?
SELECT MAX (salary) FROM employees WHERE salary (SELECT MAX (salary) FROM
employees);
2. How to get the nth highest salary in?
SELECT salary FROM (SELECT salary, DENSE RANK () OVER (ORDER BY salary DESC) AS
rank FROM employees) AS ranked_salaries WHERE rank N;
3. How do you fetch all employees whose salary is greater than the average
salary?
SELECT FROM employees WHERE salary> (SELECT AVG (salary) FROM employees);
4. Write a query to display the current date and time in
SELECT CURRENT TIMESTAMP;
5. How to find duplicate records in a table?
SELECT column name, COUNT (*) FROM table name GROUP BY column_name HAVING
COUNT (*)>1;
6. How can you delete duplicate rows in?
WITH CTE AS SELECT ( column_name ,ROW NUMBER () OVER (PARTITION BY column
name ORDER BY column name) AS row num FROM table name) DELETE FROM CTE
WHERE row num > 1;
7. How to get the common records from two tables?
SELECT FROM table1 ITERSECT SELECT FROM table 2;
8. How to retrieve the last 10 records from a table?
SELECT Live chat SELECT FROM employees ORDER BY employee id DESC LIMIT 10;
9. How do you fetch the top 5 employees with the highest salaries?
FROM employees ORDER BY salary DESC LIMIT 5;
10. How to calculate the total salary of all employees?
SELECT SUM (salary) FROM employees;