Rule: SELECT Modifiers Must Be on the Same Line as SELECT

Rule Code: LT10

Name: select_modifiers_check

Overview

In SQL queries, modifiers such as DISTINCT, ALL, or TOP should be placed on the same line as the SELECT keyword. This best practice improves the readability and consistency of the query, ensuring that the modifier’s relationship to the SELECT statement is clear. Placing modifiers on a separate line can make the query harder to read and may lead to confusion.

Explanation

Anti-pattern: SELECT Modifiers on a Separate Line

When modifiers like DISTINCT are placed on a separate line from the SELECT keyword, it can break the flow of the query and make it harder to understand. This layout may cause unnecessary confusion for developers when reading or modifying the query.

Example of SELECT Modifiers on a Separate Line (Anti-pattern):

SELECT
    DISTINCT
    foo.id,
    foo.name
FROM foo;

In this example, DISTINCT is placed on a separate line, which breaks the connection between the SELECT keyword and its modifier, making the query less readable.

Best Practice: SELECT Modifiers on the Same Line

For clarity and readability, any modifiers such as DISTINCT should be placed on the same line as the SELECT keyword. This keeps the query concise and ensures that the intent of the modifier is immediately clear to anyone reading the code.

Refactored Example with SELECT Modifiers on the Same Line (Best Practice):

SELECT DISTINCT
    foo.id,
    foo.name
FROM foo;

In this refactored example, the DISTINCT modifier is placed directly after the SELECT keyword on the same line, making the query clearer and more readable.

Conclusion

Keeping SELECT modifiers on the same line as the SELECT keyword ensures that SQL queries are more readable and consistent. Following this rule helps developers understand the structure and intent of the query more easily, leading to better maintainability.

Groups: