-
Aug 12th, 2024, 10:30 AM
#1
Thread Starter
Addicted Member
A UNION SQL query got me today.
Sorry in advance if it isn't appropriate to put this here.
Say you have Select_Query_A UNION Select_Query_B
I understood that if both queries returned the same row that only one would appear in the recordset returned.
What I wasn't expecting was that the UNION will also removes duplicates within the results of each subquery.
But that seems to be the case.
Can someone confirm please if they know? Thanks
-
Aug 12th, 2024, 10:44 AM
#2
Fanatic Member
Re: A UNION SQL query got me today.
-
Aug 12th, 2024, 11:23 AM
#3
Thread Starter
Addicted Member
Re: A UNION SQL query got me today.
Originally Posted by qvb6
You are correct but it was the removal of the duplicates internal to the separate queries that caught me out, I wasn't expecting that to happen when I replaced 'Select * from' with 'select col1, col2, col3 from'
As far as I can see, UNION behaves like UNION ALL when the subqueries are of the form 'Select * from' or when your list of cols to be returned includes the primary key.
-
Aug 13th, 2024, 12:56 AM
#4
Re: A UNION SQL query got me today.
Originally Posted by vbrad
Sorry in advance if it isn't appropriate to put this here.
Say you have Select_Query_A UNION Select_Query_B
I understood that if both queries returned the same row that only one would appear in the recordset returned.
What I wasn't expecting was that the UNION will also removes duplicates within the results of each subquery.
But that seems to be the case.
Can someone confirm please if they know? Thanks
Of course UNION deduplicates: https://www.shiksha.com/online-cours...and-union-all/
The UNION command combines two or more SELECT statements but removes duplicates from the final result set,
In a nutshell it's the same as
Code:
SELECT DISTINCT * FROM
(SELECT SomeFields FROM Table1
UNION ALL
SELECT SomeFields FROM Table2)
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Aug 14th, 2024, 02:14 AM
#5
Thread Starter
Addicted Member
Re: A UNION SQL query got me today.
Originally Posted by Zvoni
Yes I know that now.
I had a UNION query that had worked for years purely because all the SELECTs in it were 'SELECT * from '
I'd just not imagined that returning fewer columns (by replacing the * with named cols) would also result in fewer rows and
couldn't understand that at first. It was a real gotcha for me.
-
Aug 14th, 2024, 02:28 AM
#6
Re: A UNION SQL query got me today.
Originally Posted by vbrad
Yes I know that now.
I had a UNION query that had worked for years purely because all the SELECTs in it were 'SELECT * from '
I'd just not imagined that returning fewer columns (by replacing the * with named cols) would also result in fewer rows and
couldn't understand that at first. It was a real gotcha for me.
Again a nail in the coffin of "SELECT * FROM...."
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Aug 14th, 2024, 02:36 AM
#7
Thread Starter
Addicted Member
Re: A UNION SQL query got me today.
Originally Posted by Zvoni
Again a nail in the coffin of "SELECT * FROM...."
Or alternatively I know just enough SQL to be dangerous
-
Aug 14th, 2024, 02:57 AM
#8
Re: A UNION SQL query got me today.
Originally Posted by vbrad
Or alternatively I know just enough SQL to be dangerous
That's the thing: If you use a SELECT * FROM on a well formed Table (AutoInc Primary Key etc.), then you should never get duplicate records!
If you do get duplicates, then there is something seriously wrong with the table
Doing a SELECT * FROM on a well formed VIEW not withstanding
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Aug 14th, 2024, 03:17 AM
#9
Thread Starter
Addicted Member
Re: A UNION SQL query got me today.
Originally Posted by Zvoni
That's the thing: If you use a SELECT * FROM on a well formed Table (AutoInc Primary Key etc.), then you should never get duplicate records!
If you do get duplicates, then there is something seriously wrong with the table
Doing a SELECT * FROM on a well formed VIEW not withstanding
Hi zvoni, not sure if we're talking at cross purposes.
The SELECT * wasn't returning duplicate records in my case because there was a pk.
For example if the full table was
id, surname, first
1, doe, john
2, doe, john
'SELECT surname, first from table' has a duplicate 'SELECT * from table' doesn't
SELECT left the duplicate in (as was desired since they're different john does) but the subsequent UNION stripped it out, which I hadn't expected because of lack of knowledge on my part.
-
Aug 15th, 2024, 08:51 AM
#10
Re: A UNION SQL query got me today.
Originally Posted by vbrad
Hi zvoni, not sure if we're talking at cross purposes.
The SELECT * wasn't returning duplicate records in my case because there was a pk.
For example if the full table was
id, surname, first
1, doe, john
2, doe, john
'SELECT surname, first from table' has a duplicate 'SELECT * from table' doesn't
SELECT left the duplicate in (as was desired since they're different john does) but the subsequent UNION stripped it out, which I hadn't expected because of lack of knowledge on my part.
Ahhhh.... OK, now i've got you.
Well, now you know: A UNION always goes on the FINAL resultset
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|