SQL Interview Mastery: 140+ Questions Covering Sharding, Indexing, and Partitioning Topics
1. What is SQL?
Answer:
SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases.
2. What are the types of SQL commands?
Answer:
SQL commands are categorized into:
- DDL (Data Definition Language): CREATE, ALTER, DROP.
- DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE.
- DCL (Data Control Language): GRANT, REVOKE.
- TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT.
3. What is a primary key?
Answer:
A primary key uniquely identifies each record in a table. It must have unique values and cannot contain NULL values.
Example:
CREATE TABLE Students ( ID INT PRIMARY KEY, Name VARCHAR(100) );
4. What is a foreign key?
Answer:
A foreign key is a column that creates a relationship between two tables by referencing the primary key of another table.
Example:
CREATE TABLE Orders ( OrderID INT PRIMARY KEY, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) );
5. What is a join in SQL?
Answer:
A join combines rows from two or more tables based on a related column between them. Types include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
6. What is an INNER JOIN?
Answer:
INNER JOIN returns records that have matching values in both tables.
Example:
SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
7. What is a LEFT JOIN?
Answer:
LEFT JOIN returns all records from the left table and matched records from the right table. If there is no match, NULL values are returned for right table columns.
Example:
SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
8. What is a RIGHT JOIN?
Answer:
RIGHT JOIN returns all records from the right table and matched records from the left table. If there is no match, NULL values are returned for left table columns.
9. What is a FULL OUTER JOIN?
Answer:
FULL OUTER JOIN returns all records when there is a match in either left or right table. Non-matching records will have NULL values in columns from the other table.
10. What is a WHERE clause?
Answer:
The WHERE clause filters records based on specified conditions.
Example:
SELECT * FROM Customers WHERE Country = 'Germany';
11. What is a GROUP BY clause?
Answer:
GROUP BY is used with aggregate functions to group the result set by one or more columns.
Example:
SELECT Country, COUNT(CustomerID) FROM Customers GROUP BY Country;
12. What is the difference between WHERE and HAVING?
Answer:
- WHERE: Filters records before grouping.
- HAVING: Filters records after grouping (used with aggregate functions).
Example:
SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
13. What is the ORDER BY clause?
Answer:
ORDER BY is used to sort the result set in ascending or descending order.
Example:
SELECT * FROM Customers ORDER BY CustomerName ASC;
14. What is an index in SQL?
Answer:
An index improves the speed of data retrieval operations on a database table. It can slow down INSERT, UPDATE, and DELETE operations.
Example:
CREATE INDEX idx_customername ON Customers(CustomerName);
15. What is a unique key?
Answer:
A unique key ensures all values in a column are distinct but allows NULL values (unlike primary keys).
Example:
CREATE TABLE Employees ( ID INT UNIQUE, Name VARCHAR(100) );
16. What is a composite key?
Answer:
A composite key is a combination of two or more columns used to uniquely identify a row in a table.
Example:
CREATE TABLE Orders ( OrderID INT, ProductID INT, PRIMARY KEY (OrderID, ProductID) );
17. What is normalization?
Answer:
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity.
18. Explain the First Normal Form (1NF).
Answer:
1NF requires that all columns in a table contain atomic (indivisible) values, and each record must be unique.
19. Explain the Second Normal Form (2NF).
Answer:
2NF ensures a table is in 1NF and that all non-key attributes are fully functionally dependent on the primary key.
20. Explain the Third Normal Form (3NF).
Answer:
3NF requires a table to be in 2NF and that all non-key attributes are not dependent on other non-key attributes (i.e., no transitive dependency).
21. What is denormalization?
Answer:
Denormalization is the process of combining tables to improve query performance by reducing the number of joins, which may introduce redundancy.
22. What are aggregate functions in SQL?
Answer:
Aggregate functions perform calculations on multiple rows and return a single result. Examples include COUNT(), SUM(), AVG(), MIN(), and MAX().
23. How do you find the number of rows in a table?
Answer:
You can use the COUNT() function.
Example:
SELECT COUNT(*) FROM Customers;
24. What is the difference between COUNT(*) and COUNT(column_name)?
Answer:
- COUNT(*) counts all rows, including rows with NULL values.
- COUNT(column_name) counts only non-NULL values in the specified column.
25. How do you find the maximum value in a column?
Answer:
Use the MAX() function.
Example:
SELECT MAX(Salary) FROM Employees;
26. What is a subquery?
Answer:
A subquery is a query within another query. It can return a single value or multiple rows and can be used in SELECT, INSERT, UPDATE, or DELETE statements.
Example:
SELECT * FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees);
27. What is a correlated subquery?
Answer:
A correlated subquery is a subquery that refers to columns from the outer query, executing for each row processed by the outer query.
28. What is the difference between EXISTS and IN?
Answer:
- IN: Checks if a value matches any value in a list or subquery.
- EXISTS: Checks if a subquery returns any rows.
Example:
SELECT * FROM Employees WHERE DepartmentID IN (1, 2, 3);
29. What is the difference between DELETE and TRUNCATE?
Answer:
- DELETE: Removes rows from a table based on a condition and can be rolled back.
- TRUNCATE: Deletes all rows from a table and cannot be rolled back.
30. What is the LIMIT clause in SQL?
Answer:
LIMIT specifies the maximum number of rows to return.
Example:
SELECT * FROM Customers LIMIT 10;
31. What is a stored procedure?
Answer:
A stored procedure is a set of SQL statements that can be stored in the database and executed repeatedly.
Example:
CREATE PROCEDURE GetEmployeeDetails() BEGIN SELECT * FROM Employees; END;
32. What is a trigger in SQL?
Answer:
A trigger is a set of actions that are automatically performed when a specified database event occurs (e.g., INSERT, UPDATE, DELETE).
33. What is a view in SQL?
Answer:
A view is a virtual table based on the result of a query. It simplifies complex queries and enhances security.
Example:
CREATE VIEW ActiveCustomers AS SELECT * FROM Customers WHERE Status = 'Active';
34. What are constraints in SQL?
Answer:
Constraints enforce rules at the table or column level. Common constraints include NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK.
35. What is the CHECK constraint?
Answer:
The CHECK constraint ensures that all values in a column satisfy a specific condition.
Example:
CREATE TABLE Employees ( ID INT, Salary INT, CHECK (Salary > 0) );
36. What is a transaction in SQL?
Answer:
A transaction is a sequence of SQL statements executed as a single unit. Transactions ensure ACID properties (Atomicity, Consistency, Isolation, Durability).
37. What is COMMIT in SQL?
Answer:
COMMIT saves all changes made during a transaction permanently to the database.
38. What is ROLLBACK in SQL?
Answer:
ROLLBACK undoes changes made during a transaction, reverting the database to its previous state.
39. What is a SAVEPOINT in SQL?
Answer:
SAVEPOINT allows setting a point within a transaction to which you can later roll back without affecting the entire transaction.
40. What is a cursor in SQL?
Answer:
A cursor allows row-by-row processing of query results. It is commonly used in stored procedures.
41. What is database indexing?
Answer:
An index is a database object that improves query performance by allowing faster data retrieval. It works similarly to the index in a book.
42. What is the difference between a clustered and non-clustered index?
Answer:
- Clustered index: Alters the physical order of the table and is faster for retrieval.
- Non-clustered index: Maintains a logical order, allowing multiple indexes on a table.
43. What are SQL wildcards?
Answer:
Wildcards are used with the LIKE operator to search for a specified pattern. % represents zero or more characters, and _ represents a single character.
44. What is the UNION operator?
Answer:
UNION combines the result sets of two or more SELECT queries, removing duplicates.
45. What is the INTERSECT operator?
Answer:
INTERSECT returns the common records between two SELECT queries.
46. What is the EXCEPT operator?
Answer:
EXCEPT returns rows from the first query that are not present in the second query.
47. What is SQL injection?
Answer:
SQL injection is a security vulnerability where an attacker inserts malicious SQL code into a query, potentially gaining unauthorized access to a database.
48. What are temporary tables in SQL?
Answer:
Temporary tables are created in a database for temporary data storage and are automatically deleted when the session ends.
49. What is a recursive query in SQL?
Answer:
A recursive query is a query that refers to itself to retrieve hierarchical or parent-child relationships within data.
50. What are materialized views?
Answer:
Materialized views store the result of a query physically and periodically update the data, unlike regular views, which are virtual and execute the query every time.
51. What is a database schema?
Answer:
A database schema is the structure that defines how data is organized in a database, including tables, fields, relationships, and constraints.
52. What is a data type in SQL?
Answer:
A data type defines the kind of data that can be stored in a column, such as INT, VARCHAR, DATE, and BOOLEAN.
53. What is the difference between CHAR and VARCHAR?
Answer:
- CHAR: Fixed-length string.
- VARCHAR: Variable-length string.
54. What is a database trigger?
Answer:
A database trigger is a set of SQL statements that automatically execute in response to a specific event in a database.
55. What is the COALESCE function?
Answer:
COALESCE returns the first non-null value from a list of expressions.
Example:
SELECT COALESCE(NULL, NULL, 'Default Value'); -- Returns 'Default Value'
56. What is the CASE statement in SQL?
Answer:
The CASE statement provides conditional logic to SQL queries, allowing different outcomes based on conditions.
Example:
SELECT Name, CASE WHEN Age < 18 THEN 'Minor' ELSE 'Adult' END AS AgeGroup FROM Users;
57. What is a data warehouse?
Answer:
A data warehouse is a centralized repository for storing, managing, and analyzing large volumes of data from various sources.
58. What is OLAP?
Answer:
OLAP (Online Analytical Processing) allows users to analyze data from multiple perspectives, facilitating complex queries and analysis.
59. What is OLTP?
Answer:
OLTP (Online Transaction Processing) focuses on managing and facilitating transaction-oriented applications, typically involving large volumes of short online transactions.
60. What is a database management system (DBMS)?
Answer:
A DBMS is software that enables users to create, manage, and manipulate databases. It provides functionalities like data storage, retrieval, and administration.
61. What is a logical data model?
Answer:
A logical data model outlines the structure of data elements and relationships within a database without considering how they will be physically implemented.
62. What is a physical data model?
Answer:
A physical data model represents the physical structure of a database, including tables, indexes, and constraints, along with their storage details.
63. What is a database migration?
Answer:
Database migration involves moving data from one database system to another or transferring data to a new database structure.
64. What is data replication?
Answer:
Data replication involves copying and maintaining database objects in multiple locations to enhance availability and performance.
65. What is the difference between a database and a spreadsheet?
Answer:
- Database: Designed for complex querying and data management; supports multiple users.
- Spreadsheet: Designed for data analysis and simple calculations; typically single-user.
66. What is data integrity?
Answer:
Data integrity refers to the accuracy, consistency, and reliability of data throughout its lifecycle.
67. What is a data model?
Answer:
A data model defines how data is structured, organized, and accessed within a database.
68. What is a field in a database?
Answer:
A field is a single piece of data stored in a database table, representing an attribute of a record.
69. What is a record in a database?
Answer:
A record is a complete set of fields (data) in a table, representing a single entity.
70. What is data abstraction?
Answer:
Data abstraction is the process of hiding complex implementation details and showing only essential features of data management.
71. What is data redundancy?
Answer:
Data redundancy occurs when the same piece of data is stored in multiple locations, leading to inconsistencies and increased storage costs.
72. What are the advantages of using SQL?
Answer:
- Standardized language for database management.
- Supports complex queries and transactions.
- Ensures data integrity and security.
- Provides a robust framework for data analysis.
73. What is a join condition?
Answer:
A join condition specifies the criteria for joining tables in a SQL query, typically using foreign keys.
74. What is the difference between a database and a file system?
Answer:
- Database: Structured, supports complex queries, and ensures data integrity.
- File System: Unstructured, less efficient for large datasets, and lacks integrity constraints.
75. What is a namespace in SQL?
Answer:
A namespace is a container for objects, allowing the same object names in different contexts (e.g., different schemas).
76. What is the RANK() function?
Answer:
RANK() assigns a unique rank number to each row within a partition, with gaps in ranking for ties.
Example:
SELECT Name, Salary, RANK() OVER (ORDER BY Salary DESC) AS Rank FROM Employees;
77. What is the DENSE_RANK() function?
Answer:
DENSE_RANK() assigns ranks without gaps, even for tied rows.
Example:
SELECT Name, Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) AS Rank FROM Employees;
78. What is the NTILE() function?
Answer:
NTILE() divides the result set into a specified number of groups and assigns a unique group number to each row.
Example:
SELECT Name, Salary, NTILE(4) OVER (ORDER BY Salary) AS Quartile FROM Employees;
79. What is the ROW_NUMBER() function?
Answer:
ROW_NUMBER() assigns a unique sequential integer to rows within a partition of a result set.
Example:
SELECT Name, Salary, ROW_NUMBER() OVER (ORDER BY Salary) AS RowNum FROM Employees;
80. What is the difference between INNER JOIN and OUTER JOIN?
Answer:
- INNER JOIN: Returns only the matching rows from both tables.
- OUTER JOIN: Returns matching rows and the remaining rows from one or both tables, including NULLs where there are no matches.
81. What are stored procedures?
Answer:
Stored procedures are precompiled SQL statements stored in the database that can be executed with a single call, improving performance and security.
82. What is the difference between stored procedures and functions?
Answer:
- Stored Procedures: Can perform actions (INSERT, UPDATE) and do not return a value.
- Functions: Must return a value and are typically used for calculations or data transformations.
83. What are database views?
Answer:
Views are virtual tables that represent the result of a stored query, providing a simplified or secured way to access data.
84. What is normalization?
Answer:
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity.
85. What are the types of normalization?
Answer:
The common types of normalization are:
- First Normal Form (1NF): Eliminates duplicate columns and ensures each column contains atomic values.
- Second Normal Form (2NF): Removes partial dependencies; all non-key attributes must depend on the entire primary key.
- Third Normal Form (3NF): Eliminates transitive dependencies; non-key attributes should not depend on other non-key attributes.
86. What is denormalization?
Answer:
Denormalization is the process of deliberately introducing redundancy into a database by combining tables to improve read performance.
87. What is an entity-relationship (ER) model?
Answer:
An ER model is a diagram that visually represents the data structure and relationships within a database, showing entities, attributes, and relationships.
88. What is a foreign key constraint?
Answer:
A foreign key constraint enforces a link between the data in two tables, ensuring referential integrity by restricting the values in one table to those present in another.
89. What is a primary key constraint?
Answer:
A primary key constraint uniquely identifies each record in a table and ensures that no duplicate or NULL values exist in the primary key column.
90. What is the difference between a view and a table?
Answer:
- View: Virtual, based on a query, does not store data.
- Table: Physical, stores data directly.
91. What is a database console?
Answer:
A database console is a command-line interface for interacting with a database, allowing users to execute SQL commands and manage database objects.
92. What is an aggregate function in SQL?
Answer:
An aggregate function performs a calculation on a set of values and returns a single value. Examples include SUM(), AVG(), COUNT(), MIN(), and MAX().
93. What is a subquery?
Answer:
A subquery is a query nested within another query, allowing complex filtering and data retrieval.
94. What is a cross join?
Answer:
A cross join produces the Cartesian product of two tables, returning all possible combinations of rows.
95. What is a self join?
Answer:
A self join is a join of a table to itself, allowing comparisons between rows within the same table.
96. What is the LIMIT clause?
Answer:
The LIMIT clause restricts the number of records returned in a query result.
97. What is a database schema diagram?
Answer:
A database schema diagram visually represents the structure of a database, including tables, columns, relationships, and constraints.
98. What is a SQL function?
Answer:
A SQL function is a set of SQL statements that perform a specific task and can return a single value or a table.
99. What is a temporary table?
Answer:
A temporary table is a table that is created to store data temporarily during a session and is automatically dropped at the end of the session.
100. What is a data mart?
Answer:
A data mart is a subset of a data warehouse, focused on a specific business area or department, providing relevant data for analysis and reporting.
101. What is a LEFT JOIN?
Answer:
A LEFT JOIN returns all records from the left table and the matched records from the right table. If there is no match, NULL values are returned for columns from the right table.
Example:
SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
102. What is a RIGHT JOIN?
Answer:
A RIGHT JOIN returns all records from the right table and the matched records from the left table. If there is no match, NULL values are returned for columns from the left table.
Example:
SELECT Customers.CustomerName, Orders.OrderID FROM Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
103. What is a FULL OUTER JOIN?
Answer:
A FULL OUTER JOIN returns all records when there is a match in either the left or right table. It includes NULL values for non-matching rows from both tables.
Example:
SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
104. What is a CROSS JOIN?
Answer:
A CROSS JOIN produces a Cartesian product of the two tables involved, returning all possible combinations of rows.
Example:
SELECT Customers.CustomerName, Products.ProductName FROM Customers CROSS JOIN Products;
105. What is a SELF JOIN?
Answer:
A SELF JOIN is a join where a table is joined with itself to compare rows within the same table.
Example:
SELECT A.EmployeeID, A.Name, B.Name AS ManagerName FROM Employees A, Employees B WHERE A.ManagerID = B.EmployeeID;
106. What is the difference between INNER JOIN and OUTER JOIN?
Answer:
- INNER JOIN: Returns only the rows with matching values in both tables.
- OUTER JOIN: Returns all rows from one table and the matched rows from the other, including NULLs for non-matching rows.
107. What is the use of the ON clause in joins?
Answer:
The ON clause specifies the condition used to join two tables, typically by matching primary and foreign keys.
Example:
SELECT Employees.Name, Departments.DepartmentName FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
108. What is a NATURAL JOIN?
Answer:
A NATURAL JOIN automatically joins tables based on columns with the same name and datatype, eliminating the need to specify join conditions.
Example:
SELECT * FROM Employees NATURAL JOIN Departments;
109. What is a LEFT JOIN vs. a RIGHT JOIN?
Answer:
- LEFT JOIN: Returns all records from the left table, and matched records from the right.
- RIGHT JOIN: Returns all records from the right table, and matched records from the left.
Example:
-- LEFT JOIN Example SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID; -- RIGHT JOIN Example SELECT Customers.CustomerName, Orders.OrderID FROM Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
110. What are join aliases and why are they used?
Answer:
Join aliases are shorthand names assigned to tables in a query, improving readability and simplifying complex queries. They are particularly useful when joining multiple tables.
Example:
SELECT C.CustomerName, O.OrderID FROM Customers AS C INNER JOIN Orders AS O ON C.CustomerID = O.CustomerID;
111. What is a stored procedure?
Answer:
A stored procedure is a precompiled collection of SQL statements that can be executed as a single unit, allowing for improved performance, security, and reusability.
112. How do you create a stored procedure?
Answer:
You can create a stored procedure using the CREATE PROCEDURE statement, followed by the procedure name and the SQL statements.
Example:
CREATE PROCEDURE GetEmployeeCount AS BEGIN SELECT COUNT(*) FROM Employees; END;
113. How do you execute a stored procedure?
Answer:
You can execute a stored procedure using the EXEC or EXECUTE statement.
Example:
EXEC GetEmployeeCount;
114. What are input parameters in stored procedures?
Answer:
Input parameters allow users to pass values into a stored procedure, which can be used within the SQL statements.
Example:
CREATE PROCEDURE GetEmployeeByID @EmployeeID INT AS BEGIN SELECT * FROM Employees WHERE EmployeeID = @EmployeeID; END;
115. What are output parameters in stored procedures?
Answer:
Output parameters are used to return values from a stored procedure back to the calling environment.
Example:
CREATE PROCEDURE GetEmployeeCountByDepartment @DepartmentID INT, @EmployeeCount INT OUTPUT AS BEGIN SELECT @EmployeeCount = COUNT(*) FROM Employees WHERE DepartmentID = @DepartmentID; END;
116. What is the difference between a stored procedure and a function?
Answer:
- Stored Procedures: Can perform actions (INSERT, UPDATE) and do not return a value directly.
- Functions: Must return a value and are typically used for calculations or data transformations.
117. How can you modify an existing stored procedure?
Answer:
You can modify an existing stored procedure using the ALTER PROCEDURE statement.
Example:
ALTER PROCEDURE GetEmployeeCount AS BEGIN SELECT COUNT(*) FROM Employees WHERE IsActive = 1; END;
118. What is a transaction in a stored procedure?
Answer:
A transaction in a stored procedure is a sequence of operations that are executed as a single unit. If one operation fails, the entire transaction can be rolled back to maintain data integrity.
Example:
CREATE PROCEDURE UpdateEmployeeSalary @EmployeeID INT, @NewSalary DECIMAL(10,2) AS BEGIN BEGIN TRANSACTION; BEGIN TRY UPDATE Employees SET Salary = @NewSalary WHERE EmployeeID = @EmployeeID; COMMIT TRANSACTION; END TRY BEGIN CATCH ROLLBACK TRANSACTION; -- Handle error END CATCH END;
119. Can a stored procedure call another stored procedure?
Answer:
Yes, a stored procedure can call another stored procedure, allowing for modular programming.
Example:
CREATE PROCEDURE GetEmployeeDetails @EmployeeID INT AS BEGIN EXEC GetEmployeeByID @EmployeeID; EXEC GetEmployeeCount; END;
120. What are the advantages of using stored procedures?
Answer:
Advantages of using stored procedures include:
- Improved performance (precompiled).
- Enhanced security (permissions can be managed).
- Better code reusability and maintainability.
- Simplified complex operations into single calls.
121. What is database sharding?
Answer:
Database sharding is a method of horizontal partitioning where a large database is divided into smaller, more manageable pieces called shards, allowing for improved performance and scalability.
122. What are the benefits of sharding?
Answer:
Benefits of sharding include improved performance, reduced load on individual servers, increased scalability, and easier maintenance as each shard can be managed independently.
123. What is indexing in databases?
Answer:
Indexing is a database optimization technique that improves the speed of data retrieval operations on a database table by creating a data structure (index) that allows for quick lookups.
124. What types of indexes are there?
Answer:
Common types of indexes include:
- Single-column index: An index on a single column.
- Composite index: An index on multiple columns.
- Unique index: Ensures all values in the index are unique.
- Full-text index: Optimizes text searching.
- Clustered index: Determines the physical order of data in a table.
- Non-clustered index: A separate structure from the data that points to the location of the data.
125. What is a clustered index?
Answer:
A clustered index sorts and stores the data rows of the table based on the key values. Each table can have only one clustered index because the data can only be sorted in one order.
126. What is a non-clustered index?
Answer:
A non-clustered index is a separate structure from the data that maintains a pointer to the actual data. A table can have multiple non-clustered indexes.
127. How does indexing improve query performance?
Answer:
Indexing reduces the amount of data scanned during queries by allowing the database to quickly locate the rows that meet search criteria, thus speeding up data retrieval.
128. What are the disadvantages of indexing?
Answer:
Disadvantages of indexing include:
- Increased storage space usage.
- Slower data modification operations (INSERT, UPDATE, DELETE) due to index maintenance.
- Potential for decreased performance if indexes are not used properly.
129. What is partitioning in databases?
Answer:
Partitioning is the process of dividing a database table into smaller, more manageable pieces called partitions, which can improve performance, maintenance, and manageability.
130. What are the types of partitioning?
Answer:
Common types of partitioning include:
- Horizontal partitioning: Divides a table into rows (e.g., by range or list).
- Vertical partitioning: Divides a table into columns (e.g., separating frequently accessed columns).
- Functional partitioning: Distributes data across multiple tables based on business logic.
131. What is range partitioning?
Answer:
Range partitioning divides data into partitions based on specified ranges of values. Each partition holds rows that fall within a defined range.
Example:
CREATE TABLE Sales ( SaleID INT, SaleDate DATE, Amount DECIMAL(10,2) ) PARTITION BY RANGE (YEAR(SaleDate)) ( PARTITION p0 VALUES LESS THAN (2020), PARTITION p1 VALUES LESS THAN (2021), PARTITION p2 VALUES LESS THAN (2022) );
132. What is list partitioning?
Answer:
List partitioning divides data into partitions based on a list of values. Each partition contains rows with specific values defined in the list.
Example:
CREATE TABLE Employees ( EmployeeID INT, Department VARCHAR(50) ) PARTITION BY LIST (Department) ( PARTITION p1 VALUES IN ('HR', 'Finance'), PARTITION p2 VALUES IN ('IT', 'Sales') );
133. What is hash partitioning?
Answer:
Hash partitioning distributes data across multiple partitions based on a hashing algorithm applied to a specified column, ensuring a more even distribution.
Example:
CREATE TABLE Orders ( OrderID INT, CustomerID INT ) PARTITION BY HASH (CustomerID) PARTITIONS 4;
134. What is composite partitioning?
Answer:
Composite partitioning combines two or more partitioning methods, such as range and hash partitioning, to allow for more complex data distribution and access patterns.
135. What are the advantages of partitioning?
Answer:
Advantages of partitioning include:
- Improved query performance by reducing the amount of data scanned.
- Easier data management (e.g., archiving, purging).
- Enhanced maintenance (e.g., rebuilding indexes on individual partitions).
136. How does partitioning affect query performance?
Answer:
Partitioning can improve query performance by allowing the database to scan only relevant partitions rather than the entire table, leading to faster data retrieval.
137. What is the role of indexes in partitioned tables?
Answer:
Indexes in partitioned tables work similarly to non-partitioned tables, allowing for efficient data retrieval within each partition. However, they can also be partitioned to align with the table's partitioning scheme.
138. Can you create indexes on partitioned tables?
Answer:
Yes, you can create indexes on partitioned tables, and the indexing strategy can be tailored to each partition to optimize performance based on data distribution.
139. What is a bitmap index?
Answer:
A bitmap index uses bitmaps (arrays of bits) to represent the existence of values in a column, making it efficient for columns with low cardinality (few distinct values).
140. What is an index scan vs. an index seek?
Answer:
- Index Scan: Reads all the entries in an index, which can be slower if many rows are returned.
- Index Seek: Quickly locates the specific entries based on search criteria, improving performance.
141. What is a covering index?
Answer:
A covering index includes all the columns needed to satisfy a query, allowing the database to retrieve the required data from the index alone without accessing the actual table.
142. What is an indexed view?
Answer:
An indexed view is a view that has a unique clustered index created on it, improving performance for complex queries by materializing the view's data.
143. How can you determine if an index is being used?
Answer:
You can determine if an index is being used by analyzing query execution plans, using database performance monitoring tools, or checking statistics on index usage.
144. What is a deadlock, and how can it be avoided?
Answer:
A deadlock occurs when two or more transactions block each other, waiting for resources held by each other. It can be avoided by:
- Implementing a consistent locking order.
- Using shorter transactions.
- Employing timeout settings.
145. What are statistics in the context of indexing?
Answer:
Statistics provide the database engine with information about the distribution of data within a table or index, helping the query optimizer make informed decisions about query execution plans.
146. How can you optimize index usage?
Answer:
You can optimize index usage by:
- Regularly reviewing and updating indexes based on query patterns.
- Removing unused indexes to reduce overhead.
- Creating composite indexes for frequently used column combinations.
147. What are index fragmentation and its impact?
Answer:
Index fragmentation occurs when the logical order of the index does not match the physical order of the data, leading to inefficiencies. It can impact query performance and requires periodic maintenance (e.g., rebuilding or reorganizing indexes).