I have two tables like employees and doctorsrating.
In employees table contains list of registered employees with columns of emp_bioid and emp_name and doctorspoints emp_bioid, points, pointsname, doctor_name, createdAt of columns.
Employees will add points for doctors which will stored by the emp_bioid. My question is i want to count total records added by the each and every employees.
I have tried this query but the business count was wrong.
SELECT emp_bioid, COUNT(*) as Doctor (SELECT emp_name from employees where emp_bioid = doctorsrating.emp_bioid) as emp_name FROM doctorsrating
WHERE createdAt between '2021-03-01' AND '2021-03-24' GROUP BY emp_bioid
You can make a join and group by both columns (id and name) as below:
SELECT e.emp_bioid, e.emp_name, COUNT(*)
FROM employees e
JOIN doctorsrating dr ON e.emp_bioid = dr.emp_bioid
WHERE dr.createdAt between '2021-03-01' AND '2021-03-24' GROUP BY e.emp_bioid, e.emp_name
Related
There are two tables, one with the employees salary and another with the department.
table1:
employee_id name salary
--------------------------------------
01 K.Irving 30000
02 J.Polk 50000
03 S.Smith 20000
04 D.Rai 35000
table2:
employee_id name department
------------------------------------------
01 K.Irving marketing
02 J.Polk finance
03 S.Smith marketing
04 D.Rai it
I would like to find out how to find the average salary per department where it is lower than 40000
Ideally it should look like this:
avg_salary department
-----------------------------
35000 it
25000 marketing
So far I have tried to combine the two tables using the following formula, however I am having trouble putting in the conditions I wanted:
SELECT table1.employee_id
, table1.name
, table1.salary
, table2.department
FROM table1
LEFT
JOIN table2
ON table1.employee_id = table2.employee_id
AND table1.employee_name = table2.employee_name
ORDER
BY salary DESC;
The solution would be a mixture of the above and below, I was not sure how to combine the two.
SELECT AVG(salary), department
FROM table1, table2
GROUP BY department
HAVING AVG(salary) <40000
ORDER BY salary DESC;
I was not sure how to get the averages per department, when I used the code above it took an average of all salaries.
You need to GROUP BY the department and limit the results with a HAVING clause.
SELECT avg(e.salary) avg_salary,
d.department
FROM table2 d
LEFT JOIN table2 e
ON e.employee_id = d.employee_id
GROUP BY d.department
HAVING avg(e.salary) < 4000
ORDER BY e.salary DESC;
But note, that your design is bad. There should be a table for the departments, that only stores the departments, not any employees to it. Then there should be a linking table with just a user and a department ID to store which employees is in which department. There should not be the employee (or the department) name in that table.
A LEFT JOIN is unnecessary for this problem. In order for the average to be well-defined, you need to have matches between the tables. Hence:
SELECT avg(e.salary) as avg_salary,
d.department
FROM table2 d JOIN
table2 e
ON e.employee_id = d.employee_id
GROUP BY d.department
HAVING avg_salary < 4000
ORDER BY e.salary DESC;
As a convenience, you can also use the column alias in the HAVING clause.
select
d.DepartmentId,
d.Name,
d.GroupName,
eph.Rate * 40 * 52 * count(edh.EmployeeId) as AnnualPay
from
HumanResources.Department d,
HumanResources.EmployeePayHistory eph,
HumanResources.Employee e,
HumanResources.EmployeeDepartmentHistory edh
where
e.CurrentFlag = 'True' and
edh.DepartmentID = d.DepartmentID and
edh.EmployeeID = eph.EmployeeID and
e.EmployeeID = eph.EmployeeID
group by
d.departmentID;
The goal is to write a report to display the departmentid, name, groupname, the total annual pay for all employees who work in that department and the number of employees who work in that department.
To calculate the annual pay, multiply each of the employees’ pay rate by 40 hours for 52 weeks in the year.
Only include departments that spend more than half a million dollars on their total annual pay for all employees.
All employees should be current (use currentflag).
Sort the report by the total annual pay of employees in descending order.
The Tables are included, I can't figure out how to group by each department...Database
If I understood what you are looking for, this should work:
SELECT
d.DepartmentId,
d.Name,
d.GroupName,
SUM(eph.Rate * 40 * 52) as AnnualPay,
Count(edh.EmployeeId) as EmployeeCount
from HumanResources.Department d
JOIN HumanResources.EmployeeDepartmentHistory edh
ON edh.DepartmentID = d.DepartmentID
JOIN HumanResources.EmployeePayHistory eph
ON edh.EmployeeID = eph.EmployeeID
JOIN HumanResources.Employee e
ON e.EmployeeID = eph.EmployeeID
WHERE e.CurrentFlag = 'True'
GROUP BY d.departmentID,d.Name,d.GroupName
HAVING AnnualPay > 500000
ORDER BY AnnualPay DESC;
Please try the following...
SELECT DepartmentId AS DepartmentId,
DepartmentName AS DepartmentName,
DepartmentGroup AS DepartmentGroup,
SUM( expectedAnnualPay ) AS TotalExpectedAnnualPay,
COUNT( DepartmentId ) AS EmployeeCount
(
SELECT Department.DepartmentId AS DepartmentId,
Name AS DepartmentName,
GroupName AS DepartmentGroup
Rate *
40 *
52 AS expectedAnnualPay,
EmployeeID AS EmployeeID
FROM HumanResources.Department HRDepartment
JOIN HumanResources.EmployeeDepartmentHistory HREmployeeDepartmentHistory ON HREmployeeDepartmentHistory.DepartmentID = HRDepartment.DepartmentID
JOIN HumanResources.Employee HREmployee ON HREmployee.EmployeeID = HREmployeeDepartmentHistory.EmployeeID
JOIN HumanResources.EmployeeDepartmentHistory HREmployeeDepartmentHistory ON HREmployee.EmployeeID = HREmployeePayHistory.EmployeeID
WHERE HREmployee.CurrentFlag = 'True'
) departmentEmployee
GROUP BY DepartmentId
HAVING TotalExpectedAnnualPay > 500000
ORDER BY TotalExpectedAnnualPay DESC;
You appear to be asking for a list of the total expected annual pay amount for each department where that value is greater than $500,000.00 on the assumption that all current employees within that Department will remain with that Department for one year at the current Rate and that no Employees will be added during that time.
I approached this problem by starting with the following query (which I gave the alias of departmentEmployeeLevel)...
SELECT Department.DepartmentId AS DepartmentId,
Name AS DepartmentName,
GroupName AS DepartmentGroup
Rate *
40 *
52 AS expectedAnnualPay,
EmployeeID AS EmployeeID
FROM HumanResources.Department HRDepartment
JOIN HumanResources.EmployeeDepartmentHistory HREmployeeDepartmentHistory ON HREmployeeDepartmentHistory.DepartmentID = HRDepartment.DepartmentID
JOIN HumanResources.Employee HREmployee ON HREmployee.EmployeeID = HREmployeeDepartmentHistory.EmployeeID
JOIN HumanResources.EmployeeDepartmentHistory HREmployeeDepartmentHistory ON HREmployee.EmployeeID = HREmployeePayHistory.EmployeeID
WHERE HREmployee.CurrentFlag = 'True'
This subquery returns the DepartmentId, Department Name and GroupName of each department along with the EmployeeID of each current Employee and their computed expected annual pay.
The main query uses the results of the subquery to aggregate / group together the results by each Department and to calculate for each Department the total of the expected annual pay and the count of employees. It then uses HAVING to refine the list to only those departments whose expected annual pay cost is greater than $500,000.00 and sorts the remaining records by that field.
If you have any questions or comments, then please feel free to post a Comment accordingly.
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 of employees. Each employee has an employee id. Some employees have a supervisor field that links back to another employee's id. There are 10 employees, two of which are supervisors, each supervising 4 people. I am trying to get a list of all the employees and the number of other employees they supervise. So far I can only seem to get the supervisors and the number they supervise to show. This is my query:
SELECT s.employee_name, COUNT(*)
FROM employee e
join employee s on e.supervisor_id= s.employee_id
group by s.
order by s.employee_name;
I tried changing JOIN to RIGHT JOIN and it will now show me all 10 employees with the two supervisors shown as having 4 people they supervise but it shows all the others having no one to supervise as having 1 instead of 0. I'm sure it's something simple I am missing.
Sample Data:
employee_name, employee_name, supervisor_id,
'10111', 'Sydnee K. Stevens' NULL
'10870', 'Colton C. Rocha', '10111'
'11425', 'Astra V. Sharp','10111'
'12973', 'Melanie X. Rojas','10111'
'14451', 'Bethany Roman','10111'
'14597', 'Lydia Edwards', NULL
'16153', 'Selma Q. Conley', '14597'
'17730', 'Kristen B. Malone', '14597'
'17762', 'Barrett B. Bauer', '14597'
'18628', 'Shana Z. Flowers','14597'
We join your employee table with a select like we would join it with a real table. The select will consist of all supervisor_ids and the number of occurences in the supervisor_id field (records where supervisor_id is null will be ignored).
SELECT e.employee_id, e.employee_name, s.supervising
FROM employee e
LEFT JOIN (SELECT supervisor_id, count(*) as supervising
FROM employee
WHERE supervisor_id is NOT NULL
GROUP BY supervisor_id) AS s
ON(e.employee_id = s.supervisor_id)
Using LEFT or RIGHT JOIN COUNT(*) will always be at least 1. If using LEFT JOIN (RIGHT JOIN is confusing) you just need to count values from a right table column. COUNT(column) will ignore all rows with NULL in that column.
SELECT s.*, COUNT(e.supervisor_id) as num_supervised
FROM employee s
LEFT JOIN employee e on e.supervisor_id = s.employee_id
group by s.employee_id
order by s.employee_name;
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.