engr007
engr007

Reputation: 51

How to include rows with missing data in SQL Server 2008

Let's say I have a table a table of employees and another employee_address.

Additionally, I have a table of employee_email_address (employees don't have to have an email)

select 
    employee.emp_id, 
    employee_address.address, 
    employee_email_address.email
from  
    employees 
inner join
    employee_address on employee.emp_id = employee_address.emp_id
inner join 
    employee_email_address on employee_email_address.emp_id = employee.emp_id

but what this query is giving me is only the employees who have an address AND an email address....

How can I include rows for which there is no email_address for the employees? (Note, the email address is not null)

Thanks.

Upvotes: 0

Views: 45

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269503

Try this query:

Select e.emp_id, ea.address, eea.email
from employees e left join
     employee_address ea
     on ea.emp_id = e.emp_id left join
     employee_email_address eea 
     on eea.emp_id = e.emp_id;

Upvotes: 1

Use left join instead of inner join.

Upvotes: 1

Related Questions