Solved

multiple column subquery

Posted on 2002-05-06
3
4,122 Views
Last Modified: 2010-04-27
Greetings,

Can someone point me to an equivalent statement that will run on both
ms sql server 7 and 2000?

I am trying to select some rows from a table where two column entries
match some other criteria. The Oracle equivalent is at the bottom of
this message... In MS SQL Server Query Analyzer I am getting errors
trying this:

        SELECT table1.col1, table1.col2, table1.col3
        FROM table1
        WHERE (table1.num, table1.code) in
          (SELECT table2.num, table2.code from table2
          where table2.col4 = value


The error is: "Incorrect Syntax near ',' ", so it seems to not like
the comma in "where (table1,num, table2.code) in"

The important thing to remember is that each row in one table will have the same two values as a row in the other table.

.. I did not want to concatenate the rows as it seems to deliver different results.

Thanks for any assistance,
Jeff

select (col1, col2, col3
from table1
where (num, code) in
(select num, code from table2 where col4 = value);

0
Comment
Question by:kishj
[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
3 Comments
 
LVL 3

Accepted Solution

by:
trouta earned 50 total points
ID: 6991723
The where exists clause will need to be used if you have multiple values to check for

SELECT table1.col1, table1.col2, table1.col3
FROM table1
WHERE EXISTS (SELECT * FROM TABLE2 WHERE table1.num = table2.num and table1.code = table2.code and table2.col4 = value)

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 6993429
You can also use a join to resolve this, like so:

SELECT table1.col1, table1.col2, table1.col3
    FROM table1
    INNER JOIN table2 ON table1.num = table2.num AND table1.code = table2.code
    WHERE table2.col4 = value

0
 
LVL 1

Expert Comment

by:usmanr
ID: 32265628
extending trouta's solution, you can also write it this way,,
select * from Address where exists (select 1 where City = 'Bothell' and AddressLine1 = '1970 Napa Ct.')
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
monitoring configuration for SQL server DB 32 46
how to check mis-spellings in a select SQL 2 37
Using this function 4 39
SQL Server Express automatically execute SQL or SP 8 30
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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