-
What is SQL Server?
- SQL Server is a relational database management system (RDBMS) developed by Microsoft. It is used to store and retrieve data as requested by other software applications.
-
What are the different editions of SQL Server?
- SQL Server comes in several editions including Enterprise, Standard, Web, Developer, and Express editions. Each edition varies in terms of features, scalability, and pricing.
-
What are the differences between clustered and non-clustered indexes?
- Clustered Index: Physically orders the data rows in the table based on the index key. There can only be one clustered index per table.
- Non-clustered Index: Stores a copy of the indexed columns with a pointer to the corresponding data rows in a separate location from the table data.
-
What is a stored procedure?
- A stored procedure is a precompiled collection of SQL statements and procedural logic that is stored in the database and can be executed by calling the procedure name.
-
Explain the difference between DELETE and TRUNCATE commands.
- DELETE: Removes rows from a table based on a condition. It generates a transaction log for each deleted row.
- TRUNCATE: Removes all rows from a table without logging individual row deletions. It resets identity columns and is faster than DELETE for large tables.
-
What are the ACID properties in a database?
- Atomicity: Ensures that transactions are treated as a single unit of work, either fully completed or fully rolled back.
- Consistency: Ensures that data meets all integrity constraints after a transaction completes.
- Isolation: Ensures that transactions operate independently of each other.
- Durability: Ensures that committed transactions are permanently saved and not lost due to system failures.
-
What is a foreign key constraint?
- A foreign key constraint is a rule that enforces referential integrity between two tables. It ensures that values in a column (or columns) of one table match values in another table's primary key or unique key column(s).
-
Explain the difference between a view and a table.
- Table: A basic unit of storage in a relational database that contains rows and columns.
- View: A virtual table created by a query that can be used like a table. It does not store data physically but retrieves it dynamically when queried.
-
How do you handle transactions in SQL Server?
- Transactions in SQL Server are managed using
BEGIN TRANSACTION, COMMIT, and ROLLBACK statements to ensure data consistency and integrity.
-
What are triggers?
- Triggers are special types of stored procedures that automatically execute in response to specific events (e.g., INSERT, UPDATE, DELETE) on a table. They are used to enforce business rules or audit changes.
-
Explain the concept of normalization in databases.
- Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It involves dividing large tables into smaller tables and defining relationships between them.
-
What is a deadlock? How can you prevent it?
- A deadlock occurs when two or more transactions are unable to proceed because each holds a lock on a resource that the other transaction requires. Deadlocks can be prevented by ensuring transactions access resources in the same order, using shorter transactions, or using deadlock detection and resolution mechanisms.
-
How do you optimize SQL queries for better performance?
- Optimization techniques include using indexes appropriately, avoiding unnecessary joins, optimizing WHERE clauses, using stored procedures, and analyzing query execution plans.
-
What are Common Table Expressions (CTEs)?
- CTEs are temporary result sets that are defined within the execution scope of a single
SELECT, INSERT, UPDATE, or DELETE statement. They can be referenced multiple times in the same query.
-
Explain the difference between UNION and UNION ALL.
- UNION: Combines the result sets of two or more SELECT statements and removes duplicate rows.
- UNION ALL: Combines the result sets of two or more SELECT statements but includes all rows, including duplicates.
-
How do you backup and restore databases in SQL Server?
- Use
BACKUP DATABASE to create full or differential backups of databases and RESTORE DATABASE to restore backups from disk.
-
What are the different types of indexes in SQL Server? Explain when you would use each type.
- Clustered Index: Physically orders the data rows in the table based on the index key. Ideal for columns that are frequently queried with range queries.
- Non-clustered Index: Contains a sorted list of key values and pointers to the data rows. Useful for columns frequently used in JOIN, WHERE, and ORDER BY clauses.
- Filtered Index: Index created on a subset of rows in a table that meets a specific condition. Useful for queries that access a well-defined subset of data.
- Columnstore Index: Optimized for data warehousing scenarios with large amounts of data. Stores data in columns rather than rows for better compression and query performance.
-
What is SQL injection? How can you prevent it in SQL Server?
- SQL injection is a security vulnerability that allows an attacker to manipulate SQL queries by injecting malicious SQL code. Prevent it by using parameterized queries or stored procedures instead of concatenating user input directly into SQL statements.
-
Explain the different types of joins in SQL Server. Provide examples.
- INNER JOIN: Returns rows when there is a match in both tables.
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
- LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and matching rows from the right table.
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
- RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table and matching rows from the left table.
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
- FULL JOIN (or FULL OUTER JOIN): Returns all rows when there is a match in either left or right table.
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;