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. SELECT statements in INNER JOINs?

SELECT statements in INNER JOINs?

Scheduled Pinned Locked Moved Database
databasexmltutorialquestion
3 Posts 3 Posters 1 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
    MS le Roux
    wrote on last edited by
    #1

    I've inherited a somewhat old system with a large number of SQL queries. In a number of these queries, there are SELECTs within INNER JOINs. For example, here are two INNER JOINs that are part of the same query: INNER JOIN ( SELECT * FROM Schema_types ) Schema_types ON Schema_types.Schema_typeID = Schemas.Schema_typeID INNER JOIN ( SELECT * FROM Marketeers WHERE MarketeerID = 7 ) Marketeers ON Marketeers.MarketeerID = Shipments.MarketeerID This looks rather peculiar to me. Is there merit in doing it this way, or is this just wrong?

    M C 2 Replies Last reply
    0
    • M MS le Roux

      I've inherited a somewhat old system with a large number of SQL queries. In a number of these queries, there are SELECTs within INNER JOINs. For example, here are two INNER JOINs that are part of the same query: INNER JOIN ( SELECT * FROM Schema_types ) Schema_types ON Schema_types.Schema_typeID = Schemas.Schema_typeID INNER JOIN ( SELECT * FROM Marketeers WHERE MarketeerID = 7 ) Marketeers ON Marketeers.MarketeerID = Shipments.MarketeerID This looks rather peculiar to me. Is there merit in doing it this way, or is this just wrong?

      M Offline
      M Offline
      Michael Potter
      wrote on last edited by
      #2

      This does look peculiar. It may have been the result of never cleaning up after intial development. I don't know any benefit of writing them this way. There may not be any benefit in cleaning them up - except for readablity.

      INNER JOIN
           Schema_Types 
           ON (Schema_types.Schema_typeID = Schemas.Schema_typeID)
      INNER JOIN
           Marketeers 
           ON ((Marketeers.MarketeerID = Shipments.MarketeerID) AND 
               (MarketeerID = 7))
      
      1 Reply Last reply
      0
      • M MS le Roux

        I've inherited a somewhat old system with a large number of SQL queries. In a number of these queries, there are SELECTs within INNER JOINs. For example, here are two INNER JOINs that are part of the same query: INNER JOIN ( SELECT * FROM Schema_types ) Schema_types ON Schema_types.Schema_typeID = Schemas.Schema_typeID INNER JOIN ( SELECT * FROM Marketeers WHERE MarketeerID = 7 ) Marketeers ON Marketeers.MarketeerID = Shipments.MarketeerID This looks rather peculiar to me. Is there merit in doing it this way, or is this just wrong?

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        Using subqueries (the SELECT statements you speak of) in INNER JOINS can be very useful. However, here it looks stupid because it is just doing a SELECT * FROM... and I guess this would be sub-optimal because it has to create an internal set of data for the subquery that contains every field and they may not all be used once the data gets into the main query. It would be better to do:

        INNER JOIN Schema_types ON Schema_types.Schema_typeID = Schemas.Schema_typeID
        INNER JOIN Marketeers ON Marketeers.MarketeerID = Shipments.MarketeerID

        If my understanding of the query optimiser is correct then this should also speed up your query.


        "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

        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