-2
Table name: test
id - student - class
1 - Jane - A
2 - David - B
3 - Jane - C
4 - Nick - A
5 - Nick - B
6 - Smith - C
----------------

Now I want to find out Student name that enrolled both A and C class (must be A and C) the result will show Jane because she enrolled in both A and C

p/s: Im using MS - ACCESS 2007

juergen d
  • 201,996
  • 37
  • 293
  • 362

4 Answers4

2

This is a case of relational division. We have assembled an arsenal of techniques here:

Should be among the fastest solutions:

SELECT student
FROM   test t1
JOIN   test t2 USING (student)
WHERE  t1.class = 'A'
AND    t2.class = 'C';

MS Access has a rather basic implementation of SQL. And it uses double quotes for strings (which contradicts the standard):

SELECT test.student
FROM   test
INNER  JOIN test AS t2 ON test.student = t2.student
WHERE  test.class = "A"
AND    t2.class = "C";

DISTINCT (or grouping of the result) is only needed if the combination (student, class) is not unique - which it should be in most use cases.

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
0

Group by the students and select only classes Aand C. then take only those students having both entries

select student
from test
group by student
having sum(iif(class = 'A',1,0)) > 0
and sum(iif(class = 'B',1,0)) > 0
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • Thanks but it show Syntax Error (missing operator) in query expression 'count (distinct class) = 2' – Minh Trương Jan 15 '15 at 08:15
  • Hmmm. What DB engine do you use? – juergen d Jan 15 '15 at 08:17
  • Im using MS ACCESS 2007 – Minh Trương Jan 15 '15 at 08:25
  • Oh. Access does nort support `distinct` AFAIK. – juergen d Jan 15 '15 at 08:28
  • Perfect dude... it worked... Can u explain me details abt these one. I really at basic SQL only. appriciated that ! – Minh Trương Jan 15 '15 at 08:37
  • You group by the students. Then you can use the `having` clause to look at all records for each student. The students you want to select need to have classes A and B. Since you look at multiple records of a student in a group you need to use aggregate functions like ´sum()`. In that sum is a case that sums up the number of correct classes. Do it doe A and B – juergen d Jan 15 '15 at 08:41
  • `if(class='A') then 1 else 0` That condition return 1 for true and 0 for false. The `sum()` sums up those true condtions. At least once (`sum>0`) this must be true for a student for each class – juergen d Jan 15 '15 at 09:03
  • appriciated dude ! Thanks a lots :D – Minh Trương Jan 15 '15 at 09:17
0

Take name's of A Class students & C Class Students & do Intersection on these result set's

select distinct
 a.student
from
 test a
 inner join test b on a.id = b.id
 where a.class = 'A' and b.class= 'C'
Amol Bavannavar
  • 2,062
  • 2
  • 15
  • 36
0

This is one way:

SELECT DISTINCT STUDENT 
FROM TEST T 
WHERE 
EXISTS (SELECT NULL FROM TEST T1 WHERE T.STUDENT = T1.STUDENT AND T1.CLASS='A') AND
EXISTS (SELECT NULL FROM TEST T2 WHERE T.STUDENT = T2.STUDENT AND T2.CLASS='C');
Lord Peter
  • 3,433
  • 2
  • 33
  • 33