I have this table:
EMPLOYEES(Code, Name, Surname, Type, Department, Salary)
I need to display the department with the HIGHEST total expense for salaries WITHOUT using a view. Is that possible? The solution with a view is this:
CREATE VIEW DEPEXPENSES (DEPNAME, EXPENSE) AS
SELECT DEPARTMENT, SUM(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT
SELECT DEPNAME
FROM DEPEXPENSES
WHERE EXPENSE=(SELECT MAX(EXPENSE) FROM DEPEXENSES)
Thanks.
The question is not too clear, but I think you want to run your second query without using a view. One solution is this:
select
DEPARTMENT,
SUM(SALARY)
from
EMPLOYEES
group by
DEPARTMENT
having
SUM(SALARY)=(
select MAX(SALARY) from (
select DEPARTMENT, SUM(SALARY) as SALARY
from EMPLOYEES
group by DEPARTMENT
) s
)
(you can apply a max aggregated function on a subquery that already contains an aggregated function)
Related
I have a table with names, job, salary etc.. what i want to do is to list all names, salary and a third column with their salary as a percentage of total salary, like this.
The table looks like this.
I am new to mysql and any help is good help.
Thanks
You could compute the total salary in a subquery, and then CROSS JOIN that with the employee table to be able to do the computation, like :
SELECT
emp.epname,
emp.salary,
emp.salary/tot.salary * 100
FROM
employees emp
CROSS JOIN (
SELECT SUM(salary) salary FROM employees
) tot
With MySQL 8.0, window functions make it easier :
SELECT
epname,
salary,
salary/(SUM(salary) OVER()) * 100
FROM employees
This question already has answers here:
Group by minimum value in one field while selecting distinct rows
(10 answers)
Closed 4 years ago.
I have a sql query that outputs employees who have the minimum salary across departments. It does so by finding the minimum salary for each department and finding people with salaries that match ANY of those. In this case, Person 1 can belong to department A and have a salary of 70k (even though her department's minimum is 45k) and be returned in the query if another department's minimum salary is 70k. However, what if I instead want to output names of people who have the minimum salary for their respective department (so Person 1 would not be returned anymore). Here is the current sql query:
SELECT first_name, last_name, salary, department_id
FROM employees
WHERE salary IN
( SELECT MIN(salary)
FROM employees
GROUP BY department_id
);
If you have MySQL 8.0 then the simplest approach is using window function:
SELECT *
FROM (SELECT *, RANK() OVER(PARTITION BY department_id ORDER BY salary DESC) AS r
FROM tab) sub
WHERE r = 1;
Or:
SELECT first_name, last_name, salary, department_id
FROM employees e
WHERE (department_id,salary) IN
( SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id);
Instead fo a IN clause you can also use an inner join
SELECT first_name, last_name, salary, department_id
FROM employees
INNER JOIN ( SELECT department_id, MIN(salary) min_sal
FROM employees
GROUP BY department_id
) t on t.department_id =employees.department_id
and employees.salary = t.min_sal;
this should be better for performance
So I have a table with the following columns: id, Fname, Lname, city and desc
What I am trying to accomplish is to create a function that counts how many employees are working in each city.
Normally I would just use something like
select city, count(*) as NumofEmp from employee group by city
but at the moment I have to create a function for this. How could I accomplish this?
select city, count(name) as NumofEmp from employee group by city
Below shows my original MY SQL table.
SELECT name, COUNT(city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
I need following result.
SAM has 4 records. But there are city of London in two records.
I need How many different cities in SAM's Records.
I did lots of queries, but I couldn't create my SQL query.
Add DISTINCT to the count parameter:
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
SELECT name, COUNT(DISTINCT city) AS "count_no"
FROM emp
GROUP BY name
ORDER BY count_no DESC;
I have two tables, EMP and Salary, where on EMP table I have the following fields:
id, emp_name,designation
And on the Salary table I have the following fields:
id, emp_id, salary
How can I get the name of employee whose salary is greater than 15000?
Use JOIN and WHERE clause
SELECT emp_name
FROM EMP
JOIN Salary
ON EMP.id = Salary.emp_id
WHERE salary > 15000
You can use an inner query, like this:
SELECT emp_name
FROM emp
WHERE id IN (SELECT emp_id FROM salary WHERE salary > 15000)
Is this homework? I will just give you the necessary hints rather than a complete statement:
The reason for salary being a table on its own is that an employee can have more than one salary. (Otherwise salary would just be a field in emp table).
So first join emp and salary to get all salaries for all employees. Then group by employees to get the sum of the salaries for each employee. Then at last filter your results to salaries over 15000. Do you know already how to filter group results?
There is more than one way to write a correct statement. I am sure you will find one using the hints given. Good luck!