More Senior SQL Questions
More Senior SQL Questions
1. **Indexing**: Ensure that the columns used in JOIN, WHERE, and ORDER BY clauses are
indexed. Composite indexes (multi-column indexes) might also be necessary if queries filter
by multiple columns.
2. **Use of EXISTS vs. IN**: If you’re using `IN` with subqueries, consider replacing it with
`EXISTS`, as it generally performs better with large datasets.
3. **Avoid SELECT ***: Always specify the required columns instead of using `SELECT *` to
reduce the amount of data returned.
4. **Denormalization**: If your data model involves many JOINs between multiple tables,
consider denormalizing certain parts of the schema to reduce the number of JOINs required.
5. **Partitioning**: Partition large tables to speed up query execution on specific subsets of
data.
6. **Query Execution Plan**: Use the `EXPLAIN` or `EXPLAIN ANALYZE` command to
understand the query execution plan. It helps identify bottlenecks like full table scans,
missing indexes, or inefficient join operations.
7. **Temporary Tables**: For extremely complex queries, break them into smaller
subqueries, using temporary tables to store intermediate results.
What are the different types of indexes in SQL Server, and when would you use
each?
SQL Server provides several types of indexes, each with specific use cases:
1. **Clustered Index**: Stores data rows in a sorted order based on the indexed column.
2. **Non-Clustered Index**: A separate data structure that stores pointers to the actual data
rows.
3. **Unique Index**: Ensures that all values in the indexed column are unique.
4. **Filtered Index**: An index with a WHERE clause that covers a subset of rows in a table.
5. **Full-Text Index**: Used for full-text search capabilities.
6. **Columnstore Index**: Stores data in a columnar format, optimized for queries involving
large scans across columns.
- **MyISAM**: Does not support foreign keys or transactions, provides faster read
operations.
- **InnoDB**: Supports ACID properties, row-level locking, foreign keys, and crash recovery.
1. **Indexes**: Ensure that proper indexes are created on columns used in JOINs, WHERE,
and ORDER BY clauses.
2. **Query Refactoring**: Simplify the query by removing unnecessary subqueries or
combining queries.
3. **Analyze Execution Plan**: Use `EXPLAIN` to analyze the execution plan and identify
performance bottlenecks.
4. **Partitioning**: For very large tables, partition the data into smaller, more manageable
segments.
5. **Caching**: Use query caching for frequently run queries.
What is database normalization, and what are the different normal forms?
Database normalization is the process of organizing data to minimize redundancy and
improve data integrity. The most common normal forms are:
1. **First Normal Form (1NF)**: Ensures that the values in a table are atomic (no repeating
groups or arrays).
2. **Second Normal Form (2NF)**: Requires 1NF and ensures that all non-key attributes are
fully dependent on the primary key.
3. **Third Normal Form (3NF)**: Requires 2NF and removes transitive dependencies
(where non-key attributes depend on other non-key attributes).
4. **Boyce-Codd Normal Form (BCNF)**: A stricter version of 3NF, where every
determinant is a candidate key.
1. **Replication**: Use replication or log shipping to keep the new database up to date with
the old database.
2. **Partitioning the Data**: If applicable, partition the data to migrate smaller chunks at a
time.
3. **Rolling Migration**: Migrate a copy of the database, switch application traffic to the
new instance, and then sync the latest changes.
4. **Test Thoroughly**: Perform a dry-run migration to identify potential issues.
What are stored procedures, and what are their benefits in SQL?
Stored procedures are precompiled SQL code that can be executed as a single unit. They
offer several benefits:
1. **Performance**: Since stored procedures are precompiled, they execute faster than
dynamically generated SQL queries.
2. **Security**: Stored procedures allow you to abstract complex business logic and restrict
direct access to database tables.
3. **Reusability**: Stored procedures can be reused across multiple applications or queries.
4. **Maintainability**: Changes to business logic can be made in the stored procedure
without modifying the application code.
1. **Atomicity**: Ensures that a transaction is either fully completed or fully rolled back.
2. **Consistency**: Ensures that a transaction moves the database from one valid state to
another.
3. **Isolation**: Ensures that concurrent transactions do not interfere with each other.
4. **Durability**: Ensures that once a transaction is committed, it will be permanently
saved, even in case of a system failure.