RANK, DENSE_RANK, and ROW_NUMBER in SELECT Statement

Ranking functions are part of SQL analytic functions and are widely used in reporting, analytics, and business intelligence. The most common ones are RANK, DENSE_RANK, and ROW_NUMBER. While they seem similar, their behavior differs when handling duplicate values. Let’s explore them through practical examples. Example 1: Ranking Employees by Salary within Each Department Here we […]

ROLLUP and CUBE in SELECT Statement

SQL provides powerful extensions to GROUP BY: ROLLUP and CUBE. These are extremely useful for building reports with subtotals and cross-tab summaries. Let’s see some practical examples. Example 1: Total Salaries with ROLLUP by Department and Job This query calculates total salaries: Output: The hierarchy is clear: job totals roll up into department totals, which […]

GROUPING SETS in SELECT Statement

When you prepare reports, you often need numbers at different levels: detailed rows, subtotals, and a grand total. Without GROUPING SETS, you would write multiple queries and stitch them together with UNION ALL. With GROUPING SETS, one query does it all — more readable, more efficient. Let’s explore this using the HR schema tables (employees, […]