Master Structured Query Language for Database Development, Data Analysis, and Performance Engineering
Introduction: Why SQL Matters
- What This Book Assumes About You
- How This Book Is Organized
- The Sample Databases
- Standard SQL Versus Vendor Differences
- How to Use This Book Effectively
- A First Glimpse: Your First Query
Chapter 1: Understanding the Relational Model
- From File Systems to Relational Databases: A Brief History
- What Is a Relation? Tables, Rows, and Columns Explained
- Entities, Attributes, and Relationships in the Real World
- The Core Principles of the Relational Model
- Schemas: Organizing Your Data Universe
- Chapter Summary
Chapter 2: Data Types, Keys, and Constraints
- Choosing the Right Data Type: Numbers, Strings, Dates, and More
- Primary Keys: Uniquely Identifying Every Row
- Foreign Keys: Linking Tables Together
- Constraints: NOT NULL, UNIQUE, CHECK, DEFAULT
- Chapter Summary
Chapter 3: Database Design and Normalization
- Why Database Design Matters More Than You Think
- First Normal Form: Eliminating Repeating Groups
- Second and Third Normal Forms: Removing Dependencies
- Beyond 3NF: BCNF and Practical Limits
- When to Denormalize: Performance vs Integrity Trade-offs
- Common Design Patterns and Anti-Patterns
- Chapter Summary
Chapter 4: Creating and Managing Database Objects
- CREATE DATABASE and Schema Organization
- CREATE TABLE: Defining Your First Table
- ALTER TABLE: Evolving Your Schema Over Time
- DROP and TRUNCATE: Removing Data Objects
- Building the Northwind Analytics Database: Complete Schema
- Building the TechCorp HR Database: Complete Schema
- Chapter Summary
Chapter 5: Retrieving Data with SELECT
- Anatomy of a SELECT Statement
- Filtering Rows with WHERE: Comparison and Logical Operators
- Sorting Results with ORDER BY
- Limiting Output: TOP, LIMIT, and OFFSET Across Vendors
- Column Aliases and Readable Queries
- Handling NULL Values: IS NULL, COALESCE, and NULLIF
- DISTINCT and Duplicate Elimination
- Chapter Summary
Chapter 6: Expressions, Functions, and Conditional Logic
- Arithmetic and Comparison Expressions
- String Functions: Concatenation, Substrings, Trimming, Searching
- Date and Time Functions Across Major Databases
- Numeric and Mathematical Functions
- The CASE Expression: Conditional Logic in SQL
- Operator Precedence and Parentheses
- Practical Examples: Transforming and Enriching Data
- Chapter Summary
Chapter 7: Aggregation and Grouping
- Aggregate Functions: COUNT, SUM, AVG, MIN, MAX
- GROUP BY: Organizing Data into Meaningful Groups
- HAVING vs WHERE: Filtering Before and After Grouping
- Multiple Column Grouping and Hierarchical Aggregation
- ROLLUP, CUBE, and GROUPING SETS
- Common Aggregation Mistakes and How to Avoid Them
- Chapter Summary
Chapter 8: Joining Tables
- Why Joins Exist: Combining Related Data
- INNER JOIN: Matching Rows Across Tables
- LEFT, RIGHT, and FULL OUTER JOINs: Including Non-Matching Rows
- SELF JOIN: Comparing Rows Within the Same Table
- CROSS JOIN: Cartesian Products and When They’re Useful
- Joining Multiple Tables: Building Complex Queries
- Join Conditions vs Filters: WHERE vs ON Semantics
- Common Join Mistakes and Performance Tips
- Chapter Summary
Chapter 9: Subqueries and Derived Tables
- What Is a Subquery and Where Can It Go?
- Scalar Subqueries: Single Value Results
- Table Subqueries in the FROM Clause
- IN, ANY, ALL, and SOME: Set-Based Comparisons
- EXISTS and NOT EXISTS: Efficient Existence Checks
- Correlated Subqueries: When Inner Queries Reference Outer Rows
- Performance Considerations for Subqueries
- Chapter Summary
Chapter 10: Common Table Expressions and Recursive Queries
- What Is a Common Table Expression?
- Using CTEs to Structure Complex Queries
- Multiple CTEs: Building Step-by-Step Logic
- Recursive CTEs Explained: Anchor and Recursive Members
- Traversing Hierarchical Data: Org Charts and Category Trees
- Practical Patterns: Running Calculations and Gap Detection
- Chapter Summary
Chapter 11: Set Operations
- UNION and UNION ALL: Combining Result Sets Vertically
- INTERSECT: Finding Common Rows
- EXCEPT and MINUS: Set Difference Operations
- Set Operations vs Joins: Choosing the Right Tool
- Practical Scenarios: Data Comparison and Reconciliation
- Chapter Summary
Chapter 12: Window Functions and Analytical Queries
- What Are Window Functions? Beyond Aggregation
- The OVER Clause: PARTITION BY and ORDER BY
- Ranking Functions: ROW_NUMBER, RANK, DENSE_RANK, NTILE
- Value Functions: LAG, LEAD, FIRST_VALUE, LAST_VALUE
- Aggregate Window Functions: Running Totals and Moving Averages
- Window Frames: ROWS, RANGE, and GROUPS
- Advanced Analytical Patterns: Gaps, Islands, and Trending
- Chapter Summary
Chapter 13: Views, Temporary Tables, and Materialized Views
- Views: Virtual Tables for Reusable Queries
- Creating and Using Temporary Tables
- Materialized Views: Precomputed Results
- Choosing Between Views, CTEs, and Temp Tables
- Performance Implications of Each Approach
- Chapter Summary
Chapter 14: Stored Procedures and User-Defined Functions
- Stored Procedures: Encapsulating Business Logic
- Parameters: Input, Output, and Default Values
- Control Flow: IF, CASE, Loops, and Cursors
- User-Defined Functions: Scalar and Table-Valued
- Error Handling: TRY-CATCH and Transaction Safety
- When to Use Procedural Code vs Pure SQL
- Chapter Summary
Chapter 15: Triggers and Automated Actions
- What Are Triggers and When Do They Fire?
- BEFORE, AFTER, and INSTEAD OF Triggers
- Row-Level vs Statement-Level Triggers
- Practical Use Cases: Auditing, Validation, and Cascading Updates
- The Hidden Costs of Triggers: Debugging and Performance
- Modern Alternatives to Trigger-Based Logic
- Chapter Summary
Chapter 16: Transactions, ACID, and Concurrency Control
- What Is a Transaction? The Concept of Atomic Work Units
- ACID Properties: Atomicity, Consistency, Isolation, Durability
- COMMIT, ROLLBACK, and SAVEPOINT
- Isolation Levels: From Read Uncommitted to Serializable
- Locking Mechanisms: Shared, Exclusive, and Intent Locks
- Deadlocks: Causes, Detection, and Prevention
- Practical Transaction Patterns for Real Applications
- Chapter Summary
Chapter 17: Indexes and Query Optimization
- How Indexes Work: The B-Tree Explained Simply
- Creating and Choosing Index Types
- Composite and Covering Indexes
- Reading Query Execution Plans
- Writing SARGable Queries: Patterns That Use Indexes
- Common Performance Anti-Patterns and Fixes
- Vendor-Specific Index Features
- Chapter Summary
Chapter 18: Advanced Performance and Large-Scale Data
- Table Partitioning: Horizontal and Vertical Strategies
- Query Statistics and the Optimizer’s Knowledge Base
- Connection Management and Pooling
- Caching Strategies at Database and Application Levels
- Handling Very Large Tables: Practical Techniques
- When Traditional SQL Databases Hit Their Limits
- Chapter Summary
Chapter 19: Security, Access Control, and Data Integrity
- Users, Roles, and Privileges: The Principle of Least Privilege
- Authentication Methods Across Major Databases
- SQL Injection: Understanding the Attack
- Preventing SQL Injection: Parameterized Queries and Best Practices
- Row-Level Security and Data Masking
- Encryption: At Rest and In Transit
- Chapter Summary
Chapter 20: Backup, Recovery, and Operational Excellence
- Backup Strategies: Full, Incremental, and Differential
- Point-in-Time Recovery Concepts
- Replication Basics: Master-Slave and Multi-Master
- Database Monitoring and Health Checks
- Routine Maintenance Tasks
- Schema Migration Best Practices
- Professional SQL Habits for Production Environments
- Chapter Summary
Conclusion: Your Path Forward with SQL
Appendix A: Quick Reference Guide
- Data Definition Language (DDL)
- Data Manipulation Language (DML)
- Common Functions
- Transaction Control Language (TCL)
- Data Control Language (DCL)
Appendix B: Vendor Comparison Cheat Sheet
- Auto-Incrementing Primary Keys
- Current Timestamp
- String Concatenation
- Pagination
- Date Truncation
- Boolean Type
- Window Functions Support
- Materialized Views
Appendix C: Troubleshooting Guide
- Common Errors and Fixes
- Debugging Techniques