SQL Interview Questions and Answers
1. What is SQL?
SQL (Structured Query Language) is a standard programming language used to manage and
manipulate relational databases. It can be used to query, update, insert, and delete data, as well as
manage database structures.
2. What are the different types of SQL commands?
SQL commands are categorized as follows:
- DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE
- DML (Data Manipulation Language): INSERT, UPDATE, DELETE
- DQL (Data Query Language): SELECT
- DCL (Data Control Language): GRANT, REVOKE
- TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
3. Write a query to fetch the second-highest salary from an Employee table.
SELECT MAX(Salary) AS SecondHighestSalary
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee);
4. What is the difference between WHERE and HAVING?
- WHERE: Filters rows before any grouping is applied.
Example: SELECT * FROM Employee WHERE Age > 30;
- HAVING: Filters groups after grouping has been applied.
Example: SELECT Department, COUNT(*) FROM Employee GROUP BY Department HAVING
COUNT(*) > 10;
5. How do you find duplicate records in a table?
SELECT Column1, Column2, COUNT(*)
FROM TableName
GROUP BY Column1, Column2
HAVING COUNT(*) > 1;
6. What is the difference between INNER JOIN and OUTER JOIN?
- INNER JOIN: Returns rows that have matching values in both tables.
Example:
SELECT Employee.Name, Department.Name
FROM Employee
INNER JOIN Department
ON Employee.DepartmentID = Department.ID;
- OUTER JOIN: Returns all rows from one table and matching rows from another. Types:
- LEFT JOIN: All rows from the left table.
- RIGHT JOIN: All rows from the right table.
- FULL JOIN: All rows from both tables.
7. Write a query to fetch the top 3 highest salaries from an Employee table.
SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 3;
8. What is the difference between TRUNCATE and DELETE?
- TRUNCATE:
- Deletes all rows from a table but does not log individual row deletions.
- Cannot be rolled back.
- Resets identity columns.
- DELETE:
- Deletes specific rows using a WHERE condition.
- Logs each row deletion and can be rolled back.
9. How can you avoid duplicate records in a query result?
Use the DISTINCT keyword:
SELECT DISTINCT Column1, Column2
FROM TableName;
10. Write a query to find employees who earn more than the average salary.
SELECT Name, Salary
FROM Employee
WHERE Salary > (SELECT AVG(Salary) FROM Employee);
11. Explain the difference between UNION and UNION ALL.
- UNION: Combines results from two queries and removes duplicates.
- UNION ALL: Combines results from two queries without removing duplicates.
Example:
SELECT Name FROM Employee1
UNION
SELECT Name FROM Employee2;
SELECT Name FROM Employee1
UNION ALL
SELECT Name FROM Employee2;
12. What is indexing, and why is it used?
- Indexing: A database optimization technique used to speed up the retrieval of rows.
- Usage: It creates a data structure (like a B-Tree) that improves the speed of searches.
Example:
CREATE INDEX idx_employee_name ON Employee(Name);
13. How can you fetch the current date in SQL?
SELECT CURRENT_DATE;
14. Write a query to get the department-wise maximum salary.
SELECT DepartmentID, MAX(Salary) AS MaxSalary
FROM Employee
GROUP BY DepartmentID;
15. How do you use CASE statements in SQL?
SELECT Name,
CASE
WHEN Salary > 50000 THEN 'High Salary'
WHEN Salary BETWEEN 30000 AND 50000 THEN 'Medium Salary'
ELSE 'Low Salary'
END AS SalaryCategory
FROM Employee;