BlogInterview QuestionsSQL Interview Question

Top 20 SQL Interview Questions and Answers

Top 20 SQL Interview Questions and Answers

Crack your SQL interview with these Top 20 SQL Interview Questions and Answers. Includes detailed explanations, examples, and key differences for freshers and experienced candidates.


🔹 1. What is SQL?

Answer:
SQL (Structured Query Language) is a standard language used to manage and manipulate databases.

Example:

SELECT * FROM employees;

🔹 2. What is the difference between DELETE, TRUNCATE, and DROP?

CommandDescriptionRollback PossibleAffects Structure
DELETEDeletes selected rowsYesNo
TRUNCATEDeletes all rows quicklyNoNo
DROPDeletes the entire tableNoYes

🔹 3. What are different types of SQL statements?

Answer:

  • DDL – Data Definition Language (CREATE, ALTER, DROP)
  • DML – Data Manipulation Language (INSERT, UPDATE, DELETE)
  • DQL – Data Query Language (SELECT)
  • DCL – Data Control Language (GRANT, REVOKE)
  • TCL – Transaction Control Language (COMMIT, ROLLBACK)

Top 20 SQL Interview Questions and Answers

🔹 4. What is a primary key?

Answer:
A primary key is a column (or set of columns) that uniquely identifies each row in a table.

Example:

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(50)
);

🔹 5. What is a foreign key?

Answer:
A foreign key in one table points to a primary key in another table to establish a relationship.

Example:

FOREIGN KEY (student_id) REFERENCES students(student_id)

🔹 6. What is the difference between WHERE and HAVING?

ClauseUse CaseWorks On
WHEREFilters rows before groupingRows
HAVINGFilters after GROUP BYAggregated data

Example:

SELECT dept, COUNT(*) FROM employees
GROUP BY dept
HAVING COUNT(*) > 5;

Top 20 SQL Interview Questions and Answers

🔹 7. What are JOINS in SQL?

Answer:
JOINS combine rows from two or more tables.

Join TypeDescription
INNER JOINMatches rows in both tables
LEFT JOINAll rows from left + matched from right
RIGHT JOINAll rows from right + matched from left
FULL JOINAll matched + unmatched rows from both

Example:

SELECT e.name, d.name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;

🔹 8. What is the difference between UNION and UNION ALL?

KeywordDuplicatesFaster
UNIONRemovesNo
UNION ALLKeepsYes

🔹 9. What is normalization?

Answer:
Normalization is a process to reduce data redundancy and improve data integrity.

Normal Forms:

  • 1NF: Atomic columns
  • 2NF: No partial dependency
  • 3NF: No transitive dependency

Top 20 SQL Interview Questions and Answers

🔹 10. What is a subquery?

Answer:
A subquery is a query nested inside another query.

Example:

SELECT name FROM students
WHERE id IN (SELECT student_id FROM enrollments);

🔹 11. What is the difference between IN and EXISTS?

  • IN: Checks values in a list or subquery.
  • EXISTS: Checks if subquery returns at least one row.

🔹 12. What is an index?

Answer:
An index improves query performance by allowing faster search and retrieval.

Example:

CREATE INDEX idx_name ON employees(name);

🔹 13. What is a view?

Answer:
A view is a virtual table based on a SQL query.

Example:

CREATE VIEW active_employees AS
SELECT * FROM employees WHERE status = 'active';

Top 20 SQL Interview Questions and Answers

🔹 14. What is a stored procedure?

Answer:
A stored procedure is a precompiled group of SQL statements.

Example:

CREATE PROCEDURE GetEmployees()
AS
BEGIN
    SELECT * FROM employees;
END;

🔹 15. What is the difference between CHAR and VARCHAR?

TypeFixed/VariableStorage
CHAR(n)Fixed lengthAlways n bytes
VARCHAR(n)Variable lengthUp to n bytes

🔹 16. What are aggregate functions in SQL?

Answer:
Aggregate functions perform calculations on multiple rows.

FunctionPurpose
SUM()Adds values
AVG()Average
COUNT()Count rows
MAX()Maximum value
MIN()Minimum value

🔹 17. What is a constraint?

Answer:
Constraints enforce rules on data columns.

  • NOT NULL
  • UNIQUE
  • PRIMARY KEY
  • FOREIGN KEY
  • CHECK
  • DEFAULT

🔹 18. What is the difference between BETWEEN and IN?

  • BETWEEN checks for a range.
  • IN checks for specific values.

Example:

-- BETWEEN
SELECT * FROM students WHERE age BETWEEN 18 AND 25;

-- IN
SELECT * FROM students WHERE city IN ('Delhi', 'Mumbai');

Top 20 SQL Interview Questions and Answers

🔹 19. What is a transaction in SQL?

Answer:
A transaction is a unit of work that is either fully completed or fully failed.

Commands:

  • BEGIN TRANSACTION
  • COMMIT
  • ROLLBACK

🔹 20. How do you optimize a slow SQL query?

Answer:

  • Use indexes
  • Avoid SELECT *
  • Use JOIN properly
  • Avoid unnecessary subqueries
  • Analyze with EXPLAIN or QUERY PLAN

Top 100 PHP Interview Questions and Answers
Top 20 SQL Interview Questions and Answers

Related posts

The AI-Powered Future of Coding Is Near

Engineer Robin

Python Reigns as the Most Popular Language on GitHub

Engineer Robin

Happy Birthday Third Part

Engineer Robin

Leave a Comment