Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2005
    Posts
    3

    Unanswered: 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)

  2. #2
    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

  3. #3
    Join Date
    Nov 2005
    Posts
    3
    Excellent it works!

    I guess I need to learn something about joins.

    Thank you for your effort.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •