Search code examples
sqlsql-servert-sqlsubquery

Filter unrelated rows


I have 2 tables

TABLE 1 (named)  coursescores
studentCode,CourseCode,Garde,MajorCode,Score

TABLE 2 (named) MajorCourses
MajorCode,CourseCode,...

I want to fetch CourseCodes that are not related to Majorcode in TABLE 1 based on refrence table that is

MajorCourses

i want to get those coursecodes that are unrelated to majorcode in table 1

can anybody help me write the query thanks in advance


Solution

  • I think you want not exists. That would look like:

    select cs.*
    from coursescores cs
    where not exists (
        select 1 
        from majorcourses mc 
        where mc.coursecode = cs.coursecode and mc.majorcode = cs.majorcode
    )