1

I'm trying to answer these questions but I'm not understanding the whole joining and functions part of MySQL. Can someone show me or explain these to me?

this is the link to the employee database we are using - https://github.com/datacharmer/test_db

  1. I want to know how many employees with each title were born after 1965-01-01.
  2. I want to know the average salary per title.
  3. How much money was spent on salary for the marketing department between the years 1990 and 1992?

This is what I have so far for each one.

1.

select count(title) as "Number Of Employees", title from titles GROUP BY title LIMIT 20;
SELECT d.dept_name as "Department", avg(s.salary) as "Average Salary" from departments d
INNER JOIN dept_emp de on de.dept_no = d.dept_no
INNER JOIN salaries s on s.emp_no = de.emp_no
GROUP BY d.dept_name;
  1. and this one seems like it's just those two put together so I completely don't understand it.
  • #2 You said "per title" but your query is per department. – Barmar Sep 13 '21 at 21:31
  • #1 Where is the check for the birth date? – Barmar Sep 13 '21 at 21:32
  • That's where I get confused I have a department there because that's the only way I can get it to work without an error I don't understand how to put the title as I get an error every time. For the birth date, I have this but that's it. –  Sep 13 '21 at 21:35
  • select count(birth_date, count(title)) as "Number Of Employees", title from titles GROUP BY title LIMIT 20; –  Sep 13 '21 at 21:35
  • `salaries` has multiple salaries for each employee, you need to get the most recent one. – Barmar Sep 13 '21 at 21:35
  • If putting `title` returns error, why didn't you post the error message [into your question](https://stackoverflow.com/posts/69169301/edit). And as for MySQL tools, there are a lot you can try, I personally have been using [SQLyog Community](https://github.com/webyog/sqlyog-community/wiki/Downloads) for the past decade. One of the most popular ones I gather is MySQL's own [workbench](https://dev.mysql.com/downloads/workbench/). Another one is [HeidiSQL](https://www.heidisql.com/). – FanoFN Sep 14 '21 at 00:20
  • Because I'm too new to explain fully why I did something the way I did and how I want it so if I show a command I got working and say basically this is what I want to do people seem to understand it more and can explain how to change it. –  Sep 15 '21 at 17:16

1 Answers1

1
  1. Join with the employee table so you can get the employee's date of birth.
SELECT t.title, COUNT(*) AS "Number of Employees"
FROM titles AS t
JOIN employees AS e ON e.emp_no = t.emp_no
WHERE e.birth_date > '1965-01-01'
GROUP BY t.title
  1. You need to get the most recent salary for each employee and average that. And you have to join with the titles table so you can average by title.
SELECT t.title, AVG(salary)
FROM titles AS t
JOIN employees AS e ON e.emp_no = t.emp_no
JOIN (
    -- subquery to get latest salary for each employee
    -- See https://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column?noredirect=1&lq=1
    SELECT s.emp_no, s.salary
    FROM salaries AS s
    JOIN (
        SELECT emp_no, MAX(from_date) AS max_date
        FROM salaries
        GROUP BY emp_no
    ) AS ms ON s.emp_no = ms.emp_no AND s.from_date = ms.from_date
) AS s ON e.emp_no = t.emp_no
GROUP BY t.title
  1. I'm not even sure what the third question means. Does it mean the total salaries for all employees during those years? This seems incredibly complex for a beginner exercise, since you have to deal with different start/end dates for employees, and changing salaries during that period. I'm not even sure how to do that in a single query.
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yeah this is the second week into MySQL and I don't understand it at all, I didn't even consider what you said as a possibility. "total salaries for all employees during those years?". –  Sep 13 '21 at 21:56
  • When I try to use the answer you gave for question 2 I get an error that says "ERROR 1054 (42S22): Unknown column 'ms.from_date' in 'on clause'" –  Sep 13 '21 at 22:36
  • Also, I'm doing this in the command prompt I say this because I don't know If there are another ways to use MySQL yet haha. –  Sep 13 '21 at 22:37