Understanding the DENSE_RANK() Function in MySQL

In MySQL, the DENSE_RANK() function is a window function that returns the rank of the current row within its partition, without gaps.

By “without gaps” this means that it returns contiguous rank numbers whenever there are peers. Peers are considered ties and receive the same rank. In such cases, the next rank value is one greater than the current one (i.e. the one that the peers receive).

Continue reading

Introduction to the OVER Clause in SQL

When running SQL database queries, sometimes we need to use a window function in order to get the results we’re looking for. A window function is an aggregate-like function that enables partitioning and ordering of data within a result set.

The OVER clause is what enables us to create a window function.

The examples below demonstrate how we can incorporate the OVER clause in our SQL queries.

Continue reading

Understanding the sys.quote_identifier() Function in MySQL

In MySQL, we can use the sys.quote_identifier() function to quote a string to produce a result that can be used as an identifier in a SQL statement.

The string is returned enclosed by backticks (`), with each instance of a backtick doubled.

This function can be handy when a value to be used as an identifier is a reserved word or contains backtick characters.

Continue reading

Understanding Window Functions in SQL

Window functions can be a useful tool when writing SQL queries. They allow us to include aggregate data across multiple rows without getting those pesky errors that sometimes occur when we try to use an aggregate function in the wrong way.

In this article, I aim to provide a simple overview of window functions and how they can be used to provide a more useful result set when running SQL queries.

Continue reading