Sobes.tech
Junior — Middle

Can you name the main analytical functions used in SQL for data analysis?

sobes.tech AI

Answer from AI

Main analytical functions in SQL, often used for data analysis:

  • ROW_NUMBER() — assigns a unique sequential number to rows within a partition.
  • RANK() and DENSE_RANK() — rank rows considering possible ties.
  • LEAD() and LAG() — retrieve values from the next or previous row in the set.
  • NTILE(n) — divides rows into n approximately equal groups.
  • SUM() OVER(), AVG() OVER(), COUNT() OVER() — aggregate functions with window (partition) capabilities.

Example of using ROW_NUMBER to number orders per customer:

SELECT OrderID, CustomerID, OrderDate,
       ROW_NUMBER() OVER (PARTITION BY CustomerID ORDER BY OrderDate) AS OrderRank
FROM Orders;