Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Are these two queries the same?

Author  Topic 

dogli
Starting Member

32 Posts

Posted - 2002-10-07 : 11:04:21
Hi SQL Guru here,

I have a questions about the following two queries:

1) Select * from tableA
where ((columnA ='aa' And ColumnB='BB')or (columnA ='aa' And ColumnB='cc')or (columnA ='dd' And ColumnB='BB'))

2) Select * from tableA
where (columnA in ('aa','dd') and columnB in ('BB','CC'))

Can I get the same results with them?

Thanks

nr
SQLTeam MVY

12543 Posts

Posted - 2002-10-07 : 11:15:09
nope.
The second will include columnA ='dd' And ColumnB='CC'
whereas the first won't.
Select * from tableA
where ((columnA = 'aa' and columnB in ('BB','CC')
or (columnA = 'dd' and ColumnB='BB'))


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

monkeybite
Posting Yak Master

152 Posts

Posted - 2002-10-07 : 11:15:31
My initial thought is no. With the second query you will return rows where columnA = 'dd' and columnB = 'cc', which is not possible in the first query.

-- monkey

Go to Top of Page

dogli
Starting Member

32 Posts

Posted - 2002-10-07 : 11:35:11
Thanks! Both of you are right,I have changed the query!

Go to Top of Page

ksw
Starting Member

24 Posts

Posted - 2002-10-09 : 15:44:01
For readability, I like writing these types of where clauses as follows.

SELECT *
FROM tableA
WHERE ColumnA + ColumnB IN ('aabb', 'aacc', 'ddbb')

--KSW

Go to Top of Page

Sitka
Aged Yak Warrior

571 Posts

Posted - 2002-10-12 : 19:50:21
thanks ksw

Voted best SQL forum nickname...."Tutorial-D"
Go to Top of Page
   

- Advertisement -