Top 20 SQL Interview Questions for Freshers
1. What is SQL?
SQL (Structured Query Language) is used to store, manipulate, and retrieve data from relational
databases.
2. What are the different types of SQL commands?
DDL (CREATE, ALTER), DML (INSERT, UPDATE), DQL (SELECT), DCL (GRANT), TCL
(COMMIT, ROLLBACK).
3. Retrieve all data from a table
SELECT * FROM employees;
4. Get only specific columns
SELECT name, salary FROM employees;
5. Find unique values
SELECT DISTINCT department FROM employees;
6. What is a Primary Key?
A primary key uniquely identifies each record in a table and cannot be null or duplicated.
7. What is a Foreign Key?
A foreign key links one table to another and maintains referential integrity.
8. Filter data using WHERE
SELECT * FROM employees WHERE salary > 30000;
9. Sort data
SELECT * FROM employees ORDER BY name ASC;
10. Count rows
SELECT COUNT(*) FROM employees;
11. WHERE vs HAVING
WHERE filters rows before grouping; HAVING filters after grouping.
12. Group data
SELECT department, COUNT(*) FROM employees GROUP BY department;
13. Pattern matching with LIKE
SELECT * FROM employees WHERE name LIKE 'A%';
14. Find NULL values
SELECT * FROM employees WHERE manager_id IS NULL;
15. What is INNER JOIN?
Returns records that match in both tables.
INNER JOIN Example
SELECT e.name, d.name FROM employees e INNER JOIN departments d ON e.dept_id = d.id;
16. DELETE vs TRUNCATE vs DROP
DELETE: Conditional remove (rollback allowed)
TRUNCATE: Removes all rows (no rollback)
DROP: Deletes table structure.
17. Update a record
UPDATE employees SET salary = 40000 WHERE id = 1;
18. Insert a record
INSERT INTO employees (name, salary, department) VALUES ('Daoud', 35000, 'IT');
19. Delete specific rows
DELETE FROM employees WHERE department = 'HR';
20. What is a subquery?
A subquery is a query inside another query.
Example: SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM
employees);