select employee_name, count(dependent) as dependents from employee error - mysql

i have this two tables "employee table, dependent table"
the employee table has "ID, employee_id, employee_name"
the dependent table has "ID, employee_id, dependent_name, relationship"
and the value of employee table is ("1, 123, Vincent")
and the value of dependent table is
("1, 123, Angel, daughter"),("2, 123, Mary, daughter")
how will i display an employee_id and employee_name and it's number of dependents?
like select employee_id, employee_name , count(dependent) as dependents ???

Try this query:
select e.employee_id,e.employee_name,count(d.employee_id) as dependents
from department d
inner join employee e on (e.employee_id=d.employee_id)
group by d.employee_id;
Sql Fiddle Example

select e.employee_id, e.employee_name , (select count(*) from
dependent_table where employee_id=e.employee_id) as dependents
from employee_table as e

select
e.employee_id,
e.employee_name,
count(select * from dependent
table where
dependent table.employee_id=employee table.employee_id)
from
employee table e
where e.employee_id=123
try this

try this its worked
select a.empid ,a.empname , count(b.deptname) from employee a , dependent b
where a.empid = b.empid and b.empid=123;

Use Group By and Count
select
em.Employee_id,
count(*)
from
employee em inner join
dependant dp on
em.employee_id = dp.employee_id
group by
em.employee_id

Related

SQL to get details based on other table

I want to get list of employees from table 'employee' based on their join_date which is in another table 'employee_details'.
For example: get list of employees names whose join_date is > 16-Feb-2020
Select *
from employee emp
inner join employee_details dtls on emp.id=dtls.employee_id
where join_date > '2020-02-16'
SELECT *
FROM Employee E
INNER JOIN Employee_Details D ON E.ID = D.Employee_ID
WHERE Join_Date > '16-Feb-2020'

SQL query to find names of employees belonging to particular department

I have created two tables one employee table and the other is department table .Employee table has fields EmpId , Empname , DeptID , sal , Editedby and editedon where
EmpId is the primary key and Dept table has DeptID and deptname where DeptID is the secondary key.
I want the SQL query to show names of employees belonging to software departmant
The entries in dept table are as below :
DeptID Deptname
1 Software
2 Accounts
3 Administration
4 Marine
Use INNER JOIN:
SELECT
E.empname
FROM Employee E
INNER JOIN department D ON E.DeptID=D.DeptID
WHERE D.DeptID = '1'
Is this what you need?
SELECT EmpName FROM Employee WHERE DeptID = 1
Try this:
SELECT
E.empname
FROM Employee E
INNER JOIN department D ON E.DeptID=D.DeptID
where D.Deptname = 'Software'

SQL count with another table in SELECT clause

In my database, have 2 different fields
i.) Employee
ii.) Department
In my employee table,
NAME Department
---------------------
John IT
Siti Research
Jason Research
In my Department,
Name
------------
IT
Research
Computer
Using statement
SELECT DEPARTMENT.DNAME
FROM DEPARTMENT,
EMPLOYEE
WHERE DEPARTMENT.DNAME = EMPLOYEE.DNAME
AND
(SELECT COUNT(*)
FROM EMPLOYEE.DNAME)=0;
when no employee in the department then will display
Name
--------------
Computer
Keep trying but having some error on it
Two alternatives:
Using IN:
SELECT name FROM Department
WHERE name NOT IN (SELECT DISTINCT Department
FROM Employee)
Using Left Join:
SELECT D.NAME
FROM DEPARTMENT D LEFT JOIN EMPLOYEE ON D.NAME = EMPLOYEE.Department
WHERE EMPLOYEE.Department IS NULL
An example in Fiddle.
This method will show higher performance than the other if you have thousands of records in your table.
Try NOT IN. Sub query need to be DISTINCT to avoid performance issue in future:
SELECT name FROM Department
WHERE name NOT IN ( SELECT DISTINCT Department FROM Employee);
Or NOT EXIST, faster in most cases:
SELECT name FROM Department
WHERE NOT EXIST ( SELECT 1 FROM Employee
WHERE Employee.Department = Department.name);
You don't state your error but you can select all departments that don't appear in the employee table:
SELECT DEPARTMENT.DNAME
FROM DEPARTMENT
WHERE DEPARTMENT.DNAME NOT IN (SELECT DEPARTMENT FROM EMPLOYEE);
SELECT D.Name
FROM Employee E
RIGHT JOIN Department D
ON E.Department=D.Name
WHERE E.Department IS NULL
SQL Fiddle DEMO

MYSQL selecting across multiple tables + using mathematical operators Average/Count

I have two tables with the following attributes:
Table: Department
dept_nbr
dept_name
dept_phone
dept_building
dept_mgr
Table: Employee
emp_nbr
emp_lname
emp_fname
emp_phone
emp_dateofbirth
emp_date_hired
emp_nbr_of_dependents
emp_dept
dept_nbr = emp_dept
I need:
for each dept
1.) total no. of employee dependents
2.) average no. of dependents. - i am guessing AVG
3.) total no. of employees - i am guessing count(*)
Could someone please help me counter this?following is my code
select DEPT_NAME, AVG(EMP_NBR_OF_DEPENDENTs), count(emp_fname) as Total_No_of_Employees from dept,employee where DEPT_NBR = EMP_DEPT group by DEPT_NAME;
second part:
include those departments that have fewer than 50 employees
select DEPT_NAME, AVG(EMP_NBR_OF_DEPENDENTs), count(emp_fname) as Total_No_of_Employees from dept,employee where DEPT_NBR in (select EMP_DEPT from employee where count(emp_fname)<50) group by DEPT_NAME ;
i tried the above and got an error 1111
Thanks Heaps
SELECT dept_name,SUM(emp_nbr_of_dependents),AVG(emp_nbr_of_dependents),COUNT(emp_nbr)
FROM deptartment JOIN employee ON dept_nbr=emp_dept
GROUP BY dept_name
HAVING COUNT(emp_nbr)<50
More aggregation functions can be combined in one select.
select DEPT_NAME,
sum(EMP_NBR_OF_DEPENDENTs) employee_dependents_sum,
AVG(EMP_NBR_OF_DEPENDENTs) avg_nbr_dependents,
count(emp_nbr) employee_count
from dept,employee
where DEPT_NBR = EMP_DEPT
group by DEPT_NAME
having employee_count < 50;
WHERE part is applied before GROUP BY and then HAVING is applied on result.
http://dev.mysql.com/doc/refman/5.5/en/select.html

I need help with a MySQL query

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.