sql & inner join - sql

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;

Related

How to use the JOIN statement in SQL

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

Where clause and its order during join Operation

Was asked in an interview a traditional question of self join with manager and employee.
I was confused as interviewer asked me to display the columns as EmployeeName, Manager Name.
Please advice how does the conditions in where clause effects the selected columns. As I wasn't able to get the required order.
The trick here is that every manager is also an employee, so you can join the table on itself, and use one side of the join for the employee details and the other for the manager's details:
SELECT e.eployee_name AS "employee name", m.employee_name AS "manager name"
FROM employee e
JOIN employee m ON e.manager_id = m.employee_id
You can use the connect by query as following:
select employee_name, prior employee_name
from employees
connect by prior employee_id = manager_id
start with manager_id is null
If you want to use join then you must do outer join as following:
Select e.employee_name, m.employee_name
From employees e left join employees m
On e.employee_id = m.employee_id
Left outer join will make sure that all the employees will exists in the result as the employee who is the top manager will have null manager_id and inner join will exclude that employee from the result.
Cheers!!

SQLite Multiple Subqueries Logic

The problem is asking for one to write a query to find the names (first_name, last_name) of the employees who have a manager who works for a department based in the United States. Here is a link to the problem, to see the tables, https://www.w3resource.com/sqlite-exercises/sqlite-subquery-exercise-3.php.
For the subquery, I did a left join on the location id between the department and location tables then I selected 'US' for the country_id, and returned the manager_id
For the outer query, I chose the Employee table, selected manager_ids from sub-query list.
SELECT first_name, last_name
FROM Employees
WHERE manager_id IN (SELECT manager_id
FROM Departments d LEFT JOIN Locations l ON d.location_id = l.location_id
WHERE country_id = 'US')
ORDER BY first_name;
With my code, I do not get the correct answer, the same results as the result-set/output shown on the website.
There are three subqueries in total in the correct answer. I do not understand what the purpose of including the subquery involving the employees table (outermost subquery). I understand that is where I messed up but don't understand why.
SELECT first_name, last_name
FROM employees
WHERE manager_id IN
(SELECT employee_id
FROM employees
WHERE department_id IN
(SELECT department_id
FROM departments
WHERE location_id IN
(SELECT location_id
FROM locations
WHERE country_id='US')));
You need to join all the tables:
select e.first_name, e.last_name
from employees e
inner join employees m on m.employee_id = e.manager_id
inner join departments d on d.department_id = m.department_id
inner join locations l on l.location_id = d.location_id
where l.country_id='US'

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.
Use left join instead of inner join.
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;

Join 3 tables and Group

I am stuck here working on a homework and this is what I have got.
Instruction is: Display department name, city and number of different jobs in each department.
tables employees (has job_id and department_id), deptos (has location_id, department_id but no job ids), locations (has location_id and city)
I need to include all cities even the ones without employees
What I was trying to do...
select d.department_name, l.city, count (distinct e.job_id)
from employees e
join deptos d on (e.department_id=d.department_id)
join locations l on (d.location_id=l.location_id)
group by d.department_name
locations can have data missing in other tables, so right join or you start from it and use left joins. You also need to group by city. Tried to do minimum changes to OP query.
select d.department_name, l.city, count(distinct e.job_id)
from employees e
join deptos d on (e.department_id=d.department_id)
right join locations l on (d.location_id=l.location_id)
group by d.department_name, l.city
SQL Fiddle to test with
You need to use a OUTER JOIN to accomplish this... like the below. You do not necessarily have to use the keyword outer as it is implied, just remember the the difference between using LEFT and RIGHT joins there is a post on that here. Example below
LEFT JOIN vs. LEFT OUTER JOIN in SQL Server
select d.department_name, l.city, count (distinct e.job_id)
from locations l
left outer join deptos d on (e.department_id=d.department_id)
left outer join employees e on (d.location_id=l.location_id)
group by d.department_name
SELECT locations.city, deptos.department_name,
Count(DISTINCT employees.job_id) AS DiffJobsDeptCount
FROM employees
RIGHT JOIN (deptos
RIGHT JOIN locations
ON deptos.location_id = locations.location_id)
ON employees.department_id = deptos.department_id
GROUP BY locations.city, deptos.department_name