Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Ho to avoid Not In & In clause in my query

Ho to avoid Not In & In clause in my query

Scheduled Pinned Locked Moved Database
database
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    member0026
    wrote on last edited by
    #1

    select id,name from stud where (id not in (select id from class)) and depid in (select depid from dep where rid=@rid)

    B T G 3 Replies Last reply
    0
    • M member0026

      select id,name from stud where (id not in (select id from class)) and depid in (select depid from dep where rid=@rid)

      B Offline
      B Offline
      Bernhard Hiller
      wrote on last edited by
      #2

      What about:

      Select DISTINCT id, name
      FROM stud
      INNER JOIN dep on stud.depid=dep.depid
      LEFT JOIN class on stud.id=class.id
      WHERE dep.rid=@rid
      AND stud.id is null

      But I doubt that it's faster...

      M 1 Reply Last reply
      0
      • M member0026

        select id,name from stud where (id not in (select id from class)) and depid in (select depid from dep where rid=@rid)

        T Offline
        T Offline
        thatraja
        wrote on last edited by
        #3

        JOIN/EXISTS SQL SERVER – Subquery or Join – Various Options – SQL Server Engine knows the Best[^]

        thatraja

        Code converters | Education Needed No thanks, I am all stocked up. - Luc Pattyn When you're wrestling a gorilla, you don't stop when you're tired, you stop when the gorilla is - Henry Minute

        1 Reply Last reply
        0
        • M member0026

          select id,name from stud where (id not in (select id from class)) and depid in (select depid from dep where rid=@rid)

          G Offline
          G Offline
          GuyThiebaut
          wrote on last edited by
          #4

          select id,name
          from stud s
          where not exists
          (
          select c.id
          from class c
          where c.id = s.id
          )
          and depid in
          (
          select depid
          from dep
          where rid=@rid
          )

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

          1 Reply Last reply
          0
          • B Bernhard Hiller

            What about:

            Select DISTINCT id, name
            FROM stud
            INNER JOIN dep on stud.depid=dep.depid
            LEFT JOIN class on stud.id=class.id
            WHERE dep.rid=@rid
            AND stud.id is null

            But I doubt that it's faster...

            M Offline
            M Offline
            member0026
            wrote on last edited by
            #5

            Im not sure, but compare not in the join will be the faster one, i heared like that :(

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups