Search code examples
mysqlsqljoinexcept

how can i make this select statement


i have two sql statements

the first one

SELECT 
    course.CourseNum, 
    course.CourseName, 
    course.CreditHours
FROM course
WHERE course.TypeID=1

result is

Course Num------Course Name---------Credit Hours                                       
1101----------Arabic Language----------3             
1103----------English LanguageI--------3               
1104----------English LanguageII-------3               
1106----------Hebrew LanguageI---------3             
1125----------Scientific Research------3             

THE second one is just show the results that have grade

SELECT 
    course.CourseNum, 
    course.CourseName,
    course.CreditHours,
    studentcoursecomplete.CourseGrade
FROM course, studentcoursecomplete
WHERE studentcoursecomplete.SID=1 
  And studentcoursecomplete.CourseID = course.CourseID 
  AND course.TypeID=1

result is

Course Num---------Course Name----------Credit Hours-----Grade               
1101---------------Arabic Language-----------3------------60    
1103---------------English LanguageI---------3------------65        
1104---------------English LanguageII--------3------------70

so now i need an sql statement that show the first record EXCEPT the second record !! the sql statement must show this result ..

Course Num------Course Name---------Credit Hours                   
1106----------Hebrew LanguageI---------3                                       
1125----------Scientific Research------3                       

Solution

  • You can do a left join and look for NULL. Also use table alias when possible to make the code more readable.

    select 
        c.CourseNum, 
        c.CourseName, 
        c.CreditHours
    from course c
    left join studentcoursecomplete cc
      on cc.CourseID = c.CourseID
      and cc.SID = 1
    where c.TypeID=1
      and cc.CourseID is null