SQL Interview Questions & Answers
Q: What is SQL?
A: SQL (Structured Query Language) is used to store, manage, and retrieve data from relational
databases.
Q: What is the difference between DELETE, TRUNCATE, and DROP?
A: DELETE → removes specific rows from a table.
TRUNCATE → removes all rows from a table (faster).
DROP → deletes the entire table structure.
Q: What is the difference between WHERE and HAVING?
A: WHERE → filters rows before grouping.
HAVING → filters rows after grouping (used with GROUP BY).
Q: Write a query to select all columns from a table named Employees.
A: SELECT * FROM Employees;
Q: Write a query to find all employees in the HR department.
A: SELECT * FROM Employees WHERE Department = 'HR';
Q: How do you sort records in SQL?
A: By using ORDER BY.
Example:
SELECT * FROM Employees ORDER BY Salary DESC;
Q: What is a Primary Key?
A: A column (or set of columns) that uniquely identifies each row in a table.
Q: What is a Foreign Key?
A: A column that links one table to another, maintaining relationships between data.
Q: Write a query to count the number of employees.
A: SELECT COUNT(*) FROM Employees;
Q: Write a query to find the highest salary from the Employees table.
A: SELECT MAX(Salary) FROM Employees;
Q: How do you find duplicate records in a table?
A: SELECT Name, COUNT(*) FROM Employees GROUP BY Name HAVING COUNT(*) > 1;
Q: What is the difference between INNER JOIN and LEFT JOIN?
A: INNER JOIN → returns only matching rows from both tables.
LEFT JOIN → returns all rows from the left table, even if there’s no match in the right table.