This repository contains a comprehensive guide and interactive tool for mastering SQL Window Functions:
A detailed 5000+ word markdown guide covering:
- β Introduction and core concepts
- β All 15+ window functions with syntax and examples
- β Aggregate functions (SUM, AVG, COUNT, MIN, MAX)
- β Ranking functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE, PERCENT_RANK)
- β Analytical functions (LAG, LEAD, FIRST_VALUE, LAST_VALUE, NTH_VALUE)
- β Frame specifications and ROWS vs RANGE
- β Top 5 Interview Questions with detailed answers
- β Real-world use cases: E-commerce, Finance, HR, Marketing, Manufacturing
- β Performance optimization tips
- β Best practices and common mistakes
An interactive Streamlit application with:
- β Live query executor with sample data
- β
8 interactive sections:
- Home page with overview
- Aggregate functions (SUM, AVG, COUNT, MIN, MAX)
- Ranking functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE, PERCENT_RANK)
- Analytical functions (LAG, LEAD, FIRST_VALUE, LAST_VALUE, NTH_VALUE)
- Sample data viewer
- Custom query runner
- Quick reference guide
- β Built-in sample datasets (employees, sales, orders, performance)
- β Real-time SQL execution and result visualization
- β Side-by-side query and output display
- β Common patterns and mistakes reference
Simply open SQL_Window_Functions_Complete_Guide.md in any markdown viewer to read the comprehensive guide with all examples and explanations.
# Install required packages
pip install streamlit pandas numpy
# Optional: If using specific database
pip install sqlite3 # Usually comes with Python# Navigate to the directory
cd "c:\SARAVANA\SQL\Window Functions"
# Run the Streamlit app
streamlit run window_functions_streamlit_app.pyThe app will open in your default browser at http://localhost:8501
The Streamlit app includes 4 pre-built sample datasets:
employee_id, employee_name, department, salary, hire_date
10 employees across 4 departments (Sales, IT, HR, Finance)
Salary range: $50,000 - $90,000
sale_id, sale_date, product, amount, region
20 transactions of products A, B, C
Regions: North, South, East, West
Amount range: $100 - $250
order_id, customer_id, customer_name, order_date, order_amount
20 orders from 9 customers
Order amount range: $50 - $300
employee_id, month, revenue, target
Monthly revenue vs target metrics
- Start with the Home section in the Streamlit app
- Read the Core Concepts in the markdown guide
- Try each aggregate function example in the app (SUM, AVG, COUNT, MIN, MAX)
- Experiment with custom queries
- Study the Using Cases section in the markdown guide
- Explore ranking functions in the app (ROW_NUMBER, RANK, DENSE_RANK)
- Master the differences between RANK vs DENSE_RANK vs ROW_NUMBER
- Work through the "Top 5 Interview Questions"
- Deep dive into Analytical Functions (LAG, LEAD, NTH_VALUE)
- Study Frame Specifications and optimize performance
- Review Real-World Use Cases
- Understand performance optimization strategies
| Function | Purpose | Use Case |
|---|---|---|
| SUM() | Running totals or partition sum | Track cumulative sales |
| AVG() | Average within window | Compare to group average |
| COUNT() | Count rows in window | Frequency analysis |
| MIN() | Minimum value | Find lowest price |
| MAX() | Maximum value | Find highest salary |
| Function | Behavior | Use Case |
|---|---|---|
| ROW_NUMBER() | Unique sequential numbers | Pagination, duplicates |
| RANK() | Ranks with gaps | Leaderboards, competitions |
| DENSE_RANK() | Consecutive ranks | Top N without gaps |
| NTILE() | Divide into N groups | Quartiles, deciles |
| PERCENT_RANK() | Percentile (0-1) | Performance percentiles |
| Function | Purpose | Use Case |
|---|---|---|
| LAG() | Access previous row | Month-over-month change |
| LEAD() | Access next row | Predict next value |
| FIRST_VALUE() | First row in frame | Compare to baseline |
| LAST_VALUE() | Last row in frame | Latest status |
| NTH_VALUE() | Nth row in frame | Access specific position |
Track cumulative amounts over time
SUM(amount) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)Get top 3 employees by department
WHERE ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) <= 3See how each salary compares to department average
salary - AVG(salary) OVER (PARTITION BY department)Calculate percentage change from previous month
100.0 * (revenue - LAG(revenue) OVER (ORDER BY month)) / LAG(revenue) ...Divide customers into quartiles by spend
NTILE(4) OVER (ORDER BY total_spent DESC)- β PostgreSQL 9.3+
- β SQL Server 2012+
- β MySQL 8.0+
- β Oracle 8.1+
- β Snowflake
- β SQLite (via Streamlit app)
Interactive Elements:
- Live SQL query runner
- Real-time result visualization
- Side-by-side code and output
- Sample data browser
- Quick reference panels
- Copy-friendly code blocks
Sample Data Management:
- In-memory SQLite database
- 4 pre-configured tables
- 40+ sample records
- Fast query execution
A:
- RANK(): Has gaps when there are ties (1, 2, 2, 4)
- DENSE_RANK(): No gaps (1, 2, 2, 3)
See the detailed explanation in the markdown guide and try both in the Streamlit app!
A:
- ROW_NUMBER(): When you need unique numbers (pagination)
- RANK(): When you need competition rankings
A:
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) as rank
FROM employees
) WHERE rank <= 3;A: You need to specify the frame explicitly:
LAST_VALUE(salary) OVER (
ORDER BY id
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)A: Minimal if you:
- Index PARTITION BY columns
- Use specific ROWS frames instead of full frames
- Materialize large intermediate results
- Group similar window definitions
The markdown guide includes the Top 5 Questions Companies Ask:
-
Difference between RANK(), DENSE_RANK(), and ROW_NUMBER()
- Detailed comparison with examples
- When to use each
-
How to get Top N records per group?
- Multiple solutions
- Modern SQL alternatives
-
Calculate running total
- Understanding ORDER BY and frames
- Real-world patterns
-
Identify gaps and islands
- Advanced problem solving
- Data quality assessment
-
Month-over-month growth calculation
- Business metrics
- Complex calculations
- Start Simple: Begin with basic aggregates (SUM, AVG)
- Try Examples: Run examples in the Streamlit app before writing your own
- Modify Queries: Change the queries to understand how they work
- Practice Ranking: Compare ROW_NUMBER, RANK, and DENSE_RANK side-by-side
- Understand Frames: ROWS BETWEEN is key to correct results
- Check Output: Always verify results make sense
- Performance: Use EXPLAIN to understand query plans
-- Wrong: Returns partition total, not running total
SUM(salary) OVER (PARTITION BY department)
-- Correct: Specify ORDER BY
SUM(salary) OVER (PARTITION BY department ORDER BY employee_id)-- Wrong: May return current row only
LAST_VALUE(salary) OVER (PARTITION BY department ORDER BY id)
-- Correct: Specify full frame
LAST_VALUE(salary) OVER (
PARTITION BY department
ORDER BY id
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)-- Wrong: Can't use window function in GROUP BY
SELECT department, ROW_NUMBER() OVER (...) FROM employees GROUP BY department
-- Correct: Use CTE or subquery
WITH ranked AS (SELECT *, ROW_NUMBER() OVER (...) FROM employees)
SELECT * FROM rankedWithin the Streamlit App:
- Use the "Quick Reference" tab for syntax
- View "Common Patterns" for typical use cases
- Check the "Sample Data" tab to understand tables
- Try "Custom Query" to experiment
In the Markdown Guide:
- Read the "Best Practices Summary"
- Study the "Common Mistakes to Avoid"
- Review the "Top 5 Interview Questions"
- Open the markdown guide in a browser/editor
- Run the Streamlit app in another window
- Read concept in guide
- Try example in app
- Modify and experiment
- Keep the markdown guide bookmarked
- Use Quick Reference tab in app for syntax
- Copy-paste and modify existing queries
- Use custom query runner
- Try to write queries from scratch
- Verify results before moving on
Key strategies covered in the guide:
- Index Smart: Create indexes on PARTITION BY and ORDER BY columns
- Frame Carefully: Use specific frames instead of full partitions
- Materialize Early: Store intermediate results
- Monitor Execution: Use EXPLAIN to understand plans
- Data Types: Ensure consistent types in calculations
The materials prepare you for:
- β SQL certification exams
- β Database professional interviews
- β Data analyst interviews
- β Advanced SQL courses
Topic coverage matches:
- Microsoft SQL Server Certifications
- AWS Database Specialty
- Google Cloud Associate Cloud Engineer
- Azure Data Engineer Associate
- v1.0 (February 2026): Initial comprehensive guide and Streamlit app
- Complete coverage of 15+ window functions
- 5000+ word markdown guide
- Interactive Streamlit application
- 40+ real-world examples
These materials are suitable for:
- Self-Learning: Comprehensive guide for individuals
- Team Training: Share guide and Streamlit app with team
- Reference: Quick reference for complex queries
- Interview Prep: Study top 5 interview questions
- Teaching: Use real-world use cases in classroom
- Markdown Guide: See SQL_Window_Functions_Complete_Guide.md
- Streamlit App: Run window_functions_streamlit_app.py
- Sample Data: View in Streamlit app "Sample Data" tab
- Interview Prep: See markdown guide "Top 5 Interview Questions"
- All examples use standard SQL syntax
- Compatible with most modern SQL servers
- Sample data is in-memory (no external DB needed)
- Streamlit app is read-only (for learning)
- Markdown guide is exportable to PDF
Happy Learning! Master Window Functions and Elevate Your SQL Skills! π
Last Updated: February 2026 Designed for: Beginners to Professionals Scope: Complete Window Functions Coverage