SELECT MAX from Counting SQL - mysql

I have a problem with extracting data from db. My code looks like:
SELECT MAX(maximum)as Number FROM
(
SELECT department_name, COUNT(employees.employee_id) AS maximum
FROM departments, employees
WHERE departments.department_id=employees.department_id
GROUP BY department_name
)t
and the result is:
Number
1 46
and this is the number of maximum employees in one of the departments.
The problem is that i want to have additional column with the name of the department in wich there is 46 employees. I tried something like:
select department_name, count(employees.employee_id)
from departments, employees
where departments.department_id=employees.department_id
group by department_name
having count(employees.employee_id) =
( SELECT MAX(maxx)FROM
(SELECT department_name, COUNT(employees.employee_id) AS maxx
FROM departments, employees
WHERE departments.department_id=employees.department_id
GROUP BY department_name
);
but it doesn't work. Please help!

try this
SELECT t.department_name, MAX(t.maximum) as Number FROM (
SELECT
department_name,
COUNT(employees.employee_id) AS maximum
FROM departments, employees
WHERE departments.department_id=employees.department_id
GROUP BY department_name
) t

How about this just order your results by maximum in descending order with limit 1,also use modern join syntax query
SELECT d.department_name,
COUNT(e.employee_id) AS maximum
FROM departments d
LEFT JOIN employees e
ON (d.department_id=e.department_id)
GROUP BY d.department_name
ORDER BY maximum DESC
LIMIT 1
This will give you the department name who has the highest count of employees

You could also tack on an additional JOIN to the first query you posted as such:
SELECT TOP 1 d.department_name, MAX(maximum) as Number FROM
(
SELECT department_id AS department_id, COUNT(employees.employee_id) AS maximum
FROM departments, employees
WHERE departments.department_id=employees.department_id
GROUP BY department_id
)t
JOIN departments d ON d.department_id = t.department_id
GROUP BY d.department_name
ORDER BY Number DESC
I tried this with a similar data model in my domain and it works quite nicely.

Related

Simple SQL query returning null value

I just started studying SQL queries.
I am practicing on this site: https://www.techonthenet.com/sql/joins_try_sql.php
I want to find:
"the name of the employee with the highest salary for every department".
My query is:
SELECT first_name, max(salary) FROM employees, departments
WHERE departments.dept_id=employees.dept_id
GROUP BY employees.dept_id
And I get null value for first_name :
I understand that the problem is due to the group by expressions. But how can I solve this?
Tables:
You can alternatively use row_number() like below, you don't need to join to departments table unless you need to show the name of the department:
Select e.*
from employees e
INNER JOIN
(
SELECT e.id, e.dept_id. e.first_name,
rn=row_number() over (partition by e.dept_id order by e.salary desc)
FROM employees e
) x ON x.id = e.id
where x.rn = 1
EDIT
(Since OP does not want to use row_number() function amd it turned out the query will be used in mysql instead of sql server) -> Can you please try this:
select em.*
from employees em, (
Select dept_id, max(salary) salary
from employees e
group by dept_id
) x on x.dept_id=em.dept_id and x.salary = em.salary
That should work but the online compiler does not accept join with a sub-query as far as I understand. Easiest solution I can think of, in this case:
select em.*
from employees em
where salary = (select max(salary) from employees em2 where em.dept_id = em2.dept_id)
SELECT top 2 first_name,max(salary) FROM employees, departments
WHERE departments.dept_id=employees.dept_id
GROUP BY first_name
order by max(salary) desc
try this:
SELECT e.first_name, e.salary FROM employees as e
INNER JOIN departments as d ON d.dept_id=e.dept_id
WHERE e.salary IN (SELECT max(salary) FROM employees GROUP BY dept_id)
Try this:
select first_name, b.dept_id, salary from employees a,
(
SELECT employees.dept_id, max(salary) as salary FROM employees, departments
WHERE departments.dept_id=employees.dept_id
GROUP BY employees.dept_id
)b where a.salary = b.salary and a.dept_id= b.dept_id

to show the name of the department which has maximum student count

There are two tables in total, one is "Department" which has department_name, department_block_number and department_id(primary key) in it and another is "Student" which has student_name,student_id and department_id(foreign key) in it. So based on this scenario, we have to show the name of the department which has maximum student count.
I have tried out something, below you can find my code but it didn't work as expected, so can you help me in rectifying it?
select u
from
(select count(s.student_id) cnt,d.department_name u
from department d
join student s using(department_id)
group by d.department_name
where cnt==(select max(cntt) from (select
count(ss.student_id) cntt,dd.department_name
from department dd
join student ss using(department_id)
group by dd.department_name)
)
);
Note: This problem belongs to SubQueries section.
You only need to get a count of all students in all departments in descending order, then limit to the first result:
SELECT a.`department_name`
count(b.`student_id`) as `num_students`
FROM `Department` a
JOIN `Student` b
ON a.`department_id` = b.`department_id`
GROUP BY `a.department_id`
ORDER BY count(b.`student_id`) DESC
LIMIT 1;

MySQL: Combining two tables with null values does not print 0

In my code I am trying to combine two data tables, Employee and Department. I tried to write a query to print the respective Department Name and number of employees for all departments, even the unstaffed ones. My query looks like this:
SELECT department.name, count(department.name) AS CountOfNAME
FROM department LEFT JOIN employee ON department.dept_id = employee.dept_id
GROUP BY department.name
ORDER BY Count(department.name) DESC, department.name ASC;
And the result is:
Engineering 5
Recruitment 5
Sales 3
Product 2
Finance 1
Operations 1
Research&Development 1
This code works in that it orders departments by number of employees, and then alphabetically, but Finance and Research&Development are not supposed to have any people in them. Is there any way to correctly display those results as having 0 employees? It seems to be a hard thing to do in SQL because of how join works.
The COUNT function should ignore NULL values, giving you a zero count for the finance and research departments. The problem is that you are counting a column in the department table, which always will be non NULL due to that this table is on the left side of the LEFT JOIN. Instead, try counting a column in the employee table:
SELECT department.name,
COUNT(employee.dept_id) AS CountOfNAME
FROM department
LEFT JOIN employee
ON department.dept_id = employee.dept_id
GROUP BY department.name
ORDER BY COUNT(employee.dept_id) DESC,
department.name ASC;
I suggest you create a view for the tallies of employees by department e.g.
CREATE VIEW DepartmentEmployeeTallies
AS
SELECT dept_id, COUNT(*) AS tally
FROM employee
UNION
SELECT dept_id, 0 AS tally
FROM department
WHERE dept_id NOT IN ( SELECT dept_id FROM employee );
Then things resolve to a simple join:
SELECT name, tally
FROM department
NATURAL JOIN
DepartmentEmployeeTallies;

Using count function to count records from two tables and returns the same even if count is zero

I have two tables: Employee and Department
Fields of Employee : id, name, dept_id
Fields of Department: dept_id, name
I need a query which fetches me department name and number of employees per department along with it, even if number of employees for a department is zero.
I tried the query below, but it returns only those departments which have number of employees more than zero.
select d.name, count(e.id) as "Number of employees per department"
from department d, employee e
where d.DEPT_ID=e.DEPT_ID
group by d.name
order by count(e.id) desc;
Please help.
You need to outer join your tables.
FROM department d
LEFT JOIN employee e
ON d.DEPT_ID=e.DEPT_ID
The following query gives the desired result.
SELECT DEPARTMENT.NAME,COUNT(EMPLOYEE.DEPT_ID)
FROM DEPARTMENT LEFT OUTER JOIN EMPLOYEE ON EMPLOYEE.DEPT_ID = DEPARTMENT.DEPT_ID GROUP BY DEPARTMENT.NAME ;

How to do sum of alias column that is already sum in mysql

I have mysql query that is.
select d.department_id,d.department_name,sum(e.salary) AS amount
from department d inner join employee e on d.department_id=e.department_id
group by d.department_id,d.department_name
I get grand total by using this query .
select d.department_id,d.department_name,sum(e.salary) AS amount
from department d inner join employee e on d.department_id=e.department_id
group by d.department_id,d.department_name WITH ROLLUP
But I want this grand total by sum amount column alias like this
select d.department_id,d.department_name,sum(e.salary) AS amount, sum(SELECT(amount)) AS grand_total
from department d inner join employee e on d.department_id=e.department_id
group by d.department_id,d.department_name
My database structure is .
And my tables are
But I want like this .
Please Help me any one.
If you want an additional column use a join or subquery:
select d.department_id, d.department_name, sum(e.salary) AS amount,
tot.salary
from department d inner join
employee e
on d.department_id = e.department_id inner join
(select sum(e.salary) as salary from employee) tot
group by d.department_id, d.department_name, tot.salary;
Note: this assumes that the total from the employee table is the total you want. This seems like a reasonable assumption, but it might not be accurate if some employees do not have departments or if some employees are in multiple departments. In that case, you just need to repeat the join to department in the subquery.