What is the difference between JOIN through ON and Using in MYSQL - mysql

I am trying to write a query to display the last name, department
number, and department name for all the employees.
And this my working code:
SELECT
last_name,
department_id,
department_name
FROM
employees
JOIN departments USING(DEPARTMENT_ID);
When I was trying to make the query using JOIN ON ,I faced an error saying that
Column 'department_id' in field list is ambiguous through this code
query using JOIN ON:
SELECT
last_name,
department_id,
department_name
FROM
employees
JOIN departments ON(
employees.DEPARTMENT_ID = departments.DEPARTMENT_ID
)
I want to know why it is not working .

The ambiguity should be resolved in select clause
SELECT
last_name,
employees.department_id,
department_name
FROM employees
JOIN departments USING(DEPARTMENT_ID);

Related

SQL Query to display a list of employees with the name of the department and the name of the head

to bring to the column the chiefs of their subordinates who are tied to one department. How can this be done? I tried different joins, but I couldn’t get the column of the remaining employees to their bosses. I'm new to SQL sorry, please.
I am getting the result only for the chief to name on the department but I can't add a column with employees.
SELECT
employee.name,
employee.chief_id,
department.name,
department_id
FROM
employee
INNER JOIN
department ON department.id = employee.chief_id
You can use the GROUP_CONCAT() function as follows:
SELECT
head.name as head ,
department.name as department,
GROUP_CONCAT(employee.name) as employess
FROM
employee
INNER JOIN
employee as head ON employee.chief_id = head.id
INNER JOIN
department ON department.id = employee.department_id
group by head.name,department.name

Incorrect records from SQL query

So im trying to list the department locations, the project names associated with each department and the number of employees that work on each project.
There is a DeptLocations table(Attributes: Dnum, DLoc)
A Project table(Attributes:PName, Pnum, PLoc, DNum)
An Employee table (Attributes: FNAME, M, LNAME, SSN, BDATE, ADDRESS, S, SALARY, SUPERSSN, DNO)
And a Works_On table (Attributes: ESSN, PNO, HOURS).
This is my SQL query:
select DeptLocations.DLocation, Project.PName, count(ESSN)
from Works_On, DeptLocations, Project, Department
where DeptLocations.DLocation = Project.PLocation and Project.PNumber = Works_On.PNo
For some reason it only yields 1 record, when clearly there should be plenty more. Any help would be awesome.
You need to add group by clause - as you've used aggregated function:
select
DeptLocations.DLocation,
Project.PName,
count(ESSN)
from
Works_On
inner join Project on Project.PNumber = Works_On.PNo
inner join DeptLocations on DeptLocations.DLocation = Project.PLocation
group by
DeptLocations.DLocation, Project.PName
Note: It's better to use explicit join instead of comma separated join.

JOIN subquery and different reault

I was writing an exercise about "Write a query to find the names (first_name, last_name) of the employees who are not supervisors"
I write it on my own and when i check the result or both, mine has less rows than the other.
I was using the JOIN function and the other doesn't.
I want help to know why two results are so different.
Thanks
the one i use join
SELECT
first_name, last_name
FROM
employees AS E
JOIN
departments AS D ON E.department_id = D.department_id
WHERE
NOT EXISTS( SELECT
0
FROM
departments
WHERE
E.manager_id = D.manager_id)
order by last_name;
the one doesn't use join
SELECT
b.first_name, b.last_name
FROM
employees b
WHERE
NOT EXISTS( SELECT
0
FROM
employees a
WHERE
a.manager_id = b.employee_id);
The big problem is that the NOT EXISTS subquery is referencing rows from the joined table. The manager_id column is qualified with D., and that's a reference to the joined table, not the table in the FROM clause of the subquery.
E.manager_id = D.manager_id
^^^^^^^ ^
We also suspect that an employee's supervisor is recorded in the employee row, as a reference to another row in the the employee table. But we don't have the schema definition or any example data, so we're just guessing.
It seems like there would be a supervisor_id in the employee table...
SELECT e.first_name
, e.last_name
, e.department_id
, d.department_id
FROM employees e
WHERE NOT EXISTS
( SELECT 1
FROM employees s
WHERE s.id = e.supervisor_id
)
ORDER
BY e.last_name
, e.first_name
It's also possible that some rows in employee have a value in the department_id column that don't have a matching row in the department table. If there is no matching row in department, the inner join will prevent the row from employee from being returned.
We can use an outer join when we want to return rows even when no matching row is found in the joined table. If we want to involve the departments table, because a "supervisor" is defined to be an employee that is the manager of a department, we can employ an anti-join pattern...
SELECT e.first_name
, e.last_name
FROM employees e
LEFT
JOIN departments d
ON d.manager_id = e.employee_id
WHERE d.manager_id IS NULL
ORDER
BY e.last_name
, e.first_name
Again, without a schema and some sample data, we're just guessing.
This query can be written using only Employees table, but in your query you have joined the employees table with department table. A query should be written with the minimal amount of tables that suffice your expected output, joining unnecessary tables may result in wrong out puts.
In this case you are joining Employees with department here what if there is no Department_ID in employee table for some employees, so those data will be dropped in the join and result won't be the expected.

