0% found this document useful (0 votes)
23 views4 pages

More Senior SQL Questions

Uploaded by

pbiegonitc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

More Senior SQL Questions

Uploaded by

pbiegonitc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Senior-Level SQL, SQL Server, and

MySQL Interview Questions and


Answers
Explain how you would optimize a complex query that involves multiple JOINs
and large datasets.
Optimizing a complex query with multiple JOINs involves several strategies:

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.

How do window functions work in SQL? Can you provide an example?


Window functions perform calculations across a set of rows that are related to the current
row, but unlike aggregate functions, they do not collapse the result set. A window function
uses the `OVER()` clause, which defines the partitioning and ordering of rows.

Common window functions include `ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`,


`LEAD()`, `LAG()`, and `SUM()`.

Example: Calculate the running total of sales for each salesperson:


```sql
SELECT
SalesPersonID,
OrderID,
OrderAmount,
SUM(OrderAmount) OVER (PARTITION BY SalesPersonID ORDER BY OrderDate) AS
RunningTotal
FROM
Orders
ORDER BY
SalesPersonID, OrderDate;
```
In this example, the `SUM` function calculates a running total of sales (`OrderAmount`) for
each `SalesPersonID`, ordered by `OrderDate`.

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.

What is the difference between `ROWLOCK`, `PAGLOCK`, and `TABLOCK` in SQL


Server?
These are lock hints in SQL Server that specify the level of locking used during transactions:

1. **ROWLOCK**: Locks individual rows during the transaction.


2. **PAGLOCK**: Locks an entire page (typically 8 KB of data) in the table.
3. **TABLOCK**: Locks the entire table, preventing access until the lock is released.

What is the difference between MyISAM and InnoDB storage engines in


MySQL?
MyISAM and InnoDB are two different storage engines in MySQL:

- **MyISAM**: Does not support foreign keys or transactions, provides faster read
operations.
- **InnoDB**: Supports ACID properties, row-level locking, foreign keys, and crash recovery.

How do you handle deadlocks in MySQL?


Deadlocks occur when two or more transactions are waiting for each other to release locks.

1. **Proper Transaction Design**: Ensure transactions access resources in a consistent


order.
2. **Retry Logic**: Implement retry logic in the application layer after a brief delay.
3. **Lock Timeout**: Adjust `innodb_lock_wait_timeout` to define lock wait limits.
4. **Analyze Deadlock Reports**: Use `SHOW ENGINE INNODB STATUS` for deadlock
information.

How would you improve the performance of a slow-running query in MySQL?


Improving query performance can involve several steps:

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.

What are the different types of backups available in SQL Server?


SQL Server supports several types of backups:

1. **Full Backup**: Captures the entire database.


2. **Differential Backup**: Captures only the data that has changed since the last full
backup.
3. **Transaction Log Backup**: Captures the transaction log, useful for point-in-time
recovery.
4. **Filegroup Backup**: Captures specific filegroups rather than the whole database.
5. **Copy-Only Backup**: A special type of full backup that doesn’t interfere with the
existing backup sequence.
How would you handle migrating a large database with minimal downtime?
Migrating a large database with minimal downtime involves careful planning and execution:

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.

Explain ACID properties and their importance in a database.


ACID stands for Atomicity, Consistency, Isolation, and Durability. These properties ensure
reliable transaction processing:

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.

You might also like