Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UNION works implizit with DISTINCT [CORE1917] #2350

Closed
firebird-automations opened this issue May 27, 2008 · 5 comments
Closed

UNION works implizit with DISTINCT [CORE1917] #2350

firebird-automations opened this issue May 27, 2008 · 5 comments

Comments

@firebird-automations
Copy link
Collaborator

Submitted by: Joe Blocher (joblo)

a simple union statement
----------------------------------
SELECT FieldX as Value FROM TABLE_A
UNION
SELECT FieldY as Value FROM TABLE_B

for example:
-------------------------------
TABLE_A has 2 rows with the same value in FieldX
TABLE_B has 1 row with another value in FieldY

The single statement "SELECT FieldX as Value FROM TABLE_A" returns 2 rows
The single statement "SELECT FieldY as Value FROM TABLE_B" returns 1 row
But the above UNION statement only returns 2 rows??

------------------------------------------------
That means, it acts implizit like:

SELECT DISTINCT FieldX as Value FROM TABLE_A
UNION
SELECT DISTINCT FieldY as Value FROM TABLE_B

I think, this is not correct, in a more complex query it can be very dangerous.

you can test it immediatly - not exactly the same, but in my opinion a bug too:
------------------------------------------------
SELECT 1, RDB$RELATION_ID FROM RDB$DATABASE
UNION
SELECT 1, RDB$RELATION_ID FROM RDB$DATABASE

compare to
------------------------------------------------
SELECT 1, RDB$RELATION_ID FROM RDB$DATABASE
UNION
SELECT 2, RDB$RELATION_ID FROM RDB$DATABASE

@firebird-automations
Copy link
Collaborator Author

Commented by: @hvlad

UNION, by default and by SQL standard, removed duplicates.
Learn about UNION ALL.

@firebird-automations
Copy link
Collaborator Author

Modified by: @hvlad

status: Open [ 1 ] => Resolved [ 5 ]

resolution: Won't Fix [ 2 ]

@firebird-automations
Copy link
Collaborator Author

Modified by: Joe Blocher (joblo)

description: the following sql-statement should return 2 rows, but actually returns only 1
in a more complex query it can be dangerous:
if rows from different tables return the same values, UNION acts as a GROUP BY clause,
I think, this is not correct

---------------------------------------------
SELECT FieldX as Value FROM TABLE_A
UNION
SELECT FieldY as Value FROM TABLE_B

you can test immediatly - should return 2 rows too:
------------------------------------------------
SELECT 1 FROM RDB$DATABASE
UNION
SELECT 1 FROM RDB$DATABASE

=>

a simple union statement
----------------------------------
SELECT FieldX as Value FROM TABLE_A
UNION
SELECT FieldY as Value FROM TABLE_B

for example:
-------------------------------
TABLE_A has 2 rows with the same value in FieldX
TABLE_B has 1 row with another value in FieldY

The single statement "SELECT FieldX as Value FROM TABLE_A" returns 2 rows
The single statement "SELECT FieldY as Value FROM TABLE_B" returns 1 row
But the above UNION statement only returns 2 rows??

------------------------------------------------
That means, it acts implizit like:

SELECT DISTINCT FieldX as Value FROM TABLE_A
UNION
SELECT DISTINCT FieldY as Value FROM TABLE_B

I think, this is not correct, in a more complex query it can be very dangerous.

you can test it immediatly - not exactly the same, but in my opinion a bug too:
------------------------------------------------
SELECT 1, RDB$RELATION_ID FROM RDB$DATABASE
UNION
SELECT 1, RDB$RELATION_ID FROM RDB$DATABASE

compare to
------------------------------------------------
SELECT 1, RDB$RELATION_ID FROM RDB$DATABASE
UNION
SELECT 2, RDB$RELATION_ID FROM RDB$DATABASE

summary: UNION works implizit as GROUP BY => UNION works implizit with DISTINCT

@firebird-automations
Copy link
Collaborator Author

Commented by: @pmakowski

It is conform to SQL standard
and it is the diferrence between UNION and UNION ALL
see for example :http://www.w3schools.com/sql/sql_union.asp
or http://www.tizag.com/sqlTutorial/sqlunion.php

@firebird-automations
Copy link
Collaborator Author

Modified by: @pmakowski

status: Resolved [ 5 ] => Closed [ 6 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant