Sobes.tech
Middle

How did you work with analytical functions (window functions)?

sobes.tech AI

Answer from AI

Analytic functions (window functions) allow performing calculations across sets of table rows that are related to the current row, without the need for grouping the data. For example, you can calculate a moving sum, ranking, or average within data partitions.

Example of usage in SQL:

SELECT
  employee_id,
  department_id,
  salary,
  RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank
FROM employees;

Here, the salary rank is calculated for each employee within their department. I used window functions for data analysis, simplifying complex queries, and improving performance by avoiding subqueries and additional joins.