I want to pick values from the same column using an and
clause query, that looks something like this:
select *
from degree_detail_result
where course_id=1 and course_id=2 and course_id=3
What is wrong with the above query?
I want to pick values from the same column using an and
clause query, that looks something like this:
select *
from degree_detail_result
where course_id=1 and course_id=2 and course_id=3
What is wrong with the above query?
Use in
:
select * from degree_detail_result where course_id in (1,2,3)
Note that and
will not work as it requires the conditions on either side of it to be true at the same time, which obviously is not possible.
If you want to get the results next to each other instead of in separate rows, you can use pivot
:
select * from degree_detail_result
pivot (min(degree_id) for course_id in ([1], [2], [3])) as degree
If you want to get a parent ID (not mentioned in your question), for which there are records for course_id 1, 2 and 3, you can do this:
select degree_id from degree_detail_result
where course_id in (1,2,3)
group by degree_id
having count(distinct course_id) = 3
select *
from degree_detail_result
where course_id=1 and course_id=2 and course_id=3
This query will not work, because this condition cannot be met at the same time.
You can try using the INTERSECT
, but you need to specify the particular columns you are looking for instead of SELECT *
i.e. the following will return no rows
SELECT *
FROM degree_detail_result
WHERE course_id = 1
INTERSECT
SELECT *
FROM degree_detail_result
WHERE course_id = 2
INTERSECT
SELECT *
FROM degree_detail_result
WHERE course_id = 3;
and you would need
SELECT degree_id
FROM degree_detail_result
WHERE course_id = 1
INTERSECT
SELECT degree_id
FROM degree_detail_result
WHERE course_id = 2
INTERSECT
SELECT degree_id
FROM degree_detail_result
WHERE course_id = 3;