Home
GROUP BY should not be used in SQL SELECT statements
Description
The rule "GROUP BY should not be used in SQL SELECT statements" states that the GROUP BY clause should not be used when writing SQL SELECT statements. This rule is applicable to all SQL dialects, including PL/SQL. The GROUP BY clause is used to group rows into sets of summary rows by values of columns or expressions. When used in SELECT statements, the GROUP BY clause can lead to unexpected results and should be avoided. Instead, the ORDER BY clause should be used to sort the results of the query.
Key Benefits
- Performance: GROUP BY should not be used in SQL SELECT statements as it can lead to slower query performance.
- Data Integrity: GROUP BY can return incorrect results if the data is not sorted correctly, leading to data integrity issues.
- Complexity: GROUP BY can be difficult to understand and can add complexity to the query.
Non-compliant Code Example
SELECT
NAME as nameCol,
ADDRESS as addressCol,
CREDIT_LIMIT as creditLimitCol,
City
FROM
CUSTOMERS
Group By City --Non compliant code (Group By clause should not be used)