Complete SQL Interview Questions & Answers
1. SQL Basics
• SQL stands for Structured Query Language and is used to manage relational databases.
• Types of SQL commands: DDL, DML, DQL, DCL, TCL.
• Primary Key uniquely identifies a record and cannot be NULL.
• Foreign Key establishes a relationship between two tables.
2. SELECT Queries
• SELECT retrieves data from database tables.
• WHERE filters rows, HAVING filters grouped data.
• DISTINCT removes duplicate records.
• ORDER BY sorts result set, GROUP BY groups rows.
3. Joins
• INNER JOIN returns matching rows from both tables.
• LEFT JOIN returns all rows from left table and matched rows from right.
• RIGHT JOIN returns all rows from right table.
• SELF JOIN joins a table with itself.
4. Aggregate Functions
• COUNT(), SUM(), AVG(), MIN(), MAX() are aggregate functions.
• COUNT(*) counts all rows including NULLs.
• COUNT(column) ignores NULL values.
5. Subqueries
• A subquery is a query within another query.
• Single-row, multi-row and correlated subqueries are types.
• Used to fetch data based on conditions.
6. Constraints
• PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK, DEFAULT.
• UNIQUE allows NULL, PRIMARY KEY does not.
7. Indexes & Performance
• Indexes improve query performance.
• Avoid indexes on frequently updated columns.
8. Normalization
• Normalization reduces redundancy.
• 1NF: atomic values, 2NF: no partial dependency, 3NF: no transitive dependency.
9. Transactions
• Transaction is a logical unit of work.
• ACID properties: Atomicity, Consistency, Isolation, Durability.
• COMMIT saves changes, ROLLBACK undoes changes.
10. Views, Procedures & Functions
• View is a virtual table.
• Stored procedure is precompiled SQL block.
• Functions return values, procedures may not.
11. NULL Handling
• NULL represents missing value.
• Use IS NULL instead of = NULL.
• COALESCE replaces NULL with value.
12. Interview Scenario Questions
• Second highest salary using subquery.
• Find and delete duplicate records.
• Difference between DELETE, TRUNCATE and DROP.
13. Performance & Optimization
• Use indexes, avoid SELECT *, analyze execution plan.