Detailed guide to SQL window and analytic functions with examples of ROW_NUMBER, PARTITION BY and OVER clause

6.What is the difference between NTILE() and PERCENT_RANK()?
NTILE() is to divide the result set into any number of equal-sized buckets and assigns a bucket number to each row. PERCENT_RANK() calculates the relative rank of a row within a result set expressed as a percentage ranging from 0 to 1.
Example Query (NTILE())
SELECT employee_id, salary,
NTILE(4) OVER (ORDER BY salary) AS quartile
FROM employees;
This query divides the employees into 4 equal quartiles based on their salary.
Example Query (PERCENT_RANK()):
SELECT employee_id, salary,
PERCENT_RANK() OVER (ORDER BY salary) AS percent_rank
FROM employees;
It returns the percentile rank for each employee's salary.


7.How do you use SUM() and AVG() as window functions to calculate running totals and moving averages?
You can use SUM() and AVG() with the OVER clause to calculate running totals or moving averages over a window of rows. The operation is performed on an order of rows and set by PARTITION BY and ORDER BY.
Query:
SELECT employee_id, salary,
SUM(salary) OVER (ORDER BY hire_date) AS running_total,
AVG(salary) OVER (ORDER BY hire_date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS moving_avg FROM employees;
In this query, SUM() gives a running sum of salaries and AVG() gives a moving average of the last 3 salaries (including the current row).


8.What is a window frame in SQL and how might you define it?
A window frame identifies the set of rows over which the window function operates and in terms of the language, may be defined either by a ROWS or RANGE clause within the OVER clause. Thus, the window frame determines the extent of the computation for the window function-by including either just the current row or a given range of rows.
Example Query:
SELECT employee_id, salary,
SUM(salary) OVER (ORDER BY hire_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total FROM employees;
In this query, the window frame includes all rows from the beginning of the result set up to the current row.


9.what are real-life use cases for window functions in SQL?
Window functions are most used in ranking, running sums, moving averages, and similar comparisons of values between the rows. They find practical applications in financial computations and sales analysis and also can be used in reporting systems.
Example Query (Ranking: Real-life usage for ranking)
SELECT employee_id, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS department_rank FROM employees;.


10.How do you optimize queries with window functions in SQL?
Optimization of queries with window functions goes about indexing, reducing rows processed and ensuring that window functions are applied where necessary. You may also use pre-aggregated data or subqueries to avoid using unnecessary window functions.
Query Example (Indexing for Optimization):
CREATE INDEX idx_employee_salary ON employees (salary);
Indexing on frequently used columns such as salary can improve query performance of queries that use window functions, sorting or partitioning on this column.