Senior SQL Questions
Senior SQL Questions
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.