SQL SERVER – Three Rules to Use UNION

I have previously written two articles on UNION and they are quite popular. I was reading SQL book Sams Teach Yourself Microsoft SQL Server T-SQL in 10 Minutes By Ben Forta and I came across three rules of UNION and I felt like mentioning them here.

UNION RULES

  • A UNION must be composed of two or more SELECT statements, each separated by the keyword UNION.
  • Each query in a UNION must contain the same columns, expressions, or aggregate functions, and they must be listed in the same order.
  • Column datatypes must be compatible: They need not be the same exact same type, but they must be of a type that SQL Server can implicitly convert.

SQL SERVER – Union vs. Union All – Which is better for performance?
SQL SERVER – Insert Multiple Records Using One Insert Statement – Use of UNION ALL

Reference : Pinal Dave (http://blog.SQLAuthority.com), Ben Forta

About these ads

4 thoughts on “SQL SERVER – Three Rules to Use UNION

  1. Pingback: SQL SERVER - Introduction and Example of UNION and UNION ALL Journey to SQL Authority with Pinal Dave

  2. Suppose, I don’t have same number of parameters for second table. I want one field which is not present in second table. But, I want to keep that column. It should show null values for second table which doesn’t have that field and should show data for the table which has that field.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s