Advanced Interview Questions
Comprehensive advanced interview questions and answers for SQL. Prepare for your next job interview with expert guidance.
Questions Overview
1. What are stored procedures in SQL?
AdvancedA stored procedure is a set of SQL statements that can be saved and reused. They encapsulate business logic within the database, improve performance, and reduce network traffic. You can call them with parameters to execute complex operations. For example: ```sql CREATE PROCEDURE GetEmployeeDetails(@id INT) AS BEGIN SELECT * FROM employees WHERE id = @id; END; ```
2. What is a transaction in SQL?
AdvancedA transaction is a sequence of SQL operations executed as a single unit of work. Transactions ensure ACID properties (Atomicity, Consistency, Isolation, Durability), meaning that either all operations in a transaction are completed successfully, or none are. You can control transactions with `BEGIN`, `COMMIT`, and `ROLLBACK` commands.
3. What is normalization in SQL?
AdvancedNormalization is the process of organizing a database to reduce redundancy and improve data integrity. It involves dividing large tables into smaller tables and defining relationships between them. The normal forms (1NF, 2NF, 3NF, etc.) are guidelines for designing a normalized database.
4. What is an index in SQL, and why is it used?
ModerateAn index in SQL is a database object that improves the speed of data retrieval operations on a table by providing a fast lookup mechanism. Indexes are typically created on columns that are frequently queried. However, they can also slow down `INSERT` and `UPDATE` operations.
5. What is a foreign key in SQL?
ModerateA foreign key is a column (or a set of columns) in one table that references the primary key in another table. It establishes a relationship between the two tables, ensuring referential integrity. For example, the `department_id` in an `employees` table might be a foreign key referencing the `id` column in a `departments` table.