Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 tables like below
Student 
   	stdId
	stdName
        stdCourseID
StudentCources
        stdCourseID
        courseName
        stdeventID
StudentEvents
        stdeventID
        eventName

i want to fill all these properties like below
stdId
stdName

stdCourseName

stdEventName

when i try with this query i am getting duplicate stdId , stdName and stdCourseID follewd by list of remaning items corseName and eventName
SQL
select * from Student as s WITH(NOLOCK) 
join StudentCources as se WITH(NOLOCK) on sc.stdCourseID = s.stdCourseID 
join StudentEvents as se WITH(NOLOCK) on se.stdeventID = se.stdeventID  
where s.stdId=118


What I have tried:

SQL
select name,courseName,eventName from Student as s WITH(NOLOCK) 
join StudentCources as se WITH(NOLOCK) on sc.stdCourseID = s.stdCourseID 
join StudentEvents as se WITH(NOLOCK) on se.stdeventID = se.stdeventID  
where s.stdId=118

Could you please tell me how to optimize this query that should fetch all data
Posted
Updated 4-Apr-19 11:14am
v2
Comments
Richard Deeming 4-Apr-19 12:18pm    
You've used se as the alias for both StudentCources (should be "courses") and StudentEvents. I assume that's a typo in your question, since SQL won't allow that.

You've also joined the StudentEvents table to itself, rather than to the courses table:
on se.stdeventID = se.stdeventID 
DGKumar 4-Apr-19 15:36pm    
Sorry the below is the actual query. Is there any optimization required for the below query
select name,courseName,eventName from Student as s WITH(NOLOCK)
join StudentCources as sc WITH(NOLOCK) on sc.stdCourseID = s.stdCourseID
join StudentEvents as se WITH(NOLOCK) on se.stdeventID = sc.stdeventID
where s.stdId=118

1 solution

Also, if your student doesn't have a record in either the "StudentCources" [sic] table and "StudentEvents" table, it will return an empty set because you are using an implied INNER JOIN.
 
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