An introduction to SQL queries but better you look for an introductory SQL text
I assume that every employee is in the job_history table and every employee and therefore job is assigned to a department. Otherwise one will loose some employees.
Let's display the columns of the tables involved:
select employee_id,first_name,last_name,department_id
from employees;
select department_id, department_name
from departments;
select job_id,employeed_id,department_id
from job_history;
select job_id,job_title
from jobs;
First a list of all employees and their departement information
select *
from employee e, departments d
where e.department_id=d.department_id;
now select only the rows from the employees that are in the department 'Sales'
select *
from employee e, departments d
where e.department_id=d.department_id
and d.department_name='Sales';
for these employees now find the jobs have already head fron the job_history
select *
from employee e, departments d, jobhistory h
where e.department_id=d.department_id
and d.department_name='Sales'
and e.employee_id=h.employee_id;
Now for each job find the job title
select *
from employee e, departments d, jobhistory h, jobs j
where e.department_id=d.department_id
and d.department_name='Sales'
and e.employee_id=h.employee_id
and h.job_id=jobs.job_id;
Now we display only the columns we need
select e.first_name,e.last_name,j.job_title
from employee e, departments d, jobhistory h, jobs j
where e.department_id=d.department_id
and d.department_name='Sales'
and e.employee_id=h.employee_id
and h.job_id=jobs.job_id;
If we want to know in which department they worked in their job wee join another department table to get these information
select e.first_name,e.last_name,j.job_title,d2.department_name
from employee e, departments d, jobhistory h, jobs j,departments d2
where e.department_id=d.department_id
and d.department_name='Sales'
and e.employee_id=h.employee_id
and h.job_id=jobs.job_id
and h.depatment_id=d2.department_id;
Best Answer
My best guess is that what you are actually trying to query is:
I changed the subselect to return
C.sid
, and removed theparts
table altogether, since in your original body text you stated no conditions on the table.Is this what you are looking for?