Table alias during table join : Table alias « Table « SQL Server / T-SQL Tutorial






4> CREATE TABLE Customers (
5>      CustomerID nchar (5) NOT NULL ,
6>      CompanyName nvarchar (40) NOT NULL ,
7>      ContactName nvarchar (30) NULL ,
8>      ContactTitle nvarchar (30) NULL ,
9>      Address nvarchar (60) NULL ,
10>     City nvarchar (15) NULL ,
11>     Region nvarchar (15) NULL ,
12>     PostalCode nvarchar (10) NULL ,
13>     Country nvarchar (15) NULL ,
14>     Phone nvarchar (24) NULL ,
15>     Fax nvarchar (24) NULL
16> )
17> GO
1>
2> INSERT Customers VALUES('1','A','Maria',    'Sales',  'Str. 57', 'Berlin'    ,NULL,'12209', 'Germany','111-1111111','111-1111111')
3> INSERT Customers VALUES('2','M','Joe',      'Owner',  'Ave. 231','Vancouver' ,NULL,'05023', 'Mexico', '(222) 222-3332',NULL)
4> INSERT Customers VALUES('3','H','Thomas',   'Sales',  'Sq.  111','London'    ,NULL,'1D00P', 'UK',     '(444) 444-4444','(444) 444-4444')
5> INSERT Customers VALUES('4','B','Berg',     'Order',  'Blv    8','Toronto'   ,NULL,'00222', 'Sweden', '4444-55 55 65','5555-55 55 55')
6> INSERT Customers VALUES('5','S','Moos',     'Sales',  'Fort  57','New York'  ,NULL,'68306', 'Germany','6666-66666','6666-77777')
7> INSERT Customers VALUES('6','F','Cite',     'Manager','24      ','Dalles'    ,NULL,'67000', 'France', '88.60.15.31','88.60.15.32')
8> INSERT Customers VALUES('7','C','Sommer',   'Owner',  'Araq, 67','Paris'     ,NULL,'28023', 'Spain',  '(91) 555 22 82','(91) 555 91 99')
9> INSERT Customers VALUES('8','P','Leb',      'Owner',  '12      ','Beijing'   ,NULL,'13008', 'France', '91.24.45.40','91.24.45.41')
10> INSERT Customers VALUES('9','D','Elizabeth','Manager','23 Blvd.','Tsawassen','BC', 'T2F8M4','Canada', '(604) 555-4729','(604) 555-3745')
11> 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>
2>
3> CREATE TABLE Orders (
4>      OrderID int NOT NULL ,
5>      CustomerID nchar (5) NULL ,
6>      EmployeeID int NULL ,
7>      OrderDate datetime NULL ,
8>      RequiredDate datetime NULL ,
9>      ShippedDate datetime NULL ,
10>     ShipVia int NULL ,
11>     Freight money NULL DEFAULT (0),
12>     ShipName nvarchar (40) NULL ,
13>     ShipAddress nvarchar (60) NULL ,
14>     ShipCity nvarchar (15) NULL ,
15>     ShipRegion nvarchar (15) NULL ,
16>     ShipPostalCode nvarchar (10) NULL ,
17>     ShipCountry nvarchar (15) NULL
18> )
19> GO
1>
2>
3> INSERT INTO Orders VALUES (10248,'1',5,'7/4/1996','8/1/2001','7/16/2001',3,32.38,'V','A','R',        NULL,N'51100','France')
7> go
1>
2>
3>
4>
5>    SELECT c.CustomerID, CompanyName
6>    FROM Customers c
7>    LEFT OUTER JOIN Orders o
8>                 ON c.CustomerID = o.CustomerID
9>    WHERE o.CustomerID IS NULL
10>
11> GO
CustomerID CompanyName
---------- ----------------------------------------
1          A
2          M
3          H
4          B
5          S
6          F
7          C
8          P
9          D

(9 rows affected)
1>
2> drop table orders;
3> drop table customers;
4> GO
1>
2>








3.6.Table alias
3.6.1.Reference column with table name
3.6.2.Table alias during table join
3.6.3.Ambiguous column name during table join
3.6.4.Query two tables with column in common
3.6.5.Column name with or without a table alias