Window Functions Flashcards

1
Q

Which clauses can a window function be called in?

A

The SELECT and ORDER BY clauses

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between a window function and GROUP BY?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does this query do?
~~~
SELECT first_name, department, salary,
LEAD(first_name) OVER (ORDER BY salary ASC) prev_sal_name
FROM employees
~~~

A

It returns first_name, department, salary, and the first name of the person with the next highest salary out of all employees.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does this query do?
~~~
SELECT first_name, department, salary,
LAG(first_name) OVER (PARTITION BY department ORDER BY salary ASC) prev_salary
FROM employees
~~~

A

It returns theIt returns first_name, department, salary, and the first name of the person with the next lowest salary from the employee’s respective department.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What types of functions are LEAD(), LAG(), FIRST_VALUE(), and LAST_VALUE()?

A

Positional functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a critical part of a window function when using positional functions?

A

ORDER BY, because positional functions are entirely dependent on the order of the values within the window

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

T/F: using OVER() without any info inside of its parentheses will fail

A

False, it will run on the entire data set as its partition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the PARTITION BY clause in this query?
~~~
SELECT
MAX(salary) OVER (PARTITION BY department) max_salary
FROM employees
~~~

A

It specifies the window in which to aggregate the data. In this case, it is specifying ‘department’ as the partition over which to aggregate salary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly