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

Fix ERROR 3580 “There is a circularity in the window dependency graph” in MySQL

If you’re getting error 3580 in MySQL, which reads “There is a circularity in the window dependency graph“, it’s probably because you’re using named windows that refer to each other in a way that creates a circular reference.

To fix this issue, make sure you don’t create a circular reference when referring to multiple named windows.

Continue reading

How to Change a CHECK Constraint’s Enforcement State in MySQL

When we create or alter a CHECK constraint in MySQL, we have the option of setting it to ENFORCED or NOT ENFORCED. This determines whether or not MySQL will enforce the rules of the constraint whenever data that violates the constraint tries to enter the database.

We can use the ALTER TABLE statement to change the enforcement state of an existing CHECK constraint in MySQL, as shown in the following example.

Continue reading

Fix ERROR 1140 “In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column…” in MySQL

If you’re getting MySQL error 1140, which reads something like “In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column ‘world.City.District’; this is incompatible with sql_mode=only_full_group_by“, it could be that you need to introduce a window function to the query.

This error can happen when we want multiple rows to contain aggregate values. For example, we might want to return subtotals of all rows within a group of rows. A window function can help us achieve this outcome.

Continue reading