SQL Not Exist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    #1

    SQL Not Exist

    I have a table called Elections where All is Yes/No

    Title ... ... All

    I also have a table called HasVoted

    ID Title

    And a table called Students

    Name ID ... ...

    I need to write a query that finds all the Titles in Election where All = yes and checks to make sure that the Title doesn't exist in HasVoted for a given ID ssn.

    this didn't work but I thought it was close:

    SELECT Title t FROM Elections WHERE Elections.All = 'Yes' AND NOT EXISTS (SELECT ID FROM HasVoted WHERE ID = '" + s + "' AND Title = t);
  • Peter Koen

    #2
    Re: SQL Not Exist

    "=?Utf-8?B?RXJpYw==?=" <anonymous@disc ussions.microso ft.com> wrote in
    news:66264576-66B8-416E-9D68-EAE52A215ED7@mi crosoft.com:
    [color=blue]
    > I have a table called Elections where All is Yes/No
    >
    > Title ... ... All
    >
    > I also have a table called HasVoted
    >
    > ID Title
    >
    > And a table called Students
    >
    > Name ID ... ...
    >
    > I need to write a query that finds all the Titles in Election where
    > All = yes and checks to make sure that the Title doesn't exist in
    > HasVoted for a given ID ssn.
    >
    > this didn't work but I thought it was close:
    >
    > SELECT Title t FROM Elections WHERE Elections.All = 'Yes' AND NOT
    > EXISTS (SELECT ID FROM HasVoted WHERE ID = '" + s + "' AND Title = t);
    >[/color]

    SELECT Title FROM Election E WHERE Elections.ALL = 'Yes' AND NOT EXISTS
    (SELECT ID FROM HasVoted V INNER JOIN Students S on V.ID = S.ID WHERE
    V.Title = E.Title)

    --
    best regards

    Peter Koen
    -----------------------------------
    MCAD, CAI/R, CAI/S, CASE/RS, CAT/RS

    Comment

    • Chris Dawe

      #3
      Re: SQL Not Exist

      On Sat, 25 Oct 2003 14:36:00 -0700, Peter Koen
      <koen-newsreply&snusn u.at> wrote:
      [color=blue]
      >"=?Utf-8?B?RXJpYw==?=" <anonymous@disc ussions.microso ft.com> wrote in
      >news:6626457 6-66B8-416E-9D68-EAE52A215ED7@mi crosoft.com:
      >[color=green]
      >> I have a table called Elections where All is Yes/No
      >>
      >> Title ... ... All
      >>
      >> I also have a table called HasVoted
      >>
      >> ID Title
      >>
      >> And a table called Students
      >>
      >> Name ID ... ...
      >>
      >> I need to write a query that finds all the Titles in Election where
      >> All = yes and checks to make sure that the Title doesn't exist in
      >> HasVoted for a given ID ssn.
      >>
      >> this didn't work but I thought it was close:
      >>
      >> SELECT Title t FROM Elections WHERE Elections.All = 'Yes' AND NOT
      >> EXISTS (SELECT ID FROM HasVoted WHERE ID = '" + s + "' AND Title = t);
      >>[/color]
      >
      >SELECT Title FROM Election E WHERE Elections.ALL = 'Yes' AND NOT EXISTS
      >(SELECT ID FROM HasVoted V INNER JOIN Students S on V.ID = S.ID WHERE
      >V.Title = E.Title)[/color]

      Why join to the Students table at all, it's unnecessary.

      SELECT Title FROM Election E WHERE Elections.ALL = 'Yes' AND NOT
      EXISTS (SELECT * FROM HasVoted V WHERE V.Title = E.Title)

      or

      SELECT Title FROM Election E WHERE Elections.ALL = 'Yes' AND Title NOT
      IN (SELECT DISTINCT Title FROM HasVoted)

      Chris
      --

      Comment

      Working...