Joining a Table with Itself : Table Join « Table Joins « SQL Server / T-SQL

SQL Server / T-SQL
1. Aggregate Functions
2. Analytical Functions
3. Constraints
4. Cursor
5. Data Set
6. Data Type
7. Database
8. Date Timezone
9. Index
10. Insert Delete Update
11. Math Functions
12. Select Query
13. Sequence
14. Store Procedure Function
15. String Functions
16. Subquery
17. System
18. Table
19. Table Joins
20. Transact SQL
21. Transaction
22. Trigger
23. View
24. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
SQL Server / T-SQL » Table Joins » Table Join 
Joining a Table with Itself


14>
15CREATE TABLE department(dept_no   CHAR(4NOT NULL,
16>                         dept_name CHAR(25NOT NULL,
17>                         location  CHAR(30NULL)
18>
19insert into department values ('d1', 'developer',   'Dallas')
20insert into department values ('d2', 'tester',      'Seattle')
21insert into department values ('d3', 'marketing',  'Dallas')
22>
23select from department
24> GO

(rows affected)

(rows affected)

(rows affected)
dept_no dept_name                 location
------- ------------------------- ------------------------------
d1      developer                 Dallas
d2      tester                    Seattle
d3      marketing                 Dallas

(rows affected)
1> -- Joining a Table with Itself
2>
3> -- ANSI join syntax:
4>
5SELECT t1.dept_no, t1.dept_name, t1.location
6>          FROM department t1 JOIN department t2
7>            ON t1.location=t2.location
8>          WHERE t1.dept_no <> t2.dept_no
9> GO
dept_no dept_name                 location
------- ------------------------- ------------------------------
d3      marketing                 Dallas
d1      developer                 Dallas

(rows affected)
1> -- SQL Server join syntax:
2>
3SELECT DISTINCT t1.dept_no, t1.dept_name, t1.location
4>        FROM department t1, department t2
5>        WHERE t1. location = t2.location
6>        AND t1.dept_no <> t2.dept_no
7> GO
dept_no dept_name                 location
------- ------------------------- ------------------------------
d1      developer                 Dallas
d3      marketing                 Dallas

(rows affected)
1>
2> drop table department
3> GO
1>
2>
           
       
Related examples in the same category
1. SQL Server join syntax
2. SET SHOWPLAN_TEXT ON for a table join
3. Joins are advantageous over subqueries if the SELECT list in a query contains columns from more than one table
4. Use Count function in a left join
5. Table join with data returned from function
6. NOT EXISTS function with table join
7. Select columns from all tables
8. Column alias in table join
9. Using TOP in table join
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.