Fix “Incorrect syntax near the keyword ‘DISTINCT'” Error in SQL Server

If you’re getting an error that reads “Incorrect syntax near the keyword ‘DISTINCT’” when using the DISTINCT clause in SQL Server, it could be that you’ve put the DISTINCT clause in the wrong position.

When using the DISTINCT clause, it must be the first item in the SELECT list.

Therefore, to fix this error, check the position of the DISTINCT keyword. If it’s not the first item in the SELECT list, move it to the front so that it is the first item in the SELECT list.

Continue reading

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

When using window functions such as RANK() in SQL Server, we must provide an OVER clause clause with an ORDER BY clause.

If you’re getting an error that reads “The function ‘RANK’ must have an OVER clause with ORDER BY”, it’s probably because you’re including an OVER clause with the RANK() function (as is required), but you’re omitting the ORDER BY clause.

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

Continue reading

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

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

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

To fix this issue, be sure to include an OVER clause when calling the LEAD() function.

Continue reading