MySQL Error: #1111 - Invalid use of group function - mysql

I need to display the department name and the number of employees of the department with more employees.
However I am receiving the following error (#1111 - Invalid use of group function). Here's the query:
select e.dept_no, dnombre, count(*) from emple e, depart d
where e.dept_no=d.dept_no
group by e.dept_no, dnombre
having count(*)=(select max(count(*)) from emple group by dept_no);
I need help with this query in MySQL.

Instead of Nesting aggregates use Limit to get the max count. Try this..
select e.dept_no, dnombre, count(*) from emple e, depart d
where e.dept_no=d.dept_no
group by e.dept_no, dnombre
having count(*)=(select count(*) from emple group by dept_no order by count(*) Desc limit 1);

Used a left join in case all departments had no employees which is not handled in this set due to an inner join.
used a group by on d.dept_no since e.dept number could be null.
ordered by count of d.depts dec
used a limit operator to only return "ONE" max record. Note, this approach will not show ties..
.
SELECT count(d.dept), d.dnombre, d.dept_no
FROM Emple e
LEFT JOIN depart d
on E.dept_no=d.dept_no
GROUP BY d.dept_no, d.dnombre
ORDER BY count(d.dept) desc
LIMIT 1;

if you want to use the MAX function you can also do it like this:
SELECT e.dept_no, dnombre, COUNT(*)
FROM emple e, depart d
WHERE e.dept_no=d.dept_no
GROUP BY e.dept_no, dnombre
HAVING COUNT(*) = (SELECT MAX(cnt) FROM (SELECT COUNT(*) AS cnt FROM emple GROUP BY dept_no) T);

Related

Join 2 different counts from 2 different tables into one subtable in sql

I'm having a problem in where i want to count how many medals in total a country has won from both the individual and team competitions does not give me the disered outcome. i have managed so far tocome up with this.
select distinct C.Cname as Country, count(i.medal) as Medals_Won
from individual_results as i, Country as C, participant as p
where (i.Olympian = p.OlympicID and C.Cname = p.country)
union
select distinct C.Cname, count(r.medal) as medals_Won
from team_results as r, Country as C, participant as p, team as t
where (r.team = t.TeamID and t.Member1 = p.OlympicID and C.Cname = p.Country)
group by C.Cname
order by medals_won desc
enter image description here
but i get this result.
even tho if i run the two separate pieces of code i ge the wanted restuls that is enter image description here
You say you can run your query and it gives you a result. This is bad. It indicates that you are MySQL's notorious cheat mode that lets you run invalid queries.
You have something like this:
select ...
union
select ...
group by ...
order by ...
There are two queries the results of which you glue together, namely
select ...
and
select ...
group by ...
So, your first query becomes:
select distinct C.Cname as Country, count(i.medal) as Medals_Won
from individual_results as i, Country as C, participant as p
where (i.Olympian = p.OlympicID and C.Cname = p.country)
You COUNT medals, i.e. you aggregate your data. And there is no GROUP BY clause. So you get one result row from all your data. You say you want to count all rows for which i.medal is not null. But you also want to select the country. The country? Which??? Is there just one country in the tables? And even then your query would be invalid, because still you'd have to tell the DBMS from which row to pick the country. You can pick the maximum country (MAX(C.Cname)) for instance or the minimum country (MIN(C.Cname)), but not the country.
The DBMS should raise an error on this invalid query, but you switched that off.
Make sure in MySQL to always
SET sql_mode = 'ONLY_FULL_GROUP_BY';
It is the default in more recent versions, so either you are working with old software or you switched from good mode to bad mode voluntarily.
And talking of old software: Even at the first moment MySQL was published, comma joins had long been deprecated. They were made redudant in 1992. Please don't ever use commas in your FROM clause. Use explicit joins ([INNER] JOIN, LEFT [OUTER] JOIN, etc.) instead.
As to the task, here is a straight-forward solution with joins:
select
c.cname as country,
coalesce(i.medals, 0) as medals_individual,
coalesce(t.medals, 0) as medals_team,
coalesce(i.medals, 0) + coalesce(t.medals, 0) as medals_total
from country c
left outer join
(
select p.country, count(ir.medal) as medals
from participant p
join individual_results ir on ir.olympian = p.olympicid
group by p.country
) i on on i.country = c.name
left outer join
(
select p.country, count(ir.medal) as medals
from participant p
join team t on t.member1 = p.olympicid
join team_results tr on tr.team = t.teamid
group by p.country
) t on on t.country = c.name
order by medals_total desc;
You should sum the union result for each of the subquery grouped by cname
select t.Cname , sum( t.Medals_Won)
from (
select C.Cname as Country, count(i.medal) Medals_Won
from individual_results i
inner join participant p ON i.Olympian = p.OlympicID
inner join Country C ON C.Cname = p.country
group by C.Cname
union
select distinct C.Cname, count(r.medal)
from team_results as r
inner join team as t ON r.team = t.TeamID
inner join participant as p ON t.Member1 = p.OlympicID
inner join Country as C ON C.Cname = p.Country
group by C.Cname
) t
group by t.Cname
order by sum( t.Medals_Won) desc

Reduce number of subqueries

I've 2 tables emp and expenditure.
Emp:
ID, NAME
Expenditure:
ID, EMP_ID, AMOUNT
Each emp has a limit of 100 that he/she can spend. We want to check which emp has expenditure > 100.
Output attributes needed: Emp name, exp id, amount
My query:
SELECT E.NAME,
EXP.ID,
EXP.AMOUNT
FROM EMP E
INNER JOIN expenditure EXP ON E.ID = EXP.EMP_ID
WHERE E.ID in
(SELECT EMP_ID
FROM
(SELECT EMP_ID,
SUM(AMOUNT) AS TOTAL
FROM expenditure
GROUP BY EMP_ID
HAVING SUM(AMOUNT) > 100.00
ORDER BY TOTAL DESC) SUBQ)
ORDER BY EXP.AMOUNT desc;
Is it possible to optimize this?
Just like code, SQL queries can be written in many different ways. Run an Execution Plan on your SQL. Check here and here
Below is more "conciser" version although it may not be any more optimised than your current code. Use Execution Plans to analyse performance.
SELECT E.NAME,
E.ID, -- THIS IS EMPLOYEE ID NOT EXPENDITURE ID
EXP.EMP_SPENT
FROM EMP E
JOIN (SELECT EMP_ID, sum(AMOUNT) as EMP_SPENT FROM expenditure GROUP BY EMP_ID) EXP
ON E.ID = EXP.EMP_ID
WHERE EXP.EMP_SPENT > 100;
Additionally...
I noticed that question is a bit confusing. The original query spits out "every line" of expense of an employee, whose "total" expenses are above 100. The text however, says "Which employee has gone above 100". These are two different questions. My answer above is for the latter "Who has gone above". It will NOT list all expenses of employees who had spent over 100, but only a list of employees who have gone above 100 and their total expenditure.
You can use a simple aggregation with HAVING clause such as
SELECT e.name, e.id, SUM(amount) AS total
FROM emp e
JOIN expenditure ep
ON e.id = ep.emp_id
GROUP BY e.name, e.id
HAVING SUM(amount) > 100
but it's not logical to have a non-aggregated column along with aggregated ones within the result

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

Is it possible to find COUNT function and then to show only certain values that are higher than 10

for example:
SELECT doctor.name
, doctor.surname
, COUNT(checkup.doctor)
FROM doctor
, checkup
WHERE doctor.id = checkup.doctor
GROUP
BY doctor.name
ORDER
BY checkup.doctor
this gives me list of all doctor that had checkup with patients, but i want to show only doctor with number of checkup more than 10 what to add to my sql
You want to limit your result rows according to an aggregation result. You'd do that in the HAVING clause.
As to your query: What is name? A unique name for every doctor? Otherwise better group by the ID - only then is this query valid according to the SQL standard. Then you are using a join syntax that we stopped using in the 1990s. Please use proper ANSI joins instead.
I prefer aggregating before joining, but that's just personal preference:
select d.name, d.surname, c.checkups
from doctor
join
(
select doctor as doctor_id, count(*) as checkups
from checkup
group by doctor
having count(*) > 10
) c on c.doctor_id = d.id
order by d.id;
You could just as well use
select d.name, d.surname, count(*) as checkups
from doctor d
join checkup c on c.doctor = d.id
group by d.id
having count(*) > 10
order by d.id;
TRY THIS: You can use HAVING clause to filter out the aggregation result and try to avoid outdated joins. Surname doesn't meaning there in GROUP BY so you use MAX to pick in select and remove from GROUP BY or leave as it is:
SELECT doctor.name
, doctor.surname
, COUNT(checkup.doctor)
FROM doctor
INNER JOIN checkup ON doctor.id = checkup.doctor
GROUP BY doctor.name, doctor.surname
HAVING COUNT(checkup.doctor) > 10
Do the fact you are using aggregation function as count If you want only the result with more then 10 COUNT(checkup.doctor)
you could filter the result using having
SELECT doctor.name,doctor.surname,COUNT(checkup.doctor)
FROM doctor
INNER JOIN checkup doctor.id=checkup.doctor
GROUP BY doctor.name
Having COUNT(checkup.doctor) > 10
ORDER BY COUNT(checkup.doctor)
the where clause work on the selecting row so don't know the result of an aggregated result .. for this SQL use the having clause that filter the result of a select
I think this will work for you:
SELECT
doctor.name, doctor.surname, checkup_stat.checkup_count
FROM
(SELECT doctor, COUNT(1) AS checkup_count FROM checkup GROUP BY doctor ORDER BY doctor) AS checkup_stat
RIGHT JOIN
doctor
ON
doctor.id = checkup_stat.doctor
WHERE
checkup_stat.checkup_count > 10;
Firstly, use a subquery to get the checkup count for every doctor.
Secondly, select the records from the subquery result where checkup count > 10.
Then, just join them together.

Sql query to join two table for the final output

Below are the two tables, customer and department. I am struggling very hard to get the output.
I want to write the query which shows only the department name which have maximum number of employees.
Answer should be like this ...
Please help me to write the query.
I would suggest using a sub-query, and then selecting from that query. With the outer select, you order by the number of employees and then limit it to 1. This will give you the top department, but also has the flexibility to be modified to give you a list of the x-number of top departments.
SELECT Dep_Name FROM (
SELECT
d.Dep_Name, COUNT(*) AS `count`
FROM
Departments d
JOIN Employees e ON e.Dep_id = d.Dep_id
GROUP BY
d.Dep_id
) AS q
ORDER BY `count` DESC
LIMIT 1
UPDATE
Per a comment by #Dems, you can actually handle this without a sub-query:
SELECT
d.Dep_Name
FROM
Departments d
JOIN Employees e ON e.Dep_id = d.Dep_id
GROUP BY
d.Dep_id
ORDER BY
COUNT(*) DESC
LIMIT 1
SELECT *
FROM
(
SELECT
d.dep_id,
d.dep_name,
count(c.cus_id) cusCount
FROM
cus c,
dep d
WHERE
c.dep_id = d.dep_id
GROUP BY
d.dep_id,d.dep_name
ORDER BY
cusCount desc)
WHERE
ROWNUM = 1;
I created cus & dep tables in Oracle 10g database and tested my code successfully.
What database are you using, and could you post you code.
There error message shows that your "Order by" clause is wrong.