HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Exclude dataset from a query

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
datasetfromqueryexclude

Problem

I have 2 tables

  • EXAM (StudentID, SubjectID, SchoolYear, Period, Mark) and



  • CANCELED_EXAM (StudentID, SubjectID, SchoolYear, Period).



Primary key for both tables are (StudentID, SubjectID, SchoolYear, Period).

When a student wants to take an exam, then we write one row in EXAM table.
But if a student passed an exam and is not satisfied, then he wants to cancel the exam and we need to write one row in table CANCELED_EXAM.

My question is: How to select just non-canceled exams from table EXAM.

Solution

This is standard SQL

SELECT
   *
FROM
   EXAM E
WHERE
   NOT EXISTS (SELECT *
       FROM
          CANCELED_EXAM CE
       WHERE
          E.StudentID = CE.StudentID AND
          E.SubjectID = CE.SubjectID AND
          E.SchoolYear = CE.SchoolYear AND
          E.Period = CE.Period )

Code Snippets

SELECT
   *
FROM
   EXAM E
WHERE
   NOT EXISTS (SELECT *
       FROM
          CANCELED_EXAM CE
       WHERE
          E.StudentID = CE.StudentID AND
          E.SubjectID = CE.SubjectID AND
          E.SchoolYear = CE.SchoolYear AND
          E.Period = CE.Period )

Context

StackExchange Database Administrators Q#3896, answer score: 6

Revisions (0)

No revisions yet.