How to find departments which has no employees without using nested query? - mysql

I have two tables:
employee(e_id,e_name,d_id),department(d_id,d_name)
my query is:
select d.d_name from department d where d.d_id not in (
select distinct e.d_id from employee e);
is there any alternative way to do this?.

Try this.
select department.* from department
left join employee
on employee.d_id = department.d_id
where employee.d_id is null

Related

Get the each count of data by combining two tables in MySQL

I want to get the each count of data by combining two tables in MySQL. This is the scenario I have following tables. emp_tab(name, dept_id ) and dept_tab(dept_id, dept_name). I want to write a query to show the number of employees in each department with the department name.
tried code:
SELECT dept_tab.dept_name, number
FROM emp_tab
INNER JOIN dept_tab ON emp_tab.dept_id=dept_tab.dept_id;
My try is not successful. Can you please show me how can I solve this. I am beginner to MySQL
Two things:
You need to use a group by and count function
Your join was joining an invalid table
SELECT dept_tab.dept_name, COUNT(*) as number
FROM emp_tab
INNER JOIN dept_tab ON emp_tab.dept_id=dept_tab.dept_id
GROUP BY dept_tab.dept_name
You can use JOIN and GROUP BY by dept_name to count number of employees.
In your question, what is Customerstable? I assume that is dept_tab?
SELECT
d.dept_name,
COUNT(d.id) AS cnt
FROM
dept_tab d
LEFT JOIN empt_tab e
ON e.dept_id = d.dept_id
GROUP BY d.dept_name ;

Get all records in Table B that don't exist in Table A

Got this question from a job interview via skype. I managed to take a screenshot of the question.
I imagine it's the reverse of a RIGHT JOIN, but I can't figure it out.
One simple way of doing this via an anti-join:
SELECT d.*
FROM departments d
LEFT JOIN students s
ON d.DEPARTMENT_ID = s.DEPARTMENT_ID
WHERE
s.DEPARTMENT_ID IS NULL;
We can also use an EXISTS query:
SELECT d.*
FROM departments d
WHERE NOT EXISTS (SELECT 1 FROM students s WHERE s.DEPARTMENT_ID = d.DEPARTMENT_ID);
Try this:
SELECT *
FROM department_table D
WHERE
NOT EXISTS (SELECT NULL
FROM student_table B
WHERE A.department_id=B.department_id);
You can do this by a using a subquery. Here are two example of MySQL query. You can try this.
SELECT *
FROM DEPARTMENT
WHERE
DEPERTMENT_ID NOT IN (
SELECT DEPERTMENT_ID
FROM STUDENTS
GROUP BY DEPERTMENT_ID)
Another way using where not exists:
SELECT d.*
FROM departments d
WHERE NOT EXISTS (
SELECT 1 FROM students s WHERE s.DEPARTMENT_ID = d.DEPARTMENT_ID
);

Displaying department_name in outer query - sql

i have this query
select last_name,job_id,department_id
from empl_demo
where (department_id) in
(select department_id
from departments
where department_id=60);
my question is how can i display the "department_name" from the departments table? for example :
select last_name,job_id,department_id,department_name
from empl_demo, departments;
but have it match my condition
thanks
Classic INNER Join, you really need to read some basics SQL courses (https://www.w3schools.com/sql/) for example.
SELECT e_d.last_name,
e_d.job_id,
e_d.department_id,
d.department_name
FROM empl_demo e_d
INNER JOIN departments d on d.department_id = e_d.department_id
where department_id=60
It seems department_id is a common attribute to both tables. So try using JOINS like:
select A.last_name, A.job_id, B.department_id, B.department_name
from empl_demo A join departments B
on A.department_id=B.department_id
where B.department_id=60;
If you want to use subqueries then try:
select A.last_name, A.job_id, B.department_id, B.department_name
from (select * from empl_demo where department_id=60) A join
(select * from departments where department_id=60) B
on A.department_id=B.department_id;

SQL statement with JOIN and WHERE clause

I'm new to SQL and trying to solve this problem with two tables Employee and Department. I want display the names of all the employees of 'HR' and 'Sales' departments.
Tables are Employee (emp_id, emp_name, dept_id) and Department (dept_id, dept_name).
Thanks!
Try this:
Select e.emp_name from
Employee e inner join Department d
on e.dept_id = d.dept_id
Where d.dept_name in ('HR','Sales');
This query will compare the dept_id of Employee table and Department table. Those values matched will be returned. then out of all the fields you will select the emp_name and limit the employees belonging to HR and Sales department using the where clause.
As you only want to display employee data, only select from that table. The rest is criteria. You want their department to be either 'HR' or 'Sales', so the straight-forward way of writing this is the IN clause (you could also use the EXISTS clause):
select emp_name
from employee
where dept_id in
(
select dept_id
from department
where dept_name in ('HR', 'Sales')
);
I think this is more easy to read than to join the tables first, only to use one as a filter for the other.
select Employee.emp_name [Emplyee Name] from Employee inner join Department on Department.dept_id=Emplyee.emp_id where Department.dept_name='HR'
You can get like this,
SELECT [emp_name] FROM [dbo].[Employee] AS E
INNER JOIN [dbo].[Department] AS D
ON D.dept_id=E.dept_id
WHERE D.dept_name='HR' OR D.dept_name='Sales'

Joining tables using foreign key

I have 2 tables , employees and departments.
departments(id, department)
employees(id, department_id, name, and a bunch more here)
so employees.department_id is a foreign key to departments.id.
I need to show the table employees, but instead of department_id (showing the IDs of the departments) I need to show the actual departments name, so in place of department_id, i need to place departments.department.
How should I do this?
Your friend told you the truth :p
You just have to use a inner join between your two tables like this:
SELECT d.name, e.name, e.email, ... FROM deparments d INNER JOIN employees e ON d.id = e.department_id.
You have to adapt your field to have the desired output :)
SELECT employees.id, employees.department_id, employees.name, departments.department
FROM employees
INNER JOIN departments ON employees.department_id = departments.id
You should not use SELECT * and just take the fields you really want if it's only to take a screenshot of the table values.
Like SELECT department.name
This should cover it for you:
SELECT
E.Id
, D.Department
, E.Name
-- More stuff
FROM Employees E
INNER JOIN Departments D
ON D.id = E.department_id
SELECT employees.*, department.department
FROM employees
INNER JOIN department ON employees.department_id = department.id