SQL for Developers
Complete guide to SQL including query optimization, indexing strategies, and database fundamentals for PostgreSQL, MySQL, and MS SQL Server
Welcome to the SQL for Developers documentation! This comprehensive guide covers everything from SQL fundamentals to advanced optimization techniques across major database platforms.
What You'll Learn
This documentation covers:
- SQL Fundamentals: SELECT, INSERT, UPDATE, DELETE, JOINs, and subqueries
- Query Optimization: EXPLAIN plans, performance tuning, and best practices
- Indexing Strategies: B-tree, hash, GIN, GiST, and composite indexes
- Database-Specific Guides: PostgreSQL, MySQL, and MS SQL Server
- Database Design: Normalization, relationships, and schema design
- Transactions & ACID: Isolation levels, locking, and concurrency control
Prerequisites
Before diving in, you should have:
- A database server installed (PostgreSQL, MySQL, or SQL Server)
- Basic command line familiarity
- Understanding of data types and programming concepts
Quick Navigation
SQL Fundamentals
Core SQL syntax and operations
Query Optimization
Performance tuning techniques
Indexing Strategies
Index types and best practices
Transactions & ACID
Transactions, isolation levels, locking
Database Design
Normalization and schema patterns
PostgreSQL
PostgreSQL-specific features
MySQL
MySQL-specific features
MS SQL Server
SQL Server-specific features
Topic Overview
SQL Fundamentals
Master the core building blocks of SQL:
- Data Retrieval: SELECT statements with filtering and sorting
- Data Manipulation: INSERT, UPDATE, DELETE operations
- Joins: INNER, LEFT, RIGHT, FULL OUTER, and CROSS joins
- Aggregations: GROUP BY, HAVING, and aggregate functions
- Subqueries: Correlated and non-correlated subqueries
- CTEs: Common Table Expressions for readable queries
Query Optimization
Learn to write performant queries:
- Execution Plans: Reading and understanding EXPLAIN output
- Index Usage: Ensuring queries use indexes effectively
- Query Rewriting: Transforming slow queries into fast ones
- Statistics: How the query planner makes decisions
- Common Anti-patterns: Avoiding N+1 queries, SELECT *, etc.
Indexing Strategies
Understand when and how to create indexes:
- B-tree Indexes: Default index type for most queries
- Hash Indexes: Equality comparisons
- GIN Indexes: Full-text search and arrays (PostgreSQL)
- GiST Indexes: Geometric and spatial data
- Composite Indexes: Multi-column indexes and column order
- Partial Indexes: Indexing subsets of data
Database Platforms
PostgreSQL
Advanced features, extensions, and psql CLI
MySQL
Storage engines, replication, and mysql CLI
MS SQL Server
T-SQL, SSMS, and sqlcmd
Quick Start
Choose your database platform below to get started with the CLI setup.
PostgreSQL
# Connect to PostgreSQL
psql -U postgres -d mydatabase
# List all databases
\l
# Connect to a specific database
\c mydatabase
# List all tables
\dtMySQL
# Connect to MySQL
mysql -u root -p
# List all databases
SHOW DATABASES;
# Use a specific database
USE mydatabase;
# List all tables
SHOW TABLES;MS SQL Server
# Connect using sqlcmd
sqlcmd -S localhost -U sa -P 'YourPassword'
# List all databases
SELECT name FROM sys.databases;
# Use a specific database
USE mydatabase;
# List all tables
SELECT * FROM INFORMATION_SCHEMA.TABLES;Performance Comparison
| Feature | PostgreSQL | MySQL | SQL Server |
|---|---|---|---|
| Default Index | B-tree | B-tree | B-tree |
| Full-Text Search | Built-in (GIN) | Built-in (FULLTEXT) | Built-in |
| JSON Support | Native (JSONB) | Native (JSON) | Native (JSON) |
| CTEs | ✅ Full Support | ✅ Full Support | ✅ Full Support |
| Window Functions | ✅ Full Support | ✅ Full Support | ✅ Full Support |
| Partitioning | Declarative | Range/List/Hash | Range/List/Hash |
| Max DB Size | Unlimited | 256 TB | 524 PB |
Best Practices
Always test queries with realistic data volumes before deploying to production.
- Use EXPLAIN: Always analyze query execution plans
- Index Wisely: Don't over-index; each index has write overhead
- **Avoid SELECT ***: Only select columns you need
- Use Parameterized Queries: Prevent SQL injection
- Monitor Slow Queries: Enable slow query logging
- Regular Maintenance: VACUUM (PostgreSQL), OPTIMIZE (MySQL)
Next Steps
Learn SQL Fundamentals
Start with the SQL Fundamentals guide to master core syntax.
Choose Your Database
Pick PostgreSQL, MySQL, or MS SQL Server.