MySQL self join if null function

I'm learning through MySQL Ver.5.7.17, please clarify on my below query.
I have a table employees which contains employee ids and corresponding manager ids, but few employees do not have manager ids as they belong to manager roles.
The table columns are employee id, first name, last name, manager id. I have designed below self join query which gives me employees with manager ids but I am looking for additional details of manager (who are also employees) who do not have manager ids (null).
SELECT E.EMPLOYEE_ID AS EMP_ID, CONCAT(E.FIRST_NAME,' ',E.LAST_NAME
) AS EMP_NAME,
M.EMPLOYEE_ID AS MGR_ID
,CONCAT(M.FIRST_NAME,' ',M.LAST_NAME) AS MGR_NAME
FROM EMPLOYEES AS E JOIN EMPLOYEES AS M
ON (E.MANAGER_ID =M.EMPLOYEE_ID) ORDER BY MGR_ID
I tried with IF NULL function in query as below and got below error message.
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL(M.EMPLOYEE_ID,'NO MGR') AS MGR_ID ,CONCAT(M.FIRST_NAME,' ',M.LAST_NAME) AS ' at line 2
SELECT E.EMPLOYEE_ID AS EMP_ID, CONCAT(E.FIRST_NAME,' ',E.LAST_NAME) AS EMP_NAME,
IF NULL(M.EMPLOYEE_ID,'NO MGR') AS MGR_ID
,CONCAT(M.FIRST_NAME,' ',M.LAST_NAME) AS MGR_NAME
FROM EMPLOYEES AS E JOIN EMPLOYEES AS M
ON (E.MANAGER_ID =M.EMPLOYEE_ID) ORDER BY MGR_ID
You can use COALESCE. Also, I think you need a left join in order to get those records as well which don't have a manager:
select E.EMPLOYEE_ID as EMP_ID,
concat (E.FIRST_NAME,' ',E.LAST_NAME) as EMP_NAME,
coalesce(M.EMPLOYEE_ID, 'NO MGR') as MGR_ID,
concat (M.FIRST_NAME,' ',M.LAST_NAME) as MGR_NAME
from EMPLOYEES as E
left join EMPLOYEES as M on (E.MANAGER_ID = M.EMPLOYEE_ID)
order by MGR_ID

SQL statement with JOIN and WHERE clause

I'm new to SQL and trying to solve this problem with two tables Employee and Department. I want display the names of all the employees of 'HR' and 'Sales' departments.
Tables are Employee (emp_id, emp_name, dept_id) and Department (dept_id, dept_name).
Thanks!
Try this:
Select e.emp_name from
Employee e inner join Department d
on e.dept_id = d.dept_id
Where d.dept_name in ('HR','Sales');
This query will compare the dept_id of Employee table and Department table. Those values matched will be returned. then out of all the fields you will select the emp_name and limit the employees belonging to HR and Sales department using the where clause.
As you only want to display employee data, only select from that table. The rest is criteria. You want their department to be either 'HR' or 'Sales', so the straight-forward way of writing this is the IN clause (you could also use the EXISTS clause):
select emp_name
from employee
where dept_id in
(
select dept_id
from department
where dept_name in ('HR', 'Sales')
);
I think this is more easy to read than to join the tables first, only to use one as a filter for the other.
select Employee.emp_name [Emplyee Name] from Employee inner join Department on Department.dept_id=Emplyee.emp_id where Department.dept_name='HR'
You can get like this,
SELECT [emp_name] FROM [dbo].[Employee] AS E
INNER JOIN [dbo].[Department] AS D
ON D.dept_id=E.dept_id
WHERE D.dept_name='HR' OR D.dept_name='Sales'