1

I am using Microsoft SQL Server 2008 and we have two tables called Applications and Enrolments. I want to know which applications are not converted into the Enrolments. As shown in the following figure.

I tried to use NOT IN but it works only with the single column. I have 2 columns to compare. Could you please advise me what is the most appropriate way? Thanks.

PS. I cannot change the structure of the database and they are from third party vendor.

enter image description here

TTCG
  • 8,165
  • 27
  • 80
  • 135

5 Answers5

4

Another way, using except

select  
        StudentID, 
        CourseID
FROM dbo.Applications
except
select  
        StudentID, 
        CourseID
FROM dbo.Enrolments
Prahalad Gaggar
  • 10,932
  • 16
  • 49
  • 68
3

Use NOT EXISTS instead:

SELECT StudentID, CourseID
FROM dbo.Applications a
WHERE NOT EXISTS(
    SELECT 1 FROM Enrolments e
    WHERE e.StudenID = a.StudenID 
    AND   e.CourseID = a.CourseID
)
Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
2

Try

SELECT a.*
FROM Applications a
LEFT JOIN Enrolments e 
       ON e.StudentId = a.StudentId
      AND e.CourseId = a.CourseId
WHERE e.StudentId IS NULL

Side note: interesting answer comparing similar solutions here

Community
  • 1
  • 1
T I
  • 9,562
  • 3
  • 26
  • 49
1
SELECT a.* FROM Applications a
LEFT JOIN Enrolments e 
   ON a.StudentID = e.StudentID AND a.CourseID = e.CourseID
WHERE e.StudentID IS NULL
mirkobrankovic
  • 2,309
  • 1
  • 19
  • 24
1

Simplest way is probably a left join between applications and enrolments - returning just the ones where the enrolment is null

IE

SELECT a.*
FROM Applications a
LEFT JOIN Enrolments e ON a.StudentID = e.StudentID AND a.CourseID = e.CourseID
WHERE e.StudentID IS NULL
James S
  • 3,498
  • 14
  • 24