0% found this document useful (0 votes)
1 views4 pages

SQL Interview Questions and Answers

The document provides a comprehensive list of SQL interview questions and answers, covering fundamental concepts such as SQL commands, keys, joins, and normalization. It also addresses advanced topics like stored procedures, triggers, and query optimization. Overall, it serves as a useful resource for preparing for SQL-related interviews.

Uploaded by

priyanshi100100
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
1 views4 pages

SQL Interview Questions and Answers

The document provides a comprehensive list of SQL interview questions and answers, covering fundamental concepts such as SQL commands, keys, joins, and normalization. It also addresses advanced topics like stored procedures, triggers, and query optimization. Overall, it serves as a useful resource for preparing for SQL-related interviews.

Uploaded by

priyanshi100100
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

SQL Interview Questions and Answers

1. What is SQL?

SQL (Structured Query Language) is used to manage and manipulate relational databases.

2. What are the different types of SQL commands?

 DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE


 DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
 DCL (Data Control Language): GRANT, REVOKE
 TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT

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

 DELETE: Removes specific rows, can be rolled back.


 TRUNCATE: Removes all rows, cannot be rolled back.
 DROP: Deletes the table structure along with data.

4. What is a Primary Key?

A Primary Key uniquely identifies each record in a table. It cannot have NULL values.

5. What is the difference between Primary Key and Unique Key?

 Primary Key: Uniquely identifies records, does not allow NULL values.
 Unique Key: Ensures uniqueness but allows NULL values.

6. What is a Foreign Key?

A Foreign Key is a field that establishes a relationship between two tables by referencing a
Primary Key in another table.

7. What is an Index in SQL?

An Index improves the speed of data retrieval but may slow down INSERT, UPDATE, and
DELETE operations.

8. What are the types of Indexes?

 Clustered Index: Sorts and stores data physically.


 Non-Clustered Index: Maintains a separate structure from data storage.

9. What is the difference between HAVING and WHERE?

 WHERE: Filters rows before aggregation.


 HAVING: Filters groups after aggregation.
10. What is the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL
JOIN?

 INNER JOIN: Returns matching records from both tables.


 LEFT JOIN: Returns all records from the left table and matching records from the
right table.
 RIGHT JOIN: Returns all records from the right table and matching records from the
left table.
 FULL JOIN: Returns all records when there is a match in either table.

11. What is a Self Join?

A Self Join is when a table joins itself using an alias.

12. What is the difference between UNION and UNION ALL?

 UNION: Combines results and removes duplicates.


 UNION ALL: Combines results including duplicates.

13. What is a Subquery?

A Subquery is a query inside another query that provides intermediate results.

14. What are Stored Procedures?

Stored Procedures are precompiled SQL statements that improve performance and security.

15. What are Triggers in SQL?

Triggers are automatic actions executed before or after an INSERT, UPDATE, or DELETE
operation.

16. What is Normalization?

Normalization reduces redundancy by organizing data into multiple related tables.

17. What are the types of Normal Forms?

1. 1NF: Eliminates duplicate columns.


2. 2NF: Ensures every non-key column is functionally dependent on the Primary Key.
3. 3NF: Eliminates transitive dependency.
4. BCNF: A stricter version of 3NF.

18. What is Denormalization?

Denormalization is the process of adding redundancy to improve performance.

19. What are Views in SQL?


Views are virtual tables based on SQL queries.

20. What is ACID in SQL?

ACID (Atomicity, Consistency, Isolation, Durability) ensures reliable transactions.

21. What is the difference between CHAR and VARCHAR?

 CHAR: Fixed-length storage.


 VARCHAR: Variable-length storage.

22. What is the difference between COUNT(*), COUNT(column), and COUNT(DISTINCT


column)?

 COUNT(*): Counts all rows.


 COUNT(column): Counts non-null values.
 COUNT(DISTINCT column): Counts unique non-null values.

23. What is a Cursor in SQL?

A Cursor is used to iterate over query results row by row.

24. What is a Common Table Expression (CTE)?

A CTE is a temporary result set within a SQL query, improving readability.

25. What is a Window Function?

Window functions perform calculations across a set of table rows related to the current row.

26. How to find the second-highest salary in SQL?


SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM
employees);

27. What is a Deadlock in SQL?

A Deadlock occurs when two transactions block each other while waiting for a resource.

28. What is an Optimized Query?

An optimized query improves performance by using indexes, avoiding unnecessary joins, and
using efficient functions.

29. How to improve SQL query performance?

 Use proper indexing.


 Optimize joins.
 Avoid SELECT *.
 Use appropriate data types.
 Use query caching.

30. How to find duplicate records in a table?


SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING
COUNT(*) > 1;

These questions cover fundamental and advanced SQL concepts commonly asked in
interviews.

You might also like