Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Left join returns fewer rows than expected?

Following is my query:

  Select Count(*)
         from Table1 s
               left join Table2 d
                    ON  s.subjectid = d.subjectid
                    and s.PROJECTID = d.projectid
                    and s.SITEName = d.SITENAME
               left join Table3 dev
                    on s.subjectid = dev.subjectid
                    and s.projectid = dev.projectid
                    and s.siteid = dev.siteid
         Where s.isprod =1
         and   d.isprod =1
         and  dev.isprod = 1
         and s.projectid =107
      -- Output 301 ROWS

This query returns 301 rows. However, if I don't use Table3 then the join returns 2203 rows, as shown in the query below:

         Select Count(*)
         from Table1 s
               left join Table2 d
                    ON  s.subjectid = d.subjectid
                    and s.PROJECTID = d.projectid
                    and s.SITEName = d.SITENAME
         Where s.isprod =1
         and   d.isprod =1
         and s.projectid =107
       -- OutPut 2203 ROWS

By my understanding of left join, all the rows from the left table should remain even if they don't match with the right table. However in this case, the number of rows is reduced from 2203 in query 2 to 301 in query 1. How is that possible?

Please suggest what could be going wrong here. For more clarification:

  • Table1 with the same where conditions as above has 1300 rows

  • Table2 with the same where conditions as above has 2203 rows

  • Table3 with the same where conditions as above has 129 rows

like image 615
Amit Singh Parihar Avatar asked May 23 '17 19:05

Amit Singh Parihar


People also ask

How many rows will return for left join?

The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.

How can I improve my left join performance?

First of all, indexes are required to speed up the query. If you do not have any, you probably should create some (depending on the query you perform). And if you do multiple LEFT JOINs, then you could (probably) separate them into different queries and this should make the application work a lot faster.

Why LEFT join get more rows?

There are two line items for ID 1003 in the second table, so the result of the join will be 2 line items. So, if your secondary tables have more than one row for the key you're joining with, then the result of the join will be multiple rows, resulting in more rows than the left table.

Does LEFT join return duplicate rows?

Again, if we perform a left outer join where date = date, each row from Table 5 will join on to every matching row from Table 4. However, in this case, the join will result in 4 rows of duplicate dates in the joined DataSet (see Table 6).


2 Answers

When you have conditions in your where clause that put non-null constraints on the records from the table you have outer joined, you effectively destroy the effect of the outer join, and make it act as an inner join

The solution is to move such constraints into the on clause of the outer join:

Select Count(*)
     from Table1 s
           left join Table2 d
                ON  s.subjectid = d.subjectid
                and s.PROJECTID = d.projectid
                and s.SITEName = d.SITENAME
                and d.isprod =1
           left join Table3 dev
                on s.subjectid = dev.subjectid
                and s.projectid = dev.projectid
                and s.siteid = dev.siteid
                and dev.isprod = 1
     Where s.isprod =1
     and s.projectid =107
like image 63
trincot Avatar answered Oct 21 '22 08:10

trincot


The first SQL has additional "where" clause of "and dev.isprod = 1". Most likely this is reducing the number of rows returned.

like image 21
tsolakp Avatar answered Oct 21 '22 07:10

tsolakp