Solved

Sql query combinational join

Posted on 2010-11-29
5
496 Views
Last Modified: 2012-08-13
Say i have a table A and table B

table A has 5 columns - 1,2 ,3,4,5
table B has 2 columns -1,2

I need to select the data from table A(3,4,5) for which the combination of data in table A and B must be same(table A 1,2 and table B 1,2)

table A
1 2 3 4 5
a b c d e
a c c d e
b c e f g

tableB
1 2
a c
b c

output:
table A
3 4 5
c d e
e f g

Sybase database.

0
Comment
Question by:magento
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Expert Comment

by:leoahmad
ID: 34231411
select col1,col2,col3 from table1 where col1 in (select col1 from table2) and col2 in (select col2 from table2)
0
 
LVL 14

Assisted Solution

by:leoahmad
leoahmad earned 200 total points
ID: 34231420
better

select col3,col4,col5 from table1 where col1 in (select col1 from table2) and col2 in (select col2 from table2)
0
 
LVL 14

Accepted Solution

by:
Jan_Franek earned 300 total points
ID: 34231437
select col3, col4, col5
from tableA a, tableB b
where a.col1 = b.col1
and a.col2 = b.col2
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34232537
Jan's query is correct
However, I would use the Ansi style join, and also add the alias prefix to all columns as good standard practice

select a.col3, a.col4, a.col5
from tableA a
inner join tableB b on a.col1 = b.col1 and a.col2 = b.col2
0
 
LVL 14

Expert Comment

by:Jan_Franek
ID: 34236871
Author did not specify, what version of Sybase database is he using. AFAIK some old versions did not support ANSI joins, so I decided to use non-ANSI supported on all Sybase databases.
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question