Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
write sql query for that.

find the list of student id who enrolled in one course english or Sindhi but not enrolled in both courses.

table name : course
having two columns
courseid|cname

table name : studentcourse
having two columns
studentid|courseid
Posted

We don't do homework. If you get stuck to something, people are ready to help in specific questions. However, you have to show that you have tried yourself and clearly point out what is the problem. Doing and trying yourself is the only way to learn and I bet that if you try, you'll succeed.
 
Share this answer
 
Try this Query..
i hope this will help you..
C#
select [StdId],count(distinct([CourseId]))  from [dbo].[tbl_Student_Course] group by [StdId]
 having count(distinct([CourseId]))=1
 
Share this answer
 
v2
Yet another solution without Count, Group By and Distinct:

Pretty verbose and I guess slower than the other ones because of its nestings:

C#
SELECT studentId
FROM studentcourse S JOIN course C ON S.courseid=C.courseid
WHERE C.cname IN ('English')
AND
studentId NOT IN
(SELECT studentId FROM studentcourse S JOIN course C ON S.courseid=C.courseid
WHERE C.cname IN ('Sindhi'))

UNION

SELECT studentId
FROM studentcourse S JOIN course C ON S.courseid=C.courseid
WHERE C.cname IN ('Sindhi')
AND
studentId NOT IN
(SELECT studentId FROM studentcourse S JOIN course C ON S.courseid=C.courseid
WHERE C.cname IN ('English'))
 
Share this answer
 
SQL
SELECT
	stdId
FROM
	studentcourse S
	JOIN course C ON S.courseid =C.courseid
WHERE
	C.cname IN ('english', 'Sindhi')
GROUP BY
	[sId]
HAVING
	COUNT(DISTINCT (C.courseid)) < 2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900