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;
Related
enter image description hereIt is a sql query on the github "employees" database. I have to calculate the average salary that each employee has received, order it from highest to lowest and indicate the job title of the employee. I have used this statement and it works fine, but I can't locate DESC, I can't get it to work for me. I am learning and it is the first database that I use with so many tables. Let's see if someone can help me. Thanks
select
concat (employees.first_name,' ',employees.last_name) as 'nombre_completo',
salaries.emp_no as 'numero_empleado',
#departments.dept_no as 'numero_departamento',
departments.dept_name as 'nombre_departamento',
avg (salaries.salary) as 'avg_salario'
from salaries
inner join dept_emp on salaries.emp_no = dept_emp.emp_no
inner join employees on salaries.emp_no = employees.emp_no
inner join departments on dept_emp.dept_no = departments.dept_no
group by salaries.emp_no,dept_emp.dept_no
I tried various methods. Sorry if I don't understand well, I'm using a translator and I'm new to sql, I'm learning
select
concat (employees.first_name,' ',employees.last_name) as
'nombre_completo',
salaries.emp_no as 'numero_empleado',
#departments.dept_no as 'numero_departamento',
departments.dept_name as 'nombre_departamento',
avg (salaries.salary) as 'avg_salario'
from salaries
inner join dept_emp on salaries.emp_no = dept_emp.emp_no
inner join employees on salaries.emp_no = employees.emp_no
inner join departments on dept_emp.dept_no = departments.dept_no
group by salaries.emp_no,dept_emp.dept_no
order by salary DESC
select
concat (employees.first_name,' ',employees.last_name) as
'nombre_completo',
salaries.emp_no as 'numero_empleado',
#departments.dept_no as 'numero_departamento',
departments.dept_name as 'nombre_departamento',
salaries.salary as 'salario'
avg (salaries.salary) as 'avg_salario'
from salaries
inner join dept_emp on salaries.emp_no = dept_emp.emp_no
inner join employees on salaries.emp_no = employees.emp_no
inner join departments on dept_emp.dept_no = departments.dept_no
group by salaries.emp_no,dept_emp.dept_no
order by salary DESC
select
concat (employees.first_name,' ',employees.last_name) as
'nombre_completo',
salaries.emp_no as 'numero_empleado',
#departments.dept_no as 'numero_departamento',
departments.dept_name as 'nombre_departamento',
salaries.salary as 'salario'
avg (salaries.salary) as 'avg_salario'
from salaries
inner join dept_emp on salaries.emp_no = dept_emp.emp_no
inner join employees on salaries.emp_no = employees.emp_no
inner join departments on dept_emp.dept_no = departments.dept_no
group by salaries.emp_no,dept_emp.dept_no
order by salario DESC
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
Im trying to get the first 100 rows consisting of First Name, Last Name, Department Name, Title, Salary from the employee database but not sure if Im doing it correctly. I tried the query below and it seems to work but doesn't feel right specially since there are no ON statements for the last 2 joins. Is it correct and is there a more efficient way I can perform this query.
This is the link to the database tables graph to give you a idea of the database.
https://dev.mysql.com/doc/employee/en/images/employees-schema.png
My query that seems to work:
SELECT e.first_name, e.last_name, dpn.dept_name, t.title, s.salary
FROM employees e, dept_emp dp
INNER JOIN departments dpn
ON dp.dept_no = dpn.dept_no
INNER JOIN salaries s
INNER JOIN titles t
LIMIT 100;
I tried the code below but kept getting error "#1054 - Unknown column 'e.emp_no' in 'on clause' " starting for the second to last ON statement.
SELECT e.first_name, e.last_name, dpn.dept_name, t.title, s.salary
FROM employees e, dept_emp dp
INNER JOIN departments dpn
ON dp.dept_no = dpn.dept_no
INNER JOIN salaries s
ON e.emp_no = salaries.emp_no
INNER JOIN titles t
ON e.emp_no = t.emp_no
LIMIT 100;
Thanks!
You have no join clause between e table and dp table eg:
SELECT e.first_name, e.last_name, dp.dept_name, t.title, s.salary
FROM employees e
INNER JOIN dept_emp dp on e.emp_no = dp.emp_no
INNER JOIN departments dpn ON dp.dept_no = dpn.dept_no
INNER JOIN salaries s ON e.emp_no = s.emp_no
INNER JOIN titles t ON e.emp_no = t.emp_no
LIMIT 100;
Try it like this:
SELECT e.first_name, e.last_name, departments.dept_name, titles.title, salaries.salary
FROM employees e
INNER JOIN dept_emp
ON e.emp_no = dept_emp.emp_no
INNER JOIN departments
ON dept_emp.dept_no = departments.dept_no
INNER JOIN titles
ON e.emp_no = titles.emp_no
INNER JOIN salaries
ON e.emp_no = salaries.emp_no
LIMIT 100;
The "On" clause in inner join is optional. If you do not have "On" clause it means a "cross join".
If what you want is inner join, then you should have an "On" clause and specify which columns should equate.
SELECT e.first_name,
e.last_name,
departments.dept_name,
titles.title,
salaries.salary
FROM employees e
LEFT JOIN dept_emp
ON e.emp_no = dept_emp.emp_no
LEFT JOIN departments
ON dept_emp.dept_no = departments.dept_no
LEFT JOIN titles
ON e.emp_no = titles.emp_no
LEFT JOIN salaries
ON e.emp_no = salaries.emp_no
LIMIT 100;
try this one too because
INNER JOIN: returns rows when there is a match in all tables.
LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table.
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')
Here are my tables:
employees
id
name
salary
dept_id
departments
id
name
SELECT employees.id, employees.name, empolyees.dept_id, departments.id,
departments.name
FROM employees, departments
WHERE employees.dept_id = departments.dept_id
ORDER BY employees.name;
Am I joining these two tables right?
Better use the explicit join syntax
SELECT e.id, e.name, e.dept_id, d.id, d.name
FROM empolyees e
INNER JOIN departments d ON e.dept_id = d.id
ORDER BY e.name
And you used departments.dept_id which does not exist. It is departments.id
could say:
SELECT e.id, e.name, empolyees.dept_id, d.id, d.name
FROM employees e
inner join departments d on e.dept_id = d.dept_id
ORDER BY e.name;