If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Simple query question? plz help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-02-05, 05:32
martijnblokzijl martijnblokzijl is offline
Registered User
 
Join Date: Nov 2005
Posts: 3
Simple query question? plz help

Hi whats up?
sql, so here's my question.

i've got 2 tables (simplified)

company
--------
id (primary key)
name


employee
---------
id (primary key)
company_id (foreign key)
name

the result i want is to get all the company names listed with their employee's. the result must also contain company's where no employee has been filled in.

i used this query:

select company.name, employee.name
from company, employee
where employee.company_id = company.id
order by employee.name asc

this statement works out fine, except the company's with no employee's are left out...

can somebody please help me with this one?

tnx in advance!

ps (sorry for my bad english, i'm dutch)
Reply With Quote
  #2 (permalink)  
Old 11-02-05, 05:40
madafaka madafaka is offline
Registered User
 
Join Date: Feb 2004
Location: Dublin, Ireland
Posts: 212
you have to use outer join
Code:
select company.name, employee.name
from company
left outer join employee
on employee.company_id = company.id
order by employee.name asc
Reply With Quote
  #3 (permalink)  
Old 11-02-05, 05:47
martijnblokzijl martijnblokzijl is offline
Registered User
 
Join Date: Nov 2005
Posts: 3
Excellent it works!

I guess I need to learn something about joins.

Thank you for your effort.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On