tried to display the name of the department that has the least student count.
Try something like this:
SELECT TOP 1 department_name, count(*) AS number_of_students
FROM department natural join student
GROUP BY department_name
ORDER BY number_of_students
It will give you the list of departments ordered by the number of students. By selecting only the first row via TOP 1 you will only get the department with the least number of students.
Is it like this way?
SELECT
T1.department_name, COUNT(T2.department_id) totalCount
FROM department T1
LEFT JOIN student T2
ON T1.department_id = T2.department_id
GROUP BY T2.department_id
HAVING COUNT(T2.department_id) =
(
SELECT
COUNT(T2.department_id) totalCount
FROM department T1
LEFT JOIN student T2
ON T1.department_id = T2.department_id
GROUP BY T2.department_id
ORDER BY totalCount ASC
LIMIT 1
)
SQLFIDDLE DEMO
Related
Lets say I have this tables:
country:
country_id,
country_name;
currency:
currency_id,
currency_name;
country_currency:
country_id,
currency_id;
I would like to select and show all countries having two or more currencies. How should my query look like?
Join all the tables with the common columns, then group the result by country_id, and count the number of rows (or currency) per group as shown below.
SELECT A.COUNTRY_ID, COUNT(*) NUMBER_OF_CURRENCY
FROM COUNTRY A
INNER JOIN COUNTRY_CURRENCY B ON A.COUNTRY_ID = B.COUNTRY_ID
INNER JOIN CURRENCY C ON B.CURRENCY_ID=C.CURRENCY_ID
GROUP BY A.COUNTRY_ID
HAVING NUMBER_OF_CURRENCY > 1 ;
A better query would be:
SELECT COUNTRY_NAME
FROM COUNTRY
WHERE COUNTRY_ID IN (
SELECT COUNTRY_ID
FROM COUNTRY_CURRENCY
GROUP BY COUNTRY_ID
HAVING COUNT(*) > 1
);
I have two tables as such:
student department
id department_id department_id department_name
5 5 5 Computer Science
1 4 4 Architecture
3 2 1 Mathematics
4 5 3 Chemistry
2 4 2 Physics
I wrote a query as follow and got the following results.
SELECT DEPARTMENTS.DEPT_NAME AS D, STUDENTS.DEPT_ID AS D_ID
FROM STUDENTS
INNER JOIN DEPARTMENTS
ON STUDENTS.DEPT_ID=DEPARTMENTS.DEPT_ID ;
Computer Science 5
Computer Science 5
Physics 2
Architecture 4
Architecture 4
It's fine till here but I want something like
Computer Science 2
Architecture 2
Physics 1
Chemistry 0
Mathematics 0
i.e department name , num_of students where num_of students are in decending order.
What can I add to the query?
I would use COUNT(*) and subquery it for the ORDER BY
SELECT * FROM (
SELECT DEPARTMENTS.DEPT_NAME, COUNT(*) AS num_ofstudents
FROM STUDENTS
LEFT JOIN DEPARTMENTS
ON STUDENTS.DEPT_ID=DEPARTMENTS.DEPT_ID
GROUP BY Departments.Dept_name
) AS a ORDER BY num_ofstudents
edit- Thanks AaronDietz for pointing this out!
You should replace the INNER JOIN with a LEFT JOIN so that the query includes the records from [Departments] that do not have any students. Also, I did not need to include the subquery.
SELECT DEPARTMENTS.DEPT_NAME, COUNT(*) AS num_ofstudents
FROM STUDENTS
LEFT JOIN DEPARTMENTS
ON STUDENTS.DEPT_ID=DEPARTMENTS.DEPT_ID
GROUP BY Departments.Dept_name
ORDER BY num_ofstudents
You can try grouping the departments and the id, then count.
SELECT DEPARTMENTS.DEPT_NAME AS D, COUNT(*) as NID
FROM STUDENTS
INNER JOIN DEPARTMENTS
ON STUDENTS.DEPT_ID=DEPARTMENTS.DEPT_ID
GROUP BY DEPARTMENTS.DEPT_NAME
ORDER BY NID DESC
I think the simplest approach is to select the departments and get the count in a subquery:
select
department_id,
department_name,
(select count(*) from student s where s.department_id = d.department_id) as student_count
from department d
order by 3 desc;
This works well, because you just want one value from the students, namely the count. If you wanted more information then you'd move the subquery to the from clause. E.g.:
select
d.department_id,
d.department_name,
colalesce(s.students, 0) as student_count,
s.ids as student_ids
from department d
left join
(
select
department_id,
count(*) as students,
group_concat(id) as ids
from student
group by department_id
) s on s.department_id = d.department_id
order by 3 desc;
SELECT DEPARTMENTS.DEPT_NAME AS D, count(STUDENTS.DEPT_ID) AS D_ID
FROM STUDENTS
INNER JOIN DEPARTMENTS
ON STUDENTS.DEPT_ID=DEPARTMENTS.DEPT_ID
GROUP BY D
ORDER BY D_ID DESC;
Grouping by department name
Other queries are mostly right but COUNT should be on student id and query should start from department instead of student.
SELECT DEPARTMENTS.DEPT_NAME,
COUNT(id) AS num_ofstudents
FROM DEPARTMENT
LEFT JOIN students ON STUDENTS.DEPT_ID = DEPARTMENTS.DEPT_ID
GROUP BY DEPARTMENTS.DEPT_NAME
ORDER BY num_ofstudents
I found one question in MySQL I am trying. Please tell me if following solution will work or is there any better solution?
select D.DEPT_NAME, COUNT(*)
from Departments D
left outer join STUDENTS S
on S.Dept_ID = D.Dept_ID
group by D.DEPT_NAME
order by 2 desc, 1
Students table has following fields:
Student_ID
Student_Name
Gender
Dept_ID
Departments table has following fields:
Dept_ID
Dept_Name
A university uses 2 data tables, Students and Departments, to store data
about its students and the departments associated with each major.
Write a query to print the respective department name and number of students
majoring in each department for all departments in the Departments table
(even ones with no current students).
Sort your results by descending number of students; if two or more departments have same number of students, then sort those departments alphabetically by department name.
Forgive me altering the formatting of the code.
I would change the ORDER BY, as follows:
SELECT
d.DEPT_NAME,
COUNT(s.STUDENT_ID)
FROM
Departments d
LEFT JOIN Students s ON d.DEPT_ID = s.DEPT_ID
GROUP by
d.DEPT_ID
ORDER by
COUNT(s.STUDENT_ID) DESC,
d.DEPT_NAME ASC
You need a way to count the students in each department, then you need a way to list all departments, even those without students.
Counting the students in each department: (http://sqlfiddle.com/#!15/39a8b/15/0)
SELECT Dept_ID, COUNT(*) Students
FROM STUDENTS
GROUP BY Dept_ID
Then, treating that as a subquery, left join it to your other table. (http://sqlfiddle.com/#!15/39a8b/16/0)
SELECT D.DEPT_NAME, S.Students
FROM Departments D
LEFT JOIN (
SELECT Dept_ID, COUNT(*) Students
FROM STUDENTS
GROUP BY Dept_ID
) S ON D.Dept_ID = S.Dept_ID
The LEFT JOIN preserves rows in the DEPARTMENTS table that don't match the ON clause. This gets you stuff like this.
Biology 7
Mathematics (NULL)
Sociology 11
Physics 3
So you have to deal with that (NULL) problem. Here's how. Change the SELECT to say
SELECT D.DEPT_NAME, IFNULL(S.Students,0)
It's a little tricky to join a table to an aggregate where the aggregate (the COUNT/GROUP BY query) has missing data. But that's how you do it.
You can figure out the ORDER BY stuff on your own.
SELECT d.department_name, COUNT(s.student_name) AS student_count
FROM student s
LEFT JOIN department d
ON s.department_id = d.department_id
GROUP BY department_name
ORDER BY d.department_name;
!!This is finally the correct answer !!
Don't hardcode the problem please stay tuned and work like professional
Excute below.
SELECT
ad.Dept_Name,
count(ass.Student_Id) as Stduent_Enrolled
FROM [Alok.Departments] ad
Left Outer Join [Alok.Students] ass
ON ad.Dept_ID = ass.Dept_ID
Group by ad.Dept_Name
ORDER by
CASE WHEN COUNT(ad.Dept_ID) >=2
THEN ad.DEPT_NAME END desc,
CASE WHEN COUNT(ad.Dept_ID) < 2
THEN ad.DEPT_NAME END asc
1 select department_name, count(student_id) as student_count
2 from student
3 left outer join department ON
4 department.department_id=student.department_id
5 group by department_name
6 order by department_name;
#jaat
Use this query
select count(*) from tblstud_info s,tbldept d where s.dno=d.dno group by d.dname
I have a table similar to the following:
ID PAYEE CATEGORY
001 Costco Grocery
002 See's Candy
003 Costco Repair
005 Costco Grocery
006 Costco
007 Costco
008 See's
Using MySQL withOUT the aid of a programming language, is there a query (nested or not) that would set the category of the three new rows to the most often used category for those payees?
For example, one of the Costco records (ID 003) has Repair as its category, whereas the other two Costco rows (ID 001 and ID 005) have Grocery as their category. Thus the desired result would be that the new Costco rows (ID 006 and ID 007) would be set to Grocery since that's the most often used category for that payee.
sure.. just change 'your_table' to the name of your table
UPDATE your_table
LEFT JOIN (SELECT payee, category
FROM
(SELECT payee, category FROM your_table WHERE category != '' AND category IS NOT NULL GROUP BY payee, category ORDER BY count(*) DESC) AS tbl2
GROUP BY payee
) AS tbl2 USING (payee)
SET your_table.category = tbl2.category;
this will change the costco that is categorized as repair to 'grocery' as well.. if you dont want this, add:
WHERE your_table.category IS NULL OR your_table.category = ''
to the very end of the query
This would work
UPDATE test t,
(SELECT category,
payee,
count(*)
FROM test ORDER BY count(*) desc LIMIT 1) t1
SET t.category = t1.category
WHERE t.payee = t1.payee
AND (t.category = ''
OR t.category IS NULL)
Sqlfiddle at http://www.sqlfiddle.com/#!2/ed5b0/1/0
I just couldn't figure out a way without repeating the creation of a derived table:
UPDATE t JOIN (
SELECT s1.payee, s1.category FROM (
SELECT payee, category, count(*) cat_count FROM t
WHERE category IS NOT NULL
GROUP BY payee, category
) s1
LEFT JOIN (
SELECT payee, category, count(*) cat_count FROM t
WHERE category IS NOT NULL
GROUP BY payee, category
) s2
ON s1.payee = s2.payee AND s1.cat_count < s2.cat_count
WHERE s2.cat_count IS NULL
) s
ON t.payee = s.payee
SET t.category = s.category
WHERE t.category IS NULL;
Fiddle here
Use mysql's multi-table update:
UPDATE mytable t
JOIN (SELECT payee, category
FROM (SELECT payee, category
FROM mytable
GROUP BY 1, 2
ORDER BY count(*) desc) x
GROUP BY 1) y
ON y.payee = t.payee
SET t.category = y.category
WHERE ifnull(t.category, '') = ''
There's a little bit of kung fu tucked away in there that makes it work: the outer group by returns the first row encountered for the group, which due to the ordering for the inner-most query will be the category with the highest count.
I have two tables - `employee` and `department`.
1. `employee` table contains column id,employee name and dept_id
2. `department` table contains column id, department name.
I need exact department name which contains
1. maximum employee and
2. no employee
Edited:
Apologizing for bad grammar, here is the example for above two questions what i need.
1. for eg: if two department contains same number of employees, i need to show both department not single by limit.
2. for eg: if more than one department contains 0 employees, i must show those departments particularly.
select department_name as `department name`,
count(*) as `number of employees`
from employee
inner join department
on employee.dept_id = department.id
group by department_name
order by count(*) desc
limit 1
i think that should do it. i've not done anything with mysql in a while.
edit: missed the second question
select department_name as `department name`,
count(*) as `number of employees`
from employee
left join department
on employee.dept_id = department.id
group by department_name
HAVING count(*) = 0
Answer to the first question:
WITH epcount(dept_id, ep_count) AS
(
SELECT dept_id, COUNT(*) AS ep_count
FROM employee
GROUP BY dept_id
)
SELECT d.name FROM epcount AS ec1 JOIN department AS d ON ec1.dept_id=d.id
WHERE NOT EXISTS
(SELECT * FROM epcount AS ec2 WHERE ec1.ep_count < ec2.ep_count)
Answer to the second question:
SELECT name FROM department AS d
WHERE NOT EXISTS
(SELECT * FROM employee AS e WHERE d.id=e.dept_id)
If I read the question right, you need:
select department_name,
count(employee.dept_id) as num_employees
from department
left join employee on employee.dept_id = department.id
group by department_name
having count(employee.dept_id) = 0 or
count(employee.dept_id) = (select count(dept_id)
from employee
group by employee.id
order by count(dept_id) desc
limit 1)
This will get you a sorted list of departments, sorted by number of employees.
SELECT `dept`.`id`, `dept`.`name`, COUNT(`employee`.`id`) as `employee_count`
FROM `dept` LEFT JOIN `employee`
ON `employee`.`dept_id` = `dept`.`id`
GROUP BY `dept`.`id`
ORDER BY `employee_count`
To get departments with no employees, add:
AND `employee_count` = 0
...before the GROUP BY.
To get the department with the most employees, add DESC LIMIT 1 to the end.
Query that shows department names with maximum employees and number of employees in it:
SELECT department.name, COUNT(employee.name) from department
INNER JOIN employee
ON employee.dept_id = department.id
GROUP BY department.name
ORDER BY COUNT(employee.name) DESC limit 1
Query that shows departments with no employees:
SELECT department.name from department
LEFT JOIN employee
ON employee.dept_id = department.id
HAVING COUNT(employee.name) = 0
GROUP BY department.name
If you need to show it in one query, paste first query, add UNION ALL and then paste second query.