mySQL Queries through multiple tables - mysql

My first post here. I have an assignment to deliver in mysql and I have found a difficulty in 2 questions.
First and foremost the tables of a database are the following:
departments (dep_no, dep_name)
dept_emp (emp_no, dept_no, from_date, to_date)
dept_manager (dept_no, emp_no, from_date, to_date)
employees (emp_no, birthdate, first_name, last_name, gender, hire_date)
salaries (emp_no, salary, from_date to_date)
titles (emp_no, title, form_date, to_date)
and the 2 questions that I am having difficulty on are those 2 :
Which department pays the highest salary currently and who is the department manager?
What is the current average salary of Marketing Department?
I would be really thankful for some help.
Cheers!

Try this for the first one:
select d.dep_name AS department
, CONCAT(e.first_name, e.last_name) AS department_manager
from salaries S
inner join dept_emp de on s.emp_no = de.emp_no
inner join departments d on de.dept_no = d.dept_no
inner join dept_manager dm on de.dept_no = d.dept_no
inner join employees e on dm.emp_no = e.emp_no
where s.salary = (select MAX(salary) from salaries)
And the second one (if Marketing Department is on table dept_manager):
select AVG(salary)
from salaries S
inner join dept_manager dm on s.emp_no = dm.emp_no

Which department pays the highest salary currently and who is the department manager?
select dept_no from
dept_emp where emp_no in
(select emp_no from salaries
where salary = (select max(salary) from salaries) )
What is the current average salary of Marketing Department?
select avg(s.salary) as marketing_avg_salary
from salaries s join dept_emp de
on s.emp_no = de.emp_no
where de.dept_no = (select dept_no from departments where dept_name = 'Marketing')

Related

MySQL: Get the count and avg from join tables

I have the following 5 tables:
employees as e
dept_manager as dm
departments as d
titles as t
salaries as s
I would like to know how many male and female managers we have, along with the avg salary per gender within a department.
I wrote the query below but the numbers don't add up:
select
d.dept_name,
count(e.gender),
avg(salary)
from employees as e
Join dept_manager as dm on e.emp_no = dm.emp_no
Join departments as d on d.dept_no = dm.dept_no
Join titles as t on e.emp_no = t.emp_no
Join salaries as s on dm.emp_no = s.emp_no
where t.title ='manager'
group by d.dept_name, e.gender
order by d.dept_name ASC;

Retrieving data from three different columns

I have three different tables salaries, dept_managers(Department managers), and departments
the first two tables are connected through emp_no(Employee number), While the other two tables are connected through dept_no(the department number),
I want to have a query that gives me the average amount of salaries that each department has.
I tried this query but It did not work.
SELECTd.dept_name,dm.dept_noFROM departments d
JOIN dept_manager dm ON dm.dept_no = d.dept_no
JOIN(
SELECT AVG(salary) AS Average_salary
FROM salaries s
GROUP BY salary)
ON d.emp_no = dm.emp_no
;
maybe I'm missing something here but wouldn't you just join department to employees and then join salaries to employees and get the avg salary grouped by department?
select d.dept_name, avg(s.salary)
from departments d join employees e on (e.dept_no = d.dept_no)
join salaries s on (s.emp_no = e.emp_no)
group by d.dept_name

MySQL Workplace Query

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)

MySql Choose users by their latest date

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;

Find employee with lowest salary but also his department

I need to find employee with lowest salary- first and last name, gender and department.
I tried with this but I have problems adding department name.
SELECT first_name, last_name, gender, salary FROM salaries
JOIN employees using(emp_no) where order by salary desc limit 0,1;
Any help?
schema-link
This should work, you are missing a couple of joins among other things (like the department name in the field list):
SELECT e.first_name, e.last_name, e.gender, s.salary, d.dept_name
FROM salaries AS s
INNER JOIN employees AS e ON s.emp_no=e.emp_no
INNER JOIN dept_emp AS de ON de.emp_no=e.emp_no
INNER JOIN departments AS d ON d.dept_no=de.dept_no
ORDER BY s.salary ASC limit 0, 1