SQL Mock Interview Questions & Answers
1. What is SQL? Difference between SQL and MySQL?
SQL (Structured Query Language) is used to manage and manipulate relational databases. MySQL is a database
management system that uses SQL.
2. Types of SQL Statements
DDL: CREATE, ALTER, DROP
DML: INSERT, UPDATE, DELETE
DCL: GRANT, REVOKE
TCL: COMMIT, ROLLBACK
3. WHERE vs HAVING
WHERE filters rows before grouping.
HAVING filters after GROUP BY.
4. DELETE vs TRUNCATE vs DROP
DELETE: Deletes rows (can rollback)
TRUNCATE: Deletes all rows (no rollback)
DROP: Deletes table structure
5. Primary vs Foreign Key
Primary: Uniquely identifies a row.
Foreign: Links to primary key in another table.
6. Constraints in SQL
NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT
7. Joins in SQL
INNER JOIN: Common rows
LEFT JOIN: All from left + matched right
RIGHT JOIN: All from right + matched left
FULL JOIN: All rows
CROSS JOIN: Cartesian product
SELF JOIN: Table joined with itself
8. GROUP BY Usage
SQL Mock Interview Questions & Answers
Used to group rows and perform aggregate functions like SUM, COUNT, etc.
9. SQL Execution Order
1. FROM
2. JOIN
3. WHERE
4. GROUP BY
5. HAVING
6. SELECT
7. ORDER BY
8. LIMIT
10. Second Highest Salary
SELECT MAX(Salary) FROM Employees WHERE Salary < (SELECT MAX(Salary) FROM Employees);
11. Window Functions
RANK(), DENSE_RANK(), ROW_NUMBER(), LAG(), LEAD(), SUM() OVER(), etc.
12. CTE (Common Table Expression)
WITH temp AS (SELECT ...) SELECT * FROM temp;
13. Indexing in SQL
Index is a database object that improves query performance. Types:
- Clustered Index
- Non-Clustered Index
- Unique Index
- Composite Index
Use indexes on WHERE, JOIN, ORDER BY columns. Avoid indexing frequently updated or low-cardinality columns.
14. Clustered vs Non-Clustered Index
Clustered: Sorts table rows, one per table
Non-Clustered: Separate structure, many allowed
15. Index Creation Example
SQL Mock Interview Questions & Answers
CREATE INDEX idx_name ON Employees(Name);
CREATE CLUSTERED INDEX idx_salary ON Employees(Salary);