Subscribe to Subscribers

Online Members

baskar
More...

Resources » Code Snippets » SQL

Use of Cross Join


Posted Date:     Category: SQL    
Author: Member Level: Gold    Points: 10



Generally the cross join will used to get the possibilities among the two differnt set of data.

Cross Join will retun all the possible records between the two table.
Consider the each table has four rows then then result of corss join will return 4 x 4 = 16 Recrods.

Example:

create table Teams (id bigint identity(1,1), TeamName nvarchar(100))

insert into Teams
select 'India'
union all
select 'Australia'
union all
select 'Pakisthan'
union all
select 'Srilanka'

In the above table the number of team names are list. If we need to get the possibilities of matches between the teams for that kind of times we can go for cross joins.

select * from Teams A
cross join Teams B

This cross join result will give the 16 posibilities of matches between the four teams.

But the 16 possibilities inclues
Inida Vs India,
Australia Vs Australia
Srilanka Vs Srilanka
Pakisthan Vs Pakistan

For the first round of matches the above possibilities are not able to be a match. we need to avoid this combination of matches.

For this Problem

select * from Teams A
cross join Teams B
where A.id != B.id

If we use the above query then the result will be fine.

The result will give the posibilities in the real time matches.





Did you like this resource? Share it with your friends and show your love!


Responses to "Use of Cross Join "
Author: Christopher F    27 Jan 2010Member Level: Gold   Points : 0

hi
good posting,and need more explanation

thanks



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to Find, When Table is Modified Recently?
    Previous Resource: Table to Table Data Transfer
    Return to Resources
    Post New Resource
    Category: SQL


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Cross join  .  Use of Cross Join  .  

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.