9

Below is my table called fittest. I need to find which students based on student id (studid) have taken the pre and post test as designated in the prepost column. So based on the simple table below I would need to return studid 123456. How do I write the SELECT query for this?

SQL query: SELECT studid, prepost FROM `fittest` LIMIT 0, 30 ; 

    studid  prepost
    123456  pre
    123456  post
    1031460 pre
1
  • what you have tried so far? show us your query and let us know where you are facing problem. Commented Jul 23, 2013 at 7:26

9 Answers 9

5

Try

SELECT studid
  FROM fittest
 GROUP BY studid
HAVING COUNT(DISTINCT prepost) = 2

or if you want to ensure that only ids that have exactly one row with pre and one row with post then you can enforce it like this

SELECT studid
  FROM fittest
 GROUP BY studid
HAVING (MAX(prepost = 'pre' ) + 
        MAX(prepost = 'post')) = 2
   AND COUNT(DISTINCT prepost) = 2

Output:

| STUDID |
----------
| 123456 |

Here is SQLFiddle demo for both queries

Sign up to request clarification or add additional context in comments.

3 Comments

@Luv : OP have only option as pre and post ONLY.
@Luv I believe OP has only pre and post values in real life. But I've added a query that eliminates possibilities of having rows with values other than pre and post.
Thank you peterm and everyone that contributed!! The suggestions have been much appreciated. I used your first example peterm. The only options for prepost is pre or post.
4

What about a JOIN:

select s1.studid, s1.prepost, s2.prepost
from fittest s1
inner join fittest s2
    on s1.studid = s2.studid
where s1.prepost = 'pre' 
  and s2.prepost = 'post'

Comments

0

Depending on your real Primary Key, you use the following:

SELECT studid
FROM fittest
WHERE prepost = 'pre' OR prepost = 'post'
GROUP BY studid
HAVING COUNT(DISTINCT prepost) = 2

Comments

0
SELECT t1.studid
FROM fittest t1
INNER JOIN t2 ON t1.studid=t2.studid
WHERE t1.prepost = 'pre'
AND t2.prepost='post'

1 Comment

I agree with you. peterm's answer seems more performant. I left my answer for information purpose, but it's definitely not the best one.
0

If Only PRE and POST are possible in this column then try

SELECT studid FROM fittest group by studid
HAVING Min(prepost)<>max(prepost)

Comments

0
SELECT 
studid 
FROM `fittest` 
where prepost = 'pre' 
or prepost = 'post'
group by studid
having count(distinct prepost)=2

SQL Fiddle with Extra records.

3 Comments

I don't think this will have any output... where prepost = 'pre' and prepost = 'post' will fail every time as that is not possible...
@peterm : I agree this is much buggy query I would say oppose to your query... sqlfiddle.com/#!2/0c999/1
Thanks for helping me in learning something which I was not aware of. But i have edited my Answer now.
0

try:

    select f1.studid
       from fittest f1
       inner join fittest f2
         on f1.studid = f2.studid
    where f1.prepost = 'pre' and f2.prepost = 'post'

here's the SQL Fiddle

2 Comments

this will never give any output
@downvoter : wrong query doesn't mean for downvote... PLEASE
0

You could use JOIN

SELECT a.studid 
FROM fittest a 
JOIN fittest b 
    ON a.studid=b.studid 
        and a.prepost like 'pre' 
        and b.prepost like 'post'

Comments

-1

Select .... where studid in (123456,123457,...)

4 Comments

'based on studid' SELECT studid, prepost FROM fittest where studid in (123456,123457,...)
'I need to find which students based on student id (studid)'
What is there that you don't understand?
Read the whole question then: "... have taken the pre and post test".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.