Skip to content

Using keyword "UNION" in queries (UnionAll)

Type Scope Severity Activated
by default
Minutes
to fix
Tags
Code smell BSL Minor Yes 5 standard
sql
performance

Description

In most cases, when you need to combine the results of two or more queries into a single result set, employ UNION ALL clause instead of UNION. The recommendation is based on the algorithm of the UNION clause, which searches for and removes duplicates from the united result even when duplicates are impossible by the query design.

Employ UNION only when removing duplicates from the result is required.

Examples

Incorrect:

SELECT
GoodsReceipt.Ref
FROM
Document.GoodsReceipt AS GoodsReceipt

UNION

SELECT
GoodsSale.Ref
FROM
Document.GoodsSale AS GoodsSale

Correct:

SELECT
GoodsReceipt.Ref
FROM
Document.GoodsReceipt AS GoodsReceipt

UNION ALL

SELECT
GoodsSale.Ref
FROM
Document.GoodsSale AS GoodsSale

Sources

Snippets

Diagnostic ignorance in code

// BSLLS:UnionAll-off
// BSLLS:UnionAll-on

Parameter for config

"UnionAll": false