views:

107

answers:

6

In some SQL dialects, you can state (something as):

SELECT * FROM SomeTable WHERE (val1,val2) IN 
  (SELECT val1,val2 FROM SomeOtherTable)

But I don't know how to do that in the TSQL (sql server 2k) I am using.

I am aware of (and using for now) workarounds like using joins or concatenated values, but is there some syntax in TSQL I am overlooking to do just that?

UPDATE : This is valid SQL-99 syntax, that's why I consider a join a workaround, even if it would be more performant. My question is maybe put better as :

Is there an implementation of this syntax in TSQL?

UPDATE2 : I just tested this syntax om Mysql and it works fine there.

+1  A: 

Joining would be the way to go here.

Kjensen
A: 

IN only works on single column arrays. Since you've explicitly stated that you want to do an IN clause:

SELECT * FROM SomeTable 
WHERE val1 IN (SELECT val1 FROM SomeOtherTable WHERE val2 = SomeTable.val2) 
AND val2 IN (SELECT val2 FROM SomeOtherTable WHERE val1 = SomeTable.val1)

Check out the documentation on it. Since there's a where clause which depended on each row (e.g.-WHERE SomeOtherTable.ID = SomeTable.MyOtherID), it will hose performance, and a join is absolutely the way to go.

Eric
Not the same, because the matching val1 and val2 might come from different rows in SomeOtherTable
Joel Coehoorn
That would not necessarily work, would it? It could give you a match if there is a row in SomeOtherTable that has val1 matching but not val2 (and vice versa) as long as there was another row that was able to match the other value.
TheTXI
*shakes angry fist at Joel*
TheTXI
You're right, don't know where my head was at.
Eric
in only works on single column arrays in all sql-server versions?
Peter
@Peter: According to W3C this is standard: http://www.w3schools.com/SQL/sql_in.asp
Eric
A: 

And what about two subselects?

erenon
No, same problem as Eric's answer.
Joel Coehoorn
you're looking for presence of a tuple; independent sub-selects won't give you this
kdgregory
@Joel: Not if you did the subselects correctly :)
Eric
+3  A: 

There is not a sql server implementation of that syntax. You'll have to do something like this:

SELECT st.*
FROM SomeTable st
  INNER JOIN  
  (
    SELECT val1, val2 
    FROM SomeOtherTable 
    GROUP BY val1, val2
  ) sot ON sot.val1= st.val1 AND sot.val2 = st.val2
Joel Coehoorn
In no single version? Not even 2008?
Peter
No, it's not in 2008.
Joel Coehoorn
+7  A: 
SELECT  *
FROM    SomeTable st
WHERE   EXISTS
        (
        SELECT  1
        FROM    SomeOtherTable sot
        WHERE   sot.val1 = st.val1
                AND sot.val2 = st.val2
        )

This is actually what an IN construct is optimized to with any SEMI JOIN method.

As for your question,

Is there an implementation of this syntax in T-SQL?

the answer is no

As documentation says:

subquery that has a result set of one column. This column must have the same data type as test_expression.

Quassnoi
+1, good substitute, but still no answer on the question
Peter
tx .
Peter
@Peter: how did you make such a short comment?
Quassnoi
haha, just add some spaces, then a point :-)
Peter
tx .
Quassnoi
@Peter: … and I thought I know something of computers …
Quassnoi
A: 
Select SomeTable.*
FROM SomeTable, SomeOtherTable
WHERE SomeTable.Val1 = SomeOtherTable.Val1
    And SomeTable.Val2 = SoemeOtherTable.Val2

You should not consider this a "workaround." This is the most basic and standard way of accomplishing what you want to accomplish.

Edit: It may not be your standard "JOIN" syntax, but it's a habit.

TheTXI
This could cause duplicate results if SomeOtherTable has more than one record with val1 and val2 columns that match a record from SomeTable.
Joel Coehoorn
Also, I really want to downvote the "Table1,Table2" syntax.
Joel Coehoorn
Good point Joel. Didn't think about the duplicate results being an unnecessary side effect.
TheTXI
Joel: Old habits are hard to break (concerning the syntax). That was the way I was originally taught and it stuck with me for this long because when I'm constructing a query in my head that's how it sounds instead of thinking about the word JOIN.
TheTXI
@theTXI I do consider join a workaround, for my example is valid SQL99 syntax
Peter
@TXI: It was a habit for me for a long time, too. I started doing it on here as "good practice," and it's broken my habit, methinks.
Eric
Eric: I try to do the same most times, but every once in a while I'll let something like this slip in when I start thinking too fast.
TheTXI
@TheTXI: my name is Quassnoi and I am a joinless syntax user. But I made significant progress since I joined Stack Overflow, and for now I almost gave up using (+) for OUTER JOINs in Oracle even in projects nobody but me sees.
Quassnoi