• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 524
  • Last Modified:

Sql query combinational join

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

1 2
a c
b c

table A
3 4 5
c d e
e f g

Sybase database.

  • 2
  • 2
2 Solutions
Muhammad Ahmad ImranDatabase DeveloperCommented:
select col1,col2,col3 from table1 where col1 in (select col1 from table2) and col2 in (select col2 from table2)
Muhammad Ahmad ImranDatabase DeveloperCommented:

select col3,col4,col5 from table1 where col1 in (select col1 from table2) and col2 in (select col2 from table2)
Jan FranekCommented:
select col3, col4, col5
from tableA a, tableB b
where a.col1 = b.col1
and a.col2 = b.col2
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
Jan FranekCommented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now