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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. help me understanding left join

help me understanding left join

Scheduled Pinned Locked Moved Database
databasehelpquestion
4 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
    Mogamboo_Khush_Hua
    wrote on last edited by
    #1

    I want to select all values from @table1 that doesn't exist in @table2 , for that see the below scenario and the query, although i get the correct result but i doesn't understand how query is evaluated , I know the concept of left join but how where is evaluated , i can't understand ? Is query is evaluating like this 1 from @table1 & 1 from @table2 then , 1=1 and t2.id is null = 1=1 and false= no rows. now , suppose 2 from @table1 and 1 from @table2 then , 2=1 and t2.id is null = 2=1 and false = no rows , according to me but here it is giving 2, IS Where condition applies only to those rows that are equal in both table. Please help me ? DECLARE @Table1 TABLE ( ID INT ) INSERT @Table1 SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 DECLARE @Table2 TABLE ( ID INT ) INSERT @Table2 SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 7 UNION ALL SELECT NULL SELECT t1.ID FROM @Table1 AS t1 LEFT JOIN @Table2 AS t2 ON t1.ID = t2.ID WHERE t2.ID is null

    R M 2 Replies Last reply
    0
    • M Mogamboo_Khush_Hua

      I want to select all values from @table1 that doesn't exist in @table2 , for that see the below scenario and the query, although i get the correct result but i doesn't understand how query is evaluated , I know the concept of left join but how where is evaluated , i can't understand ? Is query is evaluating like this 1 from @table1 & 1 from @table2 then , 1=1 and t2.id is null = 1=1 and false= no rows. now , suppose 2 from @table1 and 1 from @table2 then , 2=1 and t2.id is null = 2=1 and false = no rows , according to me but here it is giving 2, IS Where condition applies only to those rows that are equal in both table. Please help me ? DECLARE @Table1 TABLE ( ID INT ) INSERT @Table1 SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 DECLARE @Table2 TABLE ( ID INT ) INSERT @Table2 SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 7 UNION ALL SELECT NULL SELECT t1.ID FROM @Table1 AS t1 LEFT JOIN @Table2 AS t2 ON t1.ID = t2.ID WHERE t2.ID is null

      R Offline
      R Offline
      Rod Kemp
      wrote on last edited by
      #2

      From SQL Server books online, you may want to spend some time looking through it; Inner joins (the typical join operation, which uses some comparison operator like = or <>). These include equi-joins and natural joins. Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. For example, retrieving all rows where the student identification number is the same in both the students and courses tables. Outer joins are specified with one of the following sets of keywords when they are specified in the FROM clause: LEFT JOIN or LEFT OUTER JOIN The result set of a left outer join includes all the rows from the left table specified in the LEFT OUTER clause, not just the ones in which the joined columns match. When a row in the left table has no matching rows in the right table, the associated result set row contains null values for all select list columns coming from the right table. RIGHT JOIN or RIGHT OUTER JOIN A right outer join is the reverse of a left outer join. All rows from the right table are returned. Null values are returned for the left table any time a right table row has no matching row in the left table. FULL JOIN or FULL OUTER JOIN A full outer join returns all rows in both the left and right tables. Any time a row has no match in the other table, the select list columns from the other table contain null values. When there is a match between the tables, the entire result set row contains data values from the base tables.

      1 Reply Last reply
      0
      • M Mogamboo_Khush_Hua

        I want to select all values from @table1 that doesn't exist in @table2 , for that see the below scenario and the query, although i get the correct result but i doesn't understand how query is evaluated , I know the concept of left join but how where is evaluated , i can't understand ? Is query is evaluating like this 1 from @table1 & 1 from @table2 then , 1=1 and t2.id is null = 1=1 and false= no rows. now , suppose 2 from @table1 and 1 from @table2 then , 2=1 and t2.id is null = 2=1 and false = no rows , according to me but here it is giving 2, IS Where condition applies only to those rows that are equal in both table. Please help me ? DECLARE @Table1 TABLE ( ID INT ) INSERT @Table1 SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 DECLARE @Table2 TABLE ( ID INT ) INSERT @Table2 SELECT 1 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 7 UNION ALL SELECT NULL SELECT t1.ID FROM @Table1 AS t1 LEFT JOIN @Table2 AS t2 ON t1.ID = t2.ID WHERE t2.ID is null

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        This article [^]may help as well

        Never underestimate the power of human stupidity RAH

        M 1 Reply Last reply
        0
        • M Mycroft Holmes

          This article [^]may help as well

          Never underestimate the power of human stupidity RAH

          M Offline
          M Offline
          Mogamboo_Khush_Hua
          wrote on last edited by
          #4

          I know that but what i am asking how that cartesian product is done when " where condition is used in left join"

          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