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_ID | E_Name |
---|---|
01 | Hansen, Ola |
02 | Svendson, Tove |
03 | Svendson, Stephen |
04 | Pettersen, Kari |
"Employees_USA":
E_ID | E_Name |
---|---|
01 | Turner, Sally |
02 | Kent, Clark |
03 | Svendson, Stephen |
04 | Scott, 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 |
My programmer is trying to convince me to move
ReplyDeleteto .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
Hi there! I just wanted to ask if you ever have any trouble
ReplyDeletewith 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
Great work! This is the kind of information that are meant to
ReplyDeletebe 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
I could not refrain from commenting. Very well
ReplyDeletewritten!
My webpage: building your network marketing business
What's up, yes this post is really nice and I have learned lot of things from it concerning blogging. thanks.
ReplyDeleteHere is my website : good remodeling company in winter garden florida
Hello, I enjoy reading through your post. I like to write a
ReplyDeletelittle comment to support you.
Also visit my website : replica Pro beats headphones by dre
Appreciate the recommendation. Let me try it out.
ReplyDeleteAlso visit my website ; Family vacation at Upper James Manor
My spouse and I stumbled over here by a different page and thought I might
ReplyDeletecheck 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
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!
ReplyDeletemy blog post - Best Baby Monitors