Previous Code Entry
Left Arrow
SQL Server Select Statement and Delete
Current Code Entry
Next Code Entry
SQL Server - Return only records where the count is not 0
Right Arrow
SQL Server - SELECT UNION Will not retrieve proper records
Article Entry Date: March 20,2024 @ 01:45:00 / Last Updated On: March 20,2024 @ 01:45:00
When using a UNION in your Select Statement, you must ensure you use a WHERE statement to return the correct records.

I am using this example.

The above statement will return all records with the ID of 1.
Now, let's say we have our table connected to another table to ensure the returned ordered numbers are correct.
We could do this.

Now, this will return the records for ID=1. However, this will also return records for 2, 3, 4, 5, etc.
This code lacks one thing: it does not look at where Tbl1 needs to get its records from.
To retrieve the records for each Query, we need to ensure we have a WHERE Statement at every corner.

The above code will now only retrieve records for the ID that is the same across both tables.