I have the following employee table:
Employee(Columns: Emp_ID, First_Name, Last_Name, DOB)
Department(Columns: Dept_Name, Dept_ID)
Dept_Emp(Columns: Emp_ID, Dept_ID)
Say I am given the employee ID of 1 and I want to find all of the employees who work in the same department as the employee with ID 1 and return their name from the Employee table. I am having trouble formulating the query for such a task.
I was thinking it would look start to look something like:
SELECT Dept_Emp.Emp_ID
FROM Dept_Emp d1, Dept_Emp d2
WHERE d1.Emp_ID = '1'
AND d1.Dept_ID = d2.Dept_ID;
On the inside of some nested query? But I am not sure, as queries get more complex I get more confused and I tend to overthink. Thank you for your time.
Simple solution would be using subquery.
SELECT Emp_ID, First_Name, Last_Name, DOB
FROM Employee
INNER JOIN Dept_Emp ON Employee.Emp_ID = Dept_Emp.Emp_ID
WHERE Dept_Emp.Dept_ID = (SELECT D1.Dept_ID FROM Dept_Emp D1 WHERE D1.Emp_Id = 1 )
Note: If an employee belongs to multiple departments use IN operator.
SELECT Emp_ID, First_Name, Last_Name, DOB
FROM Employee
INNER JOIN Dept_Emp ON Employee.Emp_ID = Dept_Emp.Emp_ID
WHERE Dept_Emp.Dept_ID IN (SELECT D1.Dept_ID FROM Dept_Emp D1 WHERE D1.Emp_Id = 1 );
You can write simple query for output:
SELECT a.Emp_ID, a.First_Name, a.Last_Name, a.DOB, b.Dept_ID FROM Dept_Emp as b LEFT JOIN Employee as a ON a.Emp_ID = b.Emp_ID WHERE b.Dept_ID=(SELECT Dept_ID FROM Dept_Emp WHERE Emp_ID = 1 LIMIT 1)
Related
Please refer below tables and fields.
Create an “employee” database and 4 tables (hobby, employee, employee_salary, employee_hobby).
hobby: id, name
employee: id, first_name, last_name, age, mobile_number, address
employee_salary: id, foreign key of employee, salary
employee_hobby: id, foreign key of the employee, foreign key of hobby
I execute this query as follows:
SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name, SUM(es.salary) AS total_salary,
(SELECT GROUP_CONCAT(h.name)
FROM hobby AS h
INNER JOIN hobby ON h.id = eh.fk_hobby_id) AS hobby_name
FROM employee_hobby AS eh
INNER JOIN employee AS e ON e.id = eh.fk_employee_id
INNER JOIN employee_salary AS es ON es.fk_employee_id = eh.fk_employee_id
GROUP BY eh.fk_employee_id;
But I fetch all same hobby name in single row for particular record.
output image
I need hobby_name for every record like sports,gaming or gaming,travelling etc.
This is not correct:
...
(SELECT GROUP_CONCAT(h.name)
FROM hobby AS h
INNER JOIN hobby ON h.id = eh.fk_hobby_id) AS hobby_name
FROM employee_hobby AS eh
...
Just join hobby table in the query like other tables:
SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name,
SUM(es.salary) AS total_salary,
GROUP_CONCAT(h.name)
FROM employee_hobby AS eh
INNER JOIN hobby h ON h.id = eh.fk_hobby_id
INNER JOIN employee AS e ON e.id = eh.fk_employee_id
INNER JOIN employee_salary AS es ON es.fk_employee_id = eh.fk_employee_id
GROUP BY eh.fk_employee_id;
Try to get the salary from the Employes by the latest date but getting multiple outputs. Tried with distinct also didn't work.
I am sure only like one word is missing for me to solve this :D
SELECT DISTINCT first_name, last_name, salary, to_date
FROM employees e
LEFT JOIN salaries s
ON e.emp_no = s.emp_no
AND salary >= 70000 // Here I miss the line which will only take the latest date
On MySQL versions 8+, ROW_NUMBER is a good way to handle this requirement:
WITH cte AS (
SELECT DISTINCT first_name, last_name, salary, to_date,
ROW_NUMBER() OVER (PARTITION BY first_name, last_name
ORDER BY to_date DESC) rn
FROM employees e
LEFT JOIN salaries s ON e.emp_no = s.emp_no AND s.salary >= 70000
)
SELECT first_name, last_name, salary, to_date
FROM cte
WHERE rn = 1;
Note that this answer assumes that the combination of an employee's first and last name would always be unique. In general, it would be much better to use a unique employee ID of some kind here.
This answer might not be complete without a version which would also run on MySQL 5.7 or earlier:
SELECT e.first_name, e.last_name, s1.salary, s1.to_date
FROM employees e
LEFT JOIN salaries s1 ON e.emp_no = s1.emp_no AND s1.salary >= 70000
INNER JOIN
(
SELECT emp_no, MAX(to_date) AS max_to_date
FROM salaries
GROUP BY emp_no
) s2
ON s1.emp_no = s2.emp_no AND s1.to_date = s2.max_to_date;
There are two tables
1) Employee
id | Name | Department | Dob
2) Salary
id | salary
I want to find the salary of the youngest and eldest employee in each department.
But using the the following query i am not able to get the correct id,salary.
SELECT salary.id,employee.Dept,salary.salary,MIN(employee.DoB)
from employee
INNER JOIN salary ON salary.id = employee.id GROUP by Dept
The above query is returning correct Dob but the ids and the salary are not matching with the Date of birth.
If you are running MySQL 8.0, just use window functions:
select *
from (
select
e.*,
s.salary,
row_number() over(partition by department order by dob asc) rn_asc,
row_number() over(partition by department order by dob desc) rn_desc
from employee e
inner join salary s on s.id = employee.id
) t
where 1 in (rn_asc, rn_desc)
In earlier versions, one option is to join with an aggregate query:
select e.*, s.salary
from employee e
inner join salary s on s.id = employee.id
inner join (
select department, min(dob) min_dob, max(dob) max_dob
from employee
group by department
) d on d.department = e.department and e.dob in (d.min_dob, d.max_dob)
I think I would use = twice with correlated subqueries:
select s.*, e.department
from salary join
employee e
on s.id = e.id
where e.dob = (select min(e2.dob) from employee e where e2.department = e.department) or
e.dob = (select max(e2.dob) from employee e where e2.department = e.department) ;
With an index on employee(department, dob), I would expect this to have very good performance.
I have 2 tables in my database.
Table 1. employee
id
name
department_id
Table 2. department
id
name
What will be the query to fetch all employees with their department?
So I have written this query
SELECT employee.name
, department.name
FROM employee
JOIN department
ON employee.department_id = department.id
And this seems to be correct but I am not able to write a query if I want to fetch only the department that has the highest number of employees. How can I achieve this?
To guarantee just one department...
SELECT
*
FROM
department
WHERE
id = (SELECT department_id
FROM employee
GROUP BY department_id
ORDER BY COUNT(*) DESC
LIMIT 1
)
Note, if two departments are tied with joint maximum employees, this will still only select One of them (arbitrarily chosen, potentially different each time).
To handle ties, you could do the following...
SELECT *
FROM department
WHERE id IN (SELECT department_id
FROM employee
GROUP BY department_id
HAVING COUNT(*) = (SELECT COUNT(*)
FROM employee
GROUP BY department_id
ORDER BY COUNT(*) DESC
LIMIT 1
)
)
This is a real pain to handle in MySQL. Here is one option:
SELECT d1.id, d1.name
FROM department d1
INNER JOIN employee e1
ON d1.id = e1.department_id
GROUP BY d1.id, d1.name
HAVING COUNT(*) = (SELECT COUNT(*) FROM department d2 INNER JOIN employee e2
ON d2.id = e2.department_id
GROUP BY d2.id ORDER BY COUNT(*) DESC LIMIT 1);
Note that if you were using a database with analytic function support, such as SQL Server, then the problem gets much easier:
SELECT id, name
FROM
(
SELECT d.id, d.name, DENSE_RANK() OVER (ORDER BY COUNT(*) DESC) dr
FROM department d
INNER JOIN employee e
ON d.id = e.department_id
GROUP BY d.id, d.name
) t
WHERE t.dr = 1;
This question can be solved in multiple ways:
Using sub query
SELECT name FROM department
WHERE id IN
(SELECT department_id FROM employee HAVING COUNT(department_id)
IN
(SELECT MAX(COUNT(department_id)) FROM employee) GROUP BY department_id)
Using Join
SELECT name FROM employee e
INNER JOIN
department d ON e.department_id = d.id
HAVING COUNT(e.department_id)
IN
(SELECT MAX(COUNT(department_id)) from employee) group by department_id)
first check the column related two types have same name, same data type and the use subquery
SELECT name
FROM department
WHERE id IN (
SELECT department_id
FROM employees
HAVING COUNT(department_id) IN (
SELECT MAX(COUNT(dept_id))
FROM employees
)
GROUP BY department_id
)
Your query should work for the first question.
for the second, You can use this. The sub query would give you the dept Id for the most employees, which the outer query would give additional details for.
select * from department where department_id in
(select limit 1 Employee.department_id from Employee group by department_id
order by count(Employee.name) desc)
I have 2 tables employees(id, first_name, last_name, salary, department_id_ and department(id, name) and I want to show number of employees in each department.
I have this question here:
SELECT department.name, COUNT(*) AS 'employees_number'
FROM department
LEFT JOIN employees
ON employees.department_id = department.id
GROUP BY department.id, department.name;
But for some reason, in departments where I have no people, it shows a number of employees as 1. Any idea why this is happening?
With an outer join you still get a result row when no match in the outer table is found. Only all employee column values are null then.
So rather than count the records, you want to count matched records, i.e. where an employee was found and its data is not null. So Count a column in the employee table (nulls are not counted, when counting a column or expression). E.g. use COUNT(e.department_id) or COUNT(e.id):
SELECT d.name, COUNT(e.id) AS employees_number
FROM department d
LEFT JOIN employees e ON e.department_id = d.id
GROUP BY d.id, d.name;
What I prefer though, is to aggregate/count before joining. The query looks a bit more complicated, but is less prone to errors on future query changes:
SELECT d.name, COALESCE(e.how_many, 0) AS employees_number
FROM department d
LEFT JOIN
(
SELECT department_id, COUNT(*) AS how_many
FROM employees
GROUP BY department_id
) e ON e.department_id = d.id;
As it's one aggregated column only you want, you can move the subquery to your SELECT clause and get thus a simpler query:
SELECT
d.name,
(
SELECT COUNT(*)
FROM employees e
WHERE e.department_id = d.id
) AS employees_number
FROM department d;
Using SUM instead of COUNT also can give you what you want:
SELECT
department.name,
SUM(CASE WHEN employees.id IS NOT NULL THEN 1 ELSE 0 END) AS 'employees_number'
FROM department
LEFT JOIN employees
ON employees.department_id = department.id
GROUP BY department.id, department.name;
SQL Fiddle:
http://sqlfiddle.com/#!9/8b8976/1
select department.name, count(employee.id) as co from
department left join employee on
department.id = employee.dept_id group by department.name
order by co desc, department.name asc