Update docs/tech_docs/sql_notes.md
This commit is contained in:
@@ -1,3 +1,55 @@
|
||||
Preparing for SQL interviews requires a solid understanding of advanced SQL concepts, queries, and optimizations. This guide is designed to provide a concise overview of typical advanced SQL interview questions, offering quick refreshers on key topics.
|
||||
|
||||
## Advanced SQL Interview Questions Guide
|
||||
|
||||
### 1. **Window Functions**
|
||||
- **Question**: Explain window functions in SQL. Provide examples where they are useful.
|
||||
- **Refresher**: Window functions perform a calculation across a set of table rows related to the current row. Unlike GROUP BY, window functions do not cause rows to become grouped into a single output row. Common examples include `ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`, and `NTILE()`, useful for tasks like ranking, partitioning, and cumulative aggregates.
|
||||
|
||||
### 2. **Common Table Expressions (CTEs)**
|
||||
- **Question**: What are Common Table Expressions and when would you use them?
|
||||
- **Refresher**: CTEs allow you to name a temporary result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. They are useful for creating readable and maintainable queries by breaking down complex queries into simpler parts, especially when dealing with hierarchical or recursive data.
|
||||
|
||||
### 3. **Indexes and Performance**
|
||||
- **Question**: How do indexes work, and what are the trade-offs of using them?
|
||||
- **Refresher**: Indexes improve the speed of data retrieval operations by providing quick access to rows in a database table. The trade-off is that they increase the time required for write operations (INSERT, UPDATE, DELETE) because the index must be updated. They also consume additional storage space.
|
||||
|
||||
### 4. **Query Optimization**
|
||||
- **Question**: Describe how you would optimize a slow-running query.
|
||||
- **Refresher**: Optimization strategies include:
|
||||
- Ensuring proper use of indexes.
|
||||
- Avoiding SELECT * and being specific about the columns needed.
|
||||
- Using JOINs instead of subqueries where appropriate.
|
||||
- Analyzing and optimizing the query execution plan.
|
||||
|
||||
### 5. **Transactions**
|
||||
- **Question**: What is a database transaction, and what properties must it have (ACID)?
|
||||
- **Refresher**: A transaction is a sequence of database operations that are treated as a single logical unit of work. It must be Atomic (all or nothing), Consistent (ensures data integrity), Isolated (independent from other transactions), and Durable (persists after completion).
|
||||
|
||||
### 6. **Database Locking**
|
||||
- **Question**: What is database locking? Explain optimistic vs. pessimistic locking.
|
||||
- **Refresher**: Database locking is a mechanism to control concurrent access to a database to prevent data inconsistencies. Pessimistic locking locks resources as they are accessed, suitable for high-conflict scenarios. Optimistic locking allows concurrent access and checks at commit time if another transaction has modified the data, suitable for low-conflict environments.
|
||||
|
||||
### 7. **Normalization vs. Denormalization**
|
||||
- **Question**: Compare normalization and denormalization. When would you use each?
|
||||
- **Refresher**: Normalization involves organizing data to reduce redundancy and improve data integrity. Denormalization adds redundancy to optimize read operations. Use normalization to design efficient schemas and maintain data integrity, and denormalization to optimize query performance in read-heavy applications.
|
||||
|
||||
### 8. **SQL Injection**
|
||||
- **Question**: What is SQL injection, and how can it be prevented?
|
||||
- **Refresher**: SQL injection is a security vulnerability that allows an attacker to interfere with the queries that an application makes to its database. It can be prevented by using prepared statements and parameterized queries, escaping all user-supplied input, and practicing least privilege access control for database operations.
|
||||
|
||||
### 9. **Data Types**
|
||||
- **Question**: Discuss the importance of choosing appropriate data types in a database schema.
|
||||
- **Refresher**: Appropriate data types ensure accurate data representation and efficient storage. They can affect performance, especially for indexing and joins, and influence the integrity of the data (e.g., using DATE types to ensure valid dates).
|
||||
|
||||
### 10. **Subqueries vs. JOINs**
|
||||
- **Question**: Compare subqueries with JOINs. When is each appropriate?
|
||||
- **Refresher**: Subqueries can simplify complex joins and are useful when you need to select rows before joining. JOINs are generally faster and more efficient for straightforward joins of tables. The choice depends on the specific use case, readability, and performance.
|
||||
|
||||
This advanced guide covers key topics and concepts that are often discussed in SQL interviews, offering a quick way to refresh your knowledge and prepare for challenging questions.
|
||||
|
||||
---
|
||||
|
||||
Creating a guide that encapsulates the lifecycle of a SQL query—from its inception to its use in production—offers a comprehensive look at the process of working with SQL in real-world scenarios. This narrative will explore how queries are built, optimized, tested, and refined, as well as considerations for maintaining and updating queries over time.
|
||||
|
||||
# The Lifecycle of a SQL Query: A Comprehensive Guide
|
||||
|
||||
Reference in New Issue
Block a user