0% found this document useful (0 votes)
22 views

SQL Learning

Case statements allow comparisons between the current row and previous rows using window functions. Window functions like AVG(), MAX(), MIN(), SUM(), and COUNT() can be used with an OVER clause to calculate aggregates over a window of rows without reducing the number of rows like a GROUP BY would. Window functions assign aggregate values to each row rather than grouping rows together.

Uploaded by

Mohd Azaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

SQL Learning

Case statements allow comparisons between the current row and previous rows using window functions. Window functions like AVG(), MAX(), MIN(), SUM(), and COUNT() can be used with an OVER clause to calculate aggregates over a window of rows without reducing the number of rows like a GROUP BY would. Window functions assign aggregate values to each row rather than grouping rows together.

Uploaded by

Mohd Azaz
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

CASE in SQL

1. Case will create a column of its own when implemented.

select *,
case
when Salary > LAG(salary,1) over (order by emp_id) then ('Higher than previous
employee')
when Salary < LAG(salary,1) over (order by emp_id) then ('Lower than previous
employee')
when Salary = LAG(salary,1) over (order by emp_id) then ('Equal to previous
employee')
end
as Salary_Comparison
from employee

Window function in SQL

The aggregate window functions are exactly the same as standard aggregate functions, with
the most popular being AVG(), MAX(), MIN(), SUM() and COUNT(). When used in normal
circumstances, these functions either apply to the entire dataset (e.g. AVG() return the mean
from all the values in a column) or go pair-in-pair with a GROUP BY statement so that the
function is applied to subsets or rows, depending on another variable

The GROUP BY statement can be replaced by using an aggregate window function instead
and specifying the aggregation conditions in the OVER clause instead. The main difference
is that while a standard aggregate function reduces the number of rows to match the
number of categories to which the data are aggregated, the window function does not
change the number of rows, instead assigns the correct value to each row of the dataset,
even if these values are the same.

You might also like