Intern
Have you worked with window functions? Which ones do you know?
sobes.tech AI
Answer from AI
Yes, I have worked with window functions in SQL. The main window functions I know and use are:
- ROW_NUMBER() — numbering rows within a window.
- RANK() and DENSE_RANK() — ranking with consideration for ties.
- LAG() and LEAD() — retrieving values from the previous or next row.
- SUM(), AVG(), COUNT() — aggregate functions with a window.
Window functions allow performing calculations across sets of table rows related to the current row without collapsing the result set, which is useful for analytics and reporting.
Example:
SELECT employee_id, department_id, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) as salary_rank
FROM employees;