Why ‘FORMAT=TRADITIONAL’ Fails but ‘explain_format=TRADITIONAL’ Succeeds When Running EXPLAIN ANALYZE in MySQL

If you’ve been using EXPLAIN ANALYZE to analyse your MySQL queries, you may have encountered a little quirk regarding the output format: When we explicitly use FORMAT=TRADITIONAL in our EXPLAIN ANALYZE statement, we get an error. But when our explain_format system variable is set to TRADITIONAL we don’t get an error.

What’s going on with that? Is this a bug?

First up, no it’s not a bug. It can all be explained by the way EXPLAIN ANALYZE handles our explain_format setting.

Continue reading

Fix “This version of MySQL doesn’t yet support ‘EXPLAIN ANALYZE with JSON format'” in MySQL

If you’re getting an error that reads “This version of MySQL doesn’t yet support ‘EXPLAIN ANALYZE with JSON format’” in MySQL, it’s probably because you’re trying to run EXPLAIN ANALYZE with the wrong output format.

The EXPLAIN ANALYZE statement only supports the TREE output format. However, there are a few things to be aware of regarding this.

Either way, the quickest way to fix the error is to use FORMAT=TREE in your EXPLAIN ANALYZE statement.

Continue reading

Fix “This version of MySQL doesn’t yet support ‘EXPLAIN ANALYZE FOR CONNECTION'” in MySQL

If you’re getting an error that reads “This version of MySQL doesn’t yet support ‘EXPLAIN ANALYZE FOR CONNECTION’” in MySQL, it’s probably because you’re trying to use the FOR CONNECTION option with EXPLAIN ANALYZE, but EXPLAIN ANALYZE doesn’t support the FOR CONNECTION option.

It’s true that we can use the FOR CONNECTION option with the EXPLAIN statement, we can’t use it when using the ANALYZE variant of EXPLAIN (we can use EXPLAIN with or without ANALYZE). At least, this is how it works as of this writing (MySQL 8.3).

To fix, either remove the ANALYZE keyword from the statement or don’t use the FOR CONNECTION option.

Continue reading

Fix Error “The function ‘RANK’ must have an OVER clause” in SQL Server

If you’re getting an error that reads “The function ‘RANK’ must have an OVER clause” in SQL Server, it’s probably because you’re calling the RANK() function without an OVER clause.

The RANK() function requires an OVER clause (and that clause must have an ORDER BY clause).

To fix this issue, add an OVER clause when calling the RANK() function.

Continue reading

Fix “The used SELECT statements have a different number of columns” in MySQL

If you’re getting an error that reads “The used SELECT statements have a different number of columns” in MySQL, it’s probably because you’re using the EXCEPT, INTERSECT, or UNION clause, but with a different number of columns for each query.

When we use these clauses, both queries must select the same number of columns. For example, if the first query has two columns in its SELECT list, then the second query must also have two columns in its SELECT list.

To fix, make sure both queries select the same number of columns.

Continue reading

How to Fix “The function ‘NTILE’ must have an OVER clause with ORDER BY” Error in SQL Server

When using the NTILE() function in SQL Server, we must include an OVER clause clause with an ORDER BY clause.

If you’re getting error msg 4112 that reads “The function ‘NTILE’ must have an OVER clause with ORDER BY” when using the NTILE() function, it’s because, although you’re (correctly) including an OVER clause, you’re omitting the ORDER BY clause.

To fix this error, add an ORDER BY clause to the OVER clause.

Continue reading

Fix ‘Parse error: near “LIMIT”‘ in SQLite When Using the VALUES Statement

If you’re getting a SQLite error that reads ‘Parse error: near “LIMIT”‘, it could be that you’re trying to use the LIMIT clause when using the VALUES clause as a stand alone statement.

SQLite doesn’t allow us to apply the LIMIT clause against the VALUES statement.

However, there is a work around. Below is an example of how we can apply the LIMIT clause against the VALUES statement.

Continue reading

Fix “Invalid object name ‘GENERATE_SERIES'” in SQL Server

If you’re getting SQL Server error 208 that reads “Invalid object name ‘GENERATE_SERIES’“, it could be that you’re calling GENERATE_SERIES() in a version of SQL Server that doesn’t support this function.

The GENERATE_SERIES() function was introduced in SQL Server 2022 (16.x), and so if we try to call it in an earlier version of SQL Server, we’ll get the above error.

So if you’re running this on an earlier version, you’ll need to upgrade before you can run it successfully. You’ll also need to run it on a database with a compatibility level of at least 160.

Continue reading

Fix “Parse error: all VALUES must have the same number of terms” in SQLite when using the VALUES Stand Alone Statement

If you’re getting an error that reads “Parse error: all VALUES must have the same number of terms” in SQLite when using the VALUES clause as a stand alone statement, it’s probably because you’re not providing the same number of columns in all rows.

When we use VALUES to create a constant table, we must provide the same number of columns in each row.

To fix this issue, be sure to provide the same number of columns across all rows.

Continue reading

Fix SQLite ‘Parse error: near “ORDER”‘ When Using VALUES as a Stand Alone Statement

If you’re getting an error that reads ‘Parse error: near “ORDER”‘ in SQLite, it could be that you’re trying to use the ORDER BY clause when using the VALUES clause as a stand alone statement.

Although we can certainly use the VALUES clause as a stand alone SQL statement, we can’t apply the ORDER BY clause against it.

However all is not lost. Below is an example of how we can sort the results of the VALUES statement.

Continue reading