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
 General SQL Server Forums
 New to SQL Server Programming
 Sql select question

Author  Topic 

naveen22
Starting Member

2 Posts

Posted - 2014-05-07 : 09:35:53
this is my query
select * from table8 where dept='Mechanical' and date between '2014-04-01' and '2014-04-26' and status='Completed' or status='Pending'

it returns other dept values

Naveen Rajan

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-05-07 : 09:42:05
quote:
Originally posted by naveen22

this is my query
SELECT * FROM Customers
WHERE Country='Germany'
or Country='Mexico';

i want to fetch the data where the country be either germany or mexico. but it returns all the values

Naveen Rajan

The way you have written it, it will return only if the Country is Germany or Mexico. Do you have additional conditions in the where clause?
Go to Top of Page

naveen22
Starting Member

2 Posts

Posted - 2014-05-07 : 09:44:28
no other conditions.

please see this link http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_where_and

Naveen Rajan
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-05-07 : 10:20:23
Not sure what you are asking. The query that you have shown the link to returns one row because there is only one row where the country and city matches.
SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';
Run this query and it returns 16 rows where country is Germany or Mexico
SELECT * FROM Customers
WHERE Country='Germany'
OR Country = 'Mexico'
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2014-05-08 : 01:30:51
try to use ()?
quote:
Originally posted by naveen22

this is my query
select * from table8 where dept='Mechanical' and date between '2014-04-01' and '2014-04-26' and (status='Completed' or status='Pending')

it returns other dept values

Naveen Rajan

Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-05-08 : 08:36:13
To expand on what waterduck is suggesting, I think what you need is this:
select * from table8 where dept='Mechanical' and date between '2014-04-01' and '2014-04-26' and (status='Completed' or status='Pending')
This is because of operator precedence - http://technet.microsoft.com/en-us/library/ms190276.aspx i.e., AND has higher precedence than OR.

This is the reason I was asking in my original reply whether you had other conditions the where clause. If you had posted the actual query to begin with, you could have resolved the issue much sooner.
Go to Top of Page
   

- Advertisement -