UNION same type of columns from different tables : Union « Set Operations « SQL Server / T-SQL Tutorial

SQL Server / T-SQL Tutorial
1. Query
2. Insert Delete Update
3. Table
4. Table Join
5. Data Types
6. Set Operations
7. Constraints
8. Subquery
9. Aggregate Functions
10. Date Functions
11. Math Functions
12. String Functions
13. Data Convert Functions
14. Analytical Functions
15. Sequence Indentity
16. View
17. Index
18. Cursor
19. Database
20. Transact SQL
21. Procedure Function
22. Trigger
23. Transaction
24. XML
25. System Functions
26. System Settings
27. System Tables Views
28. User Role
29. CLR
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL Tutorial » Set Operations » Union 
6. 3. 3. UNION same type of columns from different tables
7CREATE TABLE Employees (
8>      EmployeeID int NOT NULL ,
9>      LastName nvarchar (20NOT NULL ,
10>     FirstName nvarchar (10NOT NULL ,
11>     Title nvarchar (30NULL ,
12>     TitleOfCourtesy nvarchar (25NULL ,
13>     BirthDate datetime NULL ,
14>     HireDate datetime NULL ,
15>     Address nvarchar (60NULL ,
16>     City nvarchar (15NULL ,
17>     Region nvarchar (15NULL ,
18>     PostalCode nvarchar (10NULL ,
19>     Country nvarchar (15NULL ,
20>     HomePhone nvarchar (24NULL ,
21>     Extension nvarchar (4NULL ,
22>     Photo image NULL ,
23>     Notes ntext NULL ,
24>     ReportsTo int NULL ,
25>     PhotoPath nvarchar (255NULL
26>
27)
28> GO
1>
2CREATE TABLE Customers (
3>      CustomerID nchar (5NOT NULL ,
4>      CompanyName nvarchar (40NOT NULL ,
5>      ContactName nvarchar (30NULL ,
6>      ContactTitle nvarchar (30NULL ,
7>      Address nvarchar (60NULL ,
8>      City nvarchar (15NULL ,
9>      Region nvarchar (15NULL ,
10>     PostalCode nvarchar (10NULL ,
11>     Country nvarchar (15NULL ,
12>     Phone nvarchar (24NULL ,
13>     Fax nvarchar (24NULL
14)
15> GO
1>
2INSERT Customers VALUES('1','A','Maria',    'Sales',  'Str. 57', 'Berlin'    ,NULL,'12209', 'Germany','111-1111111','111-1111111')
3INSERT Customers VALUES('2','M','Joe',      'Owner',  'Ave. 231','Vancouver' ,NULL,'05023', 'Mexico', '(222222-3332',NULL)
4INSERT Customers VALUES('3','H','Thomas',   'Sales',  'Sq.  111','London'    ,NULL,'1D00P', 'UK',     '(444444-4444','(444444-4444')
5INSERT Customers VALUES('4','B','Berg',     'Order',  'Blv    8','Toronto'   ,NULL,'00222', 'Sweden', '4444-55 55 65','5555-55 55 55')
6INSERT Customers VALUES('5','S','Moos',     'Sales',  'Fort  57','New York'  ,NULL,'68306', 'Germany','6666-66666','6666-77777')
7INSERT Customers VALUES('6','F','Cite',     'Manager','24      ','Dalles'    ,NULL,'67000', 'France', '88.60.15.31','88.60.15.32')
8INSERT Customers VALUES('7','C','Sommer',   'Owner',  'Araq, 67','Paris'     ,NULL,'28023', 'Spain',  '(91555 22 82','(91555 91 99')
9INSERT Customers VALUES('8','P','Leb',      'Owner',  '12      ','Beijing'   ,NULL,'13008', 'France', '91.24.45.40','91.24.45.41')
10INSERT Customers VALUES('9','D','Elizabeth','Manager','23 Blvd.','Tsawassen','BC', 'T2F8M4','Canada', '(604555-4729','(604555-3745')
11> go

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2>
3CREATE TABLE Suppliers (
4>      SupplierID int  NOT NULL ,
5>      CompanyName nvarchar (40NOT NULL ,
6>      ContactName nvarchar (30NULL ,
7>      ContactTitle nvarchar (30NULL ,
8>      Address nvarchar (60NULL ,
9>      City nvarchar (15NULL ,
10>     Region nvarchar (15NULL ,
11>     PostalCode nvarchar (10NULL ,
12>     Country nvarchar (15NULL ,
13>     Phone nvarchar (24NULL ,
14>     Fax nvarchar (24NULL ,
15>     HomePage ntext NULL
16)
17> GO
1>
2>
3INSERT Suppliers VALUES(1,'L','N','Manager','10','Van',NULL,'2800','Paris','1114108','43844115',NULL)
4INSERT Suppliers VALUES(2,'Z','D','Manager','22','Zaa',NULL,'9999 ZZ','USA',' 1212','(123451210',NULL)
5INSERT Suppliers VALUES(3,'K','A','Manager','12','Lap',NULL,'53120','Finland',' 10956',NULL,NULL)
6INSERT Suppliers VALUES(4,'G','W','Tester', '1 Hill','Sydney','NSW','2042','Australia','(02555-5914','(021555-2222',null)
7INSERT Suppliers VALUES(5,'M','J','Manager','St.','Mon','BC','H1J 1C3','Canada','(514555-9022',NULL,NULL)
8INSERT Suppliers VALUES(6,'P','G','Administrator','153','Sal',NULL,'84100','Italy','(0896547665','(0891111111',NULL)
9INSERT Suppliers VALUES(7,'E','M','Sales','22 Str','Mont',NULL,'71300','France','85.57.00.07',NULL,NULL)
10INSERT Suppliers VALUES(8,'G','E','Sales','B Ave','Ann',NULL,'74000','France','38.76.98.06','38.76.98.58',NULL)
11INSERT Suppliers VALUES(9,'F','C','Manager','Str','Ste','Calgary','J2S 7S8','Canada','(514555-2955','(514555-2921',NULL)
12> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2>
3>    SELECT CompanyName AS Name,
4>           Address,
5>           City,
6>           Region,
7>           PostalCode,
8>           Country
9>    FROM Customers
10>    UNION
11>    SELECT CompanyName,
12>           Address,
13>           City,
14>           Region,
15>           PostalCode,
16>           Country
17>    FROM Suppliers
18>    UNION
19>    SELECT FirstName + ' ' + LastName,
20>           Address,
21>           City,
22>           Region,
23>           PostalCode,
24>           Country
25>    FROM Employees
26> go
Name                                     Address                                                      City            Region          PostalCode Country
---------------------------------------- ------------------------------------------------------------ --------------- --------------- ---------- ---------------
A                                        Str. 57                                                      Berlin          NULL            12209      Germany
B                                        Blv    8                                                     Toronto         NULL            00222      Sweden
C                                        Araq, 67                                                     Paris           NULL            28023      Spain
D                                        23 Blvd.                                                     Tsawassen       BC              T2F8M4     Canada
E                                        22 Str                                                       Mont            NULL            71300      France
F                                        Str                                                        Ste             Calgary         J2S 7S8    Canada
F                                        24                                                           Dalles          NULL            67000      France
G                                        Hill                                                       Sydney          NSW             2042       Australia
G                                        B Ave                                                        Ann             NULL            74000      France
H                                        Sq.  111                                                     London          NULL            1D00P      UK
K                                        V 12                                                         Lap             NULL            53120      Finland
L                                        L 10                                                         Van             NULL            2800       Paris
M                                        St.                                                        Mon             BC              H1J 1C3    Canada
M                                        Ave. 231                                                     Vancouver       NULL            05023      Mexico
P                                        12                                                           Beijing         NULL            13008      France
P                                        V 153                                                        Sal             NULL            84100      Italy
S                                        Fort  57                                                     New York        NULL            68306      Germany
Z                                        V 22                                                         Zaa             NULL            9999 ZZ    USA

(18 rows affected)
1>
2> drop table Customers;
3> drop table Employees;
4> drop table Suppliers;
5> GO
6. 3. Union
6. 3. 1. Using Unions to Display Data from Multiple Queries
6. 3. 2. Listing the output from two identical tables.
6. 3. 3. UNION same type of columns from different tables
6. 3. 4. A union that combines information from the Billings table
6. 3. 5. A union that combines payment data from the same joined tables
6. 3. 6. OR operator can be used instead of the UNION operator, as the two equivalent examples.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.