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
 =ALL or =ANY query problem

Author  Topic 

andretru2
Starting Member

2 Posts

Posted - 2011-02-28 : 15:16:46
I need to run a query for students that took ALL the following courses: 6710, 6711, 1032, 1035 and ANY of the following courses: 3061,3065

Here is my query:
select distinct student_number, last_name, first_name, gender, ethnicity, students.grade_level, course_name, course_number
from students inner join storedgrades on students.id=storedgrades.studentid
where (students.grade_level >'9' and students.grade_level <= '12')
and (course_number = all('6710', '6711', '1032', '1035') and course_number=Any('3061','3065'))
order by last_name, first_name, grade_level, course_number

The problem is that I don't get any results. But when I change the query to OR instead of AND I can see that students took the courses required (6710, 6711, 1032, 1035 and any 3061, 3065). What am I doing wrong?

Thanks in advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-28 : 15:29:06
ALL and ANY? What dbms are you using?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

andretru2
Starting Member

2 Posts

Posted - 2011-02-28 : 15:33:49
Oracle 10g. Am I in the wrong forum?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-28 : 15:35:03
Yep. You've posted your question on a Microsoft SQL Server site. Try the oracle site or dbforums.com's Oracle forum.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2011-02-28 : 15:58:04
ALL and ANY(or SOME) are valid in SQL Server, but the way you are using them is not correct.

Both ALL and ANY require a subquery, not a list of delimited values.
Example from SQL Server Books Online:
IF @NumberOfDays >= ALL
(
SELECT DaysToManufacture
FROM Sales.SalesOrderDetail
JOIN Production.Product
ON Sales.SalesOrderDetail.ProductID = Production.Product.ProductID
WHERE SalesOrderID = @OrderID
)
PRINT 'All items for this order can be manufactured in specified number of days or less.'
ELSE
PRINT 'Some items for this order cannot be manufactured in specified number of days or less.' ;







CODO ERGO SUM
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-02-28 : 16:09:05
Yeah I meant in that context.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -