Column name with or without a table alias : Table alias « Table « SQL Server / T-SQL Tutorial

Home
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
SQL Server / T-SQL Tutorial » Table » Table alias 
3.6.5.Column name with or without a table alias
2>
3CREATE TABLE stores(
4>    stor_id        char(4)           NOT NULL,
5>    stor_name      varchar(40)           NULL,
6>    stor_address   varchar(40)           NULL,
7>    city           varchar(20)           NULL,
8>    state          char(2)               NULL,
9>    zip            char(5)               NULL
10)
11> GO
1insert stores values('1','B','56Ave.','Tustin',   'CA','92789')
2insert stores values('2','N','57St.', 'Los Gatos','CA','96745')
3insert stores values('3','T','67St.', 'Portland', 'OR','89076')
4insert stores values('4','F','89  St.', 'Fremont',  'CA','90019')
5> GO

(rows affected)

(rows affected)

(rows affected)

(rows affected)
1>
2>
3CREATE TABLE discounts(
4>    discounttype   varchar(40)       NOT NULL,
5>    stor_id        char(4NULL              ,
6>    lowqty         smallint              NULL,
7>    highqty        smallint              NULL,
8>    discount       dec(4,2)          NOT NULL
9)
10> GO
1>
2insert discounts values('Initial Customer',  NULL,   NULL, NULL, 10.5)
3insert discounts values('Volume Discount',   NULL,   100,  10006.7)
4insert discounts values('Customer Discount', '8042', NULL, NULL, 5.0)
5> GO

(rows affected)

(rows affected)

(rows affected)
1>
2>    SELECT discounttype, discount, s.stor_name
3>    FROM discounts d
4>    JOIN stores s
5>      ON d.stor_id = s.stor_id
6> GO
discounttype                             discount stor_name
---------------------------------------- -------- ----------------------------------------

(rows affected)
1>
2> drop table stores;
3> drop table discounts;
4> GO
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.