Listing the output from two identical tables. : Union « Set Operations « SQL Server / T-SQL Tutorial






3> CREATE TABLE SalesMw (
4>     StoreID            integer NOT NULL,
5>     CD_ID               integer  NOT NULL,
6>     QtySold            integer,
7>     SalesDate          datetime)
8> GO
1> INSERT into SalesMw VALUES(1300,2001,10,"Oct 31,1997")
2> INSERT into SalesMw VALUES(1300,2002,15,"Oct 31,1997")
3> INSERT into SalesMw VALUES(1300,2001,5,"Nov 30,1997")
4> INSERT into SalesMw VALUES(1300,2003,16,"Nov 30,1997")
5> INSERT into SalesMw VALUES(1300,2017,9,"Nov 30,1997")
6> INSERT into SalesMw VALUES(1330,2000,2,"Oct 31,1997")
7> INSERT into SalesMw VALUES(1330,2000,109,"Nov 30,1997")
8> INSERT into SalesMw VALUES(1330,2029,5,"Nov 30,1997")
9> INSERT into SalesMw VALUES(1330,2030,5,"Nov 30,1997")
10> INSERT into SalesMw VALUES(1330,2015,20,"Nov 30,1997")
11> INSERT into SalesMw VALUES(1330,2016,66,"Nov 30,1997")
12> INSERT into SalesMw VALUES(1310,2005,11,"Nov 30,1997")
13> INSERT into SalesMw VALUES(1320,2022,14,"Nov 30,1997")
14> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
1>
2> CREATE TABLE SalesNe (
3>      StoreID           integer NOT NULL,
4>      CD_ID              integer NOT NULL,
5>      QtySold           integer,
6>      SalesDate         datetime)
7>
8> INSERT into SalesNe VALUES(1200,2006,10,"Oct 31,1997")
9> INSERT into SalesNe VALUES(1200,2007,15,"Oct 31,1997")
10> INSERT into SalesNe VALUES(1200,2007,8,"Nov 30,1997")
11> INSERT into SalesNe VALUES(1200,2008,12,"Nov 30,1997")
12> INSERT into SalesNe VALUES(1200,2009,19,"Nov 30,1997")
13> INSERT into SalesNe VALUES(1210,2008,7,"Oct 31,1997")
14> INSERT into SalesNe VALUES(1210,2009,55,"Nov 30,1997")
15> INSERT into SalesNe VALUES(1210,2010,18,"Nov 30,1997")
16> INSERT into SalesNe VALUES(1210,2022,11,"Nov 30,1997")
17> INSERT into SalesNe VALUES(1210,2023,20,"Nov 30,1997")
18> INSERT into SalesNe VALUES(1210,2020,15,"Nov 30,1997")
19> INSERT into SalesNe VALUES(1220,2018,6,"Nov 30,1997")
20> INSERT into SalesNe VALUES(1220,2005,3,"Nov 30,1997")
21>
22>
23>
24>
25>
26>
27>
28>
29>
30> SELECT * from SalesMw
31> UNION
32> SELECT * from SalesNe
33>
34> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)

(1 rows affected)
StoreID     CD_ID        QtySold     SalesDate
----------- ----------- ----------- -----------------------
       1200        2006          10 1997-10-31 00:00:00.000
       1200        2007           8 1997-11-30 00:00:00.000
       1200        2007          15 1997-10-31 00:00:00.000
       1200        2008          12 1997-11-30 00:00:00.000
       1200        2009          19 1997-11-30 00:00:00.000
       1210        2008           7 1997-10-31 00:00:00.000
       1210        2009          55 1997-11-30 00:00:00.000
       1210        2010          18 1997-11-30 00:00:00.000
       1210        2020          15 1997-11-30 00:00:00.000
       1210        2022          11 1997-11-30 00:00:00.000
       1210        2023          20 1997-11-30 00:00:00.000
       1220        2005           3 1997-11-30 00:00:00.000
       1220        2018           6 1997-11-30 00:00:00.000
       1300        2001           5 1997-11-30 00:00:00.000
       1300        2001          10 1997-10-31 00:00:00.000
       1300        2002          15 1997-10-31 00:00:00.000
       1300        2003          16 1997-11-30 00:00:00.000
       1300        2017           9 1997-11-30 00:00:00.000
       1310        2005          11 1997-11-30 00:00:00.000
       1320        2022          14 1997-11-30 00:00:00.000
       1330        2000           2 1997-10-31 00:00:00.000
       1330        2000         109 1997-11-30 00:00:00.000
       1330        2015          20 1997-11-30 00:00:00.000
       1330        2016          66 1997-11-30 00:00:00.000
       1330        2029           5 1997-11-30 00:00:00.000
       1330        2030           5 1997-11-30 00:00:00.000

(26 rows affected)
1>
2> drop table SalesNe;
3> 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.