-1

New to SQL but I want to be able to optimize my query by bringing just the right amount of data. I am doing a left join on CS Rep Name and WE, which are two columns present in both tables. I find that if I don't bring in CS Rep Name and WE in the TECDR table, the query would error. Is there a workaround to this? Since it is a left join, I don't need redundant data.

SELECT *
FROM   Tish_Email_CSAT_Dump AS TECD
       LEFT JOIN (SELECT CS_Rep_Name,
                         Team_Leader,
                         Operations_Manager,
                         Tenure,
                         WE,
                         FileName
                  FROM   Tish_Email_CSAT_Dump_Roster) AS TECDR
         ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name
            AND TECD.WE = TECDR.WE 
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
Zeta
  • 105
  • 9
  • 1
    what do you mean with `don't bring in CS Rep Name and WE in the TECDR table`?, what do you actually want as a result? – Lamak Aug 10 '16 at 17:55
  • Don't use `select *`. Select just the columns you need. – Martin Smith Aug 10 '16 at 17:57
  • avoid the subquery, join the two tables directly. Then limit the amount of data replacing select * for the columns you really need – vercelli Aug 10 '16 at 17:58
  • 2
    @vercelli - the derived table doesn't make any actual difference except to clarity possibly. It will be resolved against the base tables anyway. – Martin Smith Aug 10 '16 at 17:59
  • Possible duplicate of [MySQL Select all columns from one table and some from another table](http://stackoverflow.com/questions/3492904/mysql-select-all-columns-from-one-table-and-some-from-another-table) – Tab Alleman Aug 10 '16 at 18:04

3 Answers3

3

When you embed a SELECT inside a query in place of a table, the result of a select (projection) behave like a table visible only inside the query.

In your case, the join is the same as if there were a table called TECDR with the columns that you select. Hence, if you leave out some columns of Tish_Email_CSAT_Dump_Roster from your SELECT, these columns would not be available for joining or selection.

However, in your case this is unnecessary: all you need to do is joining to the underlying table, like this:

SELECT
    TECD.*
,   TECDR.Team_Leader
,   TECDR.Operations_Manager
,   TECDR.Tenure
,   TECDR.FileName
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN Tish_Email_CSAT_Dump_Roster AS TECDR
       ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name AND TECD.WE = TECDR.WE
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2
select 
  <place the columns you want here>
from 
  Tish_Email_CSAT_Dump as TECD
  Left join Tish_Email_CSAT_Dump_Roster as TECDR 
    On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE
mikey
  • 5,090
  • 3
  • 24
  • 27
1

Hope the following helps or else please share the query that errors:

select TECD.Column1, TECD.Column2, TECDR.Column1, TECDR.Column2 from Tish_Email_CSAT_Dump as TECD Left join Tish_Email_CSAT_Dump_Roster as TECDR On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE