I'm working on an employee_data dataset which has two tables; employees and departments. I'm trying to join data from the employee table to the departments table using department_id as the primary key. It keeps giving me an error message. Please where am I missing it?
SELECT name AS departments_name,
(
SELECT name AS employees_name,
FROM `my-first-analysis.employee_data.employees`
)
FROM `my-first-analysis.employee_data.departments`
JOIN departments ON departments.department_id = employees.department_id
Like this:
SELECT departments.name AS departments_name,
employees.name AS employees_name,
From employees
Left JOIN departments ON departments.department_id = employees.department_id
Related
I'm having a challenge with INNER JOIN in BigQuery.
When trying to run this, I get following error message:
Unrecognized name: employees at [8:2]’ with line 8 being
employees.department_id =departments.department_id
SELECT
name, role, department_id
FROM
`dataanalysis001.employee_data.employees`
INNER JOIN
`dataanalysis001.employee_data.departments`
ON
employees.department_id =departments.department_id
Any suggestions would be welcome. Thanks
You need to either use the fully qualified name for a table every time you use it or, preferably, alias it and use the alias.
so either:
SELECT
name, role, department_id
FROM
`dataanalysis001.employee_data.employees`
INNER JOIN
`dataanalysis001.employee_data.departments`
ON dataanalysis001.employee_data.employees.department_id
= dataanalysis001.employee_data.departments.department_id
or
SELECT
name, role, department_id
FROM
`dataanalysis001.employee_data.employees` emp
INNER JOIN
`dataanalysis001.employee_data.departments` dep
ON emp.department_id = dep.department_id
By on you usually compare a foreign key with a primary key
ON
employees.department_id =departments.id
But if it's the database structure then is it OK.
She is an example of an inner join
SELECT Authors.AuthorID, Books.name, Books.date
FROM Authors
INNER JOIN Books ON Authors.BookId=Books.id;
SELECT
d.name, e.role, e.department_id
FROM
`dataanalysis001.employee_data.employees` as e
INNER JOIN
`dataanalysis001.employee_data.departments` as d
ON
e.department_id = d.department_id;
Im trying to understand inner join correct use but im having trouble when I have to call more than 2 tables, im using oracle HR schema for tests, so for example im trying this query:
select emps.employee_id,
emps.first_name,
dep.department_name,
dep.department_id,
jh.job_id
from employees emps
inner join departments dep
on emps.department_id = dep.department_id
inner join job_history jh
on dep.department_id = jh.department_id;
which shows the following result:
I know its wrong because first its showing duplicate rows, and second if I run this query
select * from job_history where employee_id = 100;
It shows no results which means that employee 100 (steven) shouldnt be appearing in the results of the first query, I expect first query to show me the results of data that is on employees and also on department and also on job_history which have in common the department_id
HR (human resources schema):
Can anyone help me, what im missing, why it shows duplicate rows?
The job_history table has 3 foreign keys:
employee_id
job_id
department_id
Your INNER JOIN only joins on the DEPARTMENT_ID so you are matching an employee to the job history for any jobs that occurred for their department but not necessarily jobs specific to that employee. Instead, you probably want to join on either the employee_id or the combination of employee_id and department_id.
So, if you want the jobs history for that employee for any department:
select emps.employee_id,
emps.first_name,
dep.department_name,
dep.department_id,
jh.job_id
from employees emps
inner join departments dep
on ( emps.department_id = dep.department_id )
inner join job_history jh
on ( emps.employee_id = jh.employee_id );
Or, if you want the job history for that employee within that department:
select emps.employee_id,
emps.first_name,
dep.department_name,
dep.department_id,
jh.job_id
from employees emps
inner join departments dep
on ( emps.department_id = dep.department_id )
inner join job_history jh
on ( emps.employee_id = jh.employee_id
and emps.department_id = jh.department_id );
I am trying to use a sub query on an inner join to get back all department numbers and names from a table that do not have programmer in the department, but I am having a little trouble as it returns no values. Here is my code, thanks for any help.
select Departments.Department_Name, Departments.Department_No
from employees inner join departments
on departments.department_No = employees.Department_No
where Employees.Department_No !=
(select Department_Name
from Employees, Departments
where Job_ID = '%pro%')
From what I can gather, you want something like this:
select d.Department_Name, d.Department_No
from departments d
WHERE NOT EXISTS (SELECT 1 FROM Employees
WHERE d.Department_No = Department_No
AND Job_ID LIKE '%pro%')
This selects all departments, for which there doesn't exist an employee whose job_ib contains 'pro'.
Try this one:
select d1.Department_Name,d1.Department_No
from departments d1
where d1.Department_No in ( select e1.Department_No
from Employees e1
where e1.Department_No=d1.Department_No and
e1.job_id not in ('programmer'));
How do I add a subquery as a column in my SQL script?
e.g.
Select emp_no, name,gender ,
(select department_name from departments where employees.emp_no = departments.emp_no)
from employees
PS: I'm using oracle 8
Going by the semantics, what I understand is that you want an employee's department name to be shown alongside his/her other information. I would suggest you do a join instead:
Select emp_no, name, gender, department_name
from employees emp, departments dept
where emp.emp_no = dept.emp_no;
That looks reasonably sound, I would suggest some (possible typos) cleaning up: add a comma after "gender" and declare the table names, also set the subquery alias
Select employees.emp_no, employees.name, employees.gender,
(select departments.department_name from departments where employees.emp_no = departments.emp_no) as dept_name
from employees
Alternatively, a nice join would would work too, if the other data is feasible:
Select employees.emp_no, employees.name, employees.gender, departments.department_name
from employees
inner join departments on employees.emp_no = departments.emp_no
left join is the best-practice, and should be faster in performance:
Select e.emp_no, e.name, e.gender , d.department_name
from employees e left join departments d on e.emp_no = d.emp_no;
You seem to be missing comma after gender.
Select emp_no, name,gender ,
(select department_name from departments where employees.emp_no = departments.emp_no) as dept_name from employees
The below is what you need. Just added a comma after gender. This subquery would need to return only one row for each result as well or else an error will be seen.
Select emp_no, name,gender,
(select department_name from departments where employees.emp_no = departments.emp_no)
from employees
This query is your answer but it will work only if there is one column mentioned in that if we use more than one column than it will retrun an error .
"Select employee_id,first_name,
(select department_name,manager_id from departments where employees.department_id = departments.department_id) as new_column
from employees;"
can you try this:
SELECT em.emp_no, em."name",em.gender ,
(SELECTdistinctdp.department_name
FROM departments dp
WHERE em.emp_no = dp.emp_no) my_sub
FROM employees em
if there :
(department) table: (id,name)
(employee) table : (id,dept_id,name)
how to show every department (id,name), then all employees (id,name) in this department under its department.
I'd like it as SQL statment
You need to use JOIN
I believe it's something like this:
SELECT department.id, department.name, employee.id, employee.name
FROM department
LEFT JOIN employee
ON department.id=employee.dept_id
ORDER BY department.id
Since all employees must be present under a particular department at any time, you can do a inner join on both the table with dept_id like
SELECT dept.id, dept.name, emp.id, emp.name
FROM department dept
JOIN employee emp
ON dept.id=emp.dept_id
Simply try this
SELECT D.ID,D.Name,E.ID,E.Name
FROM Department D Left JOIN Employee E ON E.dept_id = D.Id