Pages

Tuesday, 11 October 2011

SQL UNION Operator




The SQL UNION operator combines two or more SELECT statements.

The SQL UNION Operator

The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

SQL UNION Syntax

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
Note: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.

SQL UNION ALL Syntax

SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
PS: The column names in the result-set of a UNION are always equal to the column names in the first SELECT statement in the UNION.

SQL UNION Example

Look at the following tables:
"Employees_Norway":
E_IDE_Name
01Hansen, Ola
02Svendson, Tove
03Svendson, Stephen
04Pettersen, Kari
"Employees_USA":
E_IDE_Name
01Turner, Sally
02Kent, Clark
03Svendson, Stephen
04Scott, Stephen
Now we want to list all the different employees in Norway and USA.
We use the following SELECT statement:
SELECT E_Name FROM Employees_Norway
UNION
SELECT E_Name FROM Employees_USA
The result-set will look like this:
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen
Note: This command cannot be used to list all employees in Norway and USA. In the example above we have two employees with equal names, and only one of them will be listed. The UNION command selects only distinct values.

SQL UNION ALL Example

Now we want to list all employees in Norway and USA:
SELECT E_Name FROM Employees_Norway
UNION ALL
SELECT E_Name FROM Employees_USA
Result
E_Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen


9 comments:

  1. My programmer is trying to convince me to move
    to .net from PHP. I have always disliked the idea because
    of the expenses. But he's tryiong none the less. I've been using
    WordPress on various websites for about a year and am concerned about switching to another platform.
    I have heard very good things about blogengine.net.
    Is there a way I can import all my wordpress content into
    it? Any help would be really appreciated!
    Also visit my blog ; Western Australia

    ReplyDelete
  2. Hi there! I just wanted to ask if you ever have any trouble
    with hackers? My last blog (wordpress) was hacked and I ended up losing months of hard work due to no backup.
    Do you have any methods to stop hackers?
    Also visit my website : chiropractor

    ReplyDelete
  3. Great work! This is the kind of information that are meant to
    be shared across the web. Disgrace on the seek engines for
    no longer positioning this publish higher!

    Come on over and discuss with my web site .
    Thanks =)
    My website: Photography Tricks

    ReplyDelete
  4. I could not refrain from commenting. Very well
    written!
    My webpage: building your network marketing business

    ReplyDelete
  5. What's up, yes this post is really nice and I have learned lot of things from it concerning blogging. thanks.
    Here is my website : good remodeling company in winter garden florida

    ReplyDelete
  6. Hello, I enjoy reading through your post. I like to write a
    little comment to support you.
    Also visit my website : replica Pro beats headphones by dre

    ReplyDelete
  7. Appreciate the recommendation. Let me try it out.
    Also visit my website ; Family vacation at Upper James Manor

    ReplyDelete
  8. My spouse and I stumbled over here by a different page and thought I might
    check things out. I like what I see so now i'm following you. Look forward to looking over your web page again.

    My weblog ... Vintage Moebel Karlsruhe

    ReplyDelete
  9. It's perfect time to make some plans for the long run and it is time to be happy. I have read this put up and if I could I want to counsel you some attention-grabbing issues or advice. Perhaps you could write next articles referring to this article. I wish to read even more issues about it!

    my blog post - Best Baby Monitors

    ReplyDelete