An Introduction to the MEDIAN() Function in SQL

Some relational database management systems (RDBMSs) have a MEDIAN() function that calculates the median value within a range of values.

It returns the middle value or an interpolated value that would be the middle value once the values are sorted.

The SQL MEDIAN() function typically operates on numeric expressions, but depending on the RDBMS, may also operate on other data types, such as datetime expressions.

Depending on the RDBMS, the MEDIAN() function can be used as a window function, as an aggregate function, or as both.

Continue reading

Introduction to the PERCENTILE_DISC() Function in SQL

Some SQL databases have a PERCENTILE_DISC() function that calculates a percentile based on a discrete distribution of a range of column values.

We specify the percentile to use when we call the function.

Depending on the DBMS, PERCENTILE_DISC() can be used as a window function, as an aggregate function, or as both.

The PERCENTILE_DISC() function always returns a value from the underlying data. This is in contrast to the PERCENTILE_CONT() function, which can interpolate between adjacent values to return a value that’s not in the underlying data.

Continue reading

Overview of the PERCENTILE_CONT() Function in SQL

Some relational database management systems (RDBMSs) have a PERCENTILE_CONT() function that calculates a percentile based on a continuous distribution across a range of column values.

We specify the percentile to use when we call the function.

Depending on the RDBMS, PERCENTILE_CONT() can be used as a window function or an aggregate function, or as both.

Continue reading

How to Fix Error “function mode() does not exist” in PostgreSQL

If you’re getting an error that reads “function mode() does not exist” in PostgreSQL, it could be that you’re using the wrong syntax with this function.

The mode() function requires a WITHIN GROUP clause, and we can get the above error if we remove that clause.

In this case, we can fix the error by adding a valid WITHIN GROUP clause.

Continue reading

Create a Running Total in SQL

We can use SQL to create a running total of a given column. By this I mean, we can create a column that calculates the cumulative sum of a given column.

The running total/cumulative sum is the sum of all rows up until the current row. The result is that the sum increases (or decreases in the case of negative values) with each row returned in the result set. Each row’s value is added to the cumulative amount from all prior rows, so for any given row, we get the total sum for all rows up to that point – the “running total”.

Continue reading