Can someone please explain what the partition by keyword does and give a simple example of it in action, as well as why one would want to use it? I have a SQL query written by someone else and I’m trying to figure out what it does.
An example of partition by:
SELECT empno, deptno, COUNT(*) OVER (PARTITION BY deptno) DEPT_COUNT FROM emp
The examples I’ve seen online seem a bit too in-depth.
The
PARTITION BYclause sets the range of records that will be used for each ‘GROUP’ within theOVERclause.In your example SQL,
DEPT_COUNTwill return the number of employees within that department for every employee record. (It is as if you’re de-nomalising theemptable; you still return every record in theemptable.)If there was another column (e.g.,
state) then you could count how many departments in that State.It is like getting the results of a
GROUP BY(SUM,AVG, etc.) without the aggregating the result set (i.e. removing matching records).It is useful when you use the
LAST OVERorMIN OVERfunctions to get, for example, the lowest and highest salary in the department and then use that in a calculation against this records salary without a sub select, which is much faster.Read the linked AskTom article for further details.