SQL to get details based on other table - mysql

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'

Related

How to get all columns from one table and only one column from another table with ID ? - MySql

I have table employee which contains about 9 columns . ' id,name,etc..'
and I have another table 'onCall' contain 3 columns 'employee_id,department_id and rank '
what I want is to retrieve the employee data who is registered as OnCall employee on this department
I try this to get the employee data :
Select * from employee where id in (SELECT employee_id FROM onCall where department_id = 3)
But like this I can't know what is the rank of the onCall employee , is he registered as primary or backup ,how can I merge rank column from onCall table but only for the selected employee by the id
I tried to join them but I get syntax error
any way to solve this ?
This calls for an inner join
select EMP.*, OC.*
from EMPLOYEE EMP
inner join ONCALL OC
on OC.EMPLOYEE_ID = EMP.ID
where OC.DEPARTMENT = 3
This may help you and use the required columns with table alias name
Select e.*, o.* from employee e
LEFT JOIN onCall o ON o.employee_id = e.id
AND o.department_id = 3

Sql query get from each category

In mySql I have 3 table name emp, emp_cat and emp_cat_relation. Employee Id from emp table and employee category id from emp_cat relate in emp_cat_relation table.
How I can pick last joined employee (1 or 2 row as I want) name and join date from each category using single sql query.
Date field in emp table
Use the LIMIT & ORDER BY Functions
SELECT *
FROM emp e
INNER JOIN emp_cat_relation ecr ON e.employee_id = ecr.employee_id
INNER JOIN emp_cat ec ON ecr.employee_category_id = ec..employee_category_id
ORDER BY e.date
LIMIT 2;

Query to select: who two employee works for the same company

Suppose that we have following tables:
company company_has_employee employee
-------------------------------------------------------------
id company_id id
companzy_name employee_id emplyee_name
How to create SQL Query, which retrieves any two employees, who works for the same company and what is this company?
Assuming juergen d's joins of the table are correct, I will modify the query to
select top 2 company_name, e.employee_name
from compyny c
join company_has_employee ce on ce.company_id = c.id
join employee e on e.id = cs.employee_id
group by company_name
having count(e.id) > 1
This will always return the top 2 employees
juergen d's original query will always return the first and last employees based on their ID.
If you want two employees chosen randomly, then you can try this:
select top 2 company_name, e.employee_name
from compyny c
join company_has_employee ce on ce.company_id = c.id
join employee e on e.id = cs.employee_id
group by company_name
having count(e.id) > 1
order by RAND((e.ID)*DATEPART(millisecond, GETDATE()))
The last order by clause will change the order of records randomly and you will always get the top 2 of a random order...which means 2 random employees will be selected each time the query is run.
select company_name,
min(e.employee_name) as emp1,
max(e.employee_name) as emp2
from compyny c
join company_has_employee ce on ce.company_id = c.id
join employee e on e.id = cs.employee_id
group by company_name
having count(e.id) > 1

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

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

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.