SQL Print departments that have employees - sql

I have two tables departments and employees.
The department table has as primary key department_id. The employees table has as foreign key department_id. I want to print all departments that have employees.
I've tried using a syntax as, but it's not good.
SELECT
departments.department_id, employees.department_id
FROM
departments, employees
WHERE
departments.department_id <> employees.department_id;

"I want to print all departments that have employees"
-- I want to print all departments
SELECT *
FROM departments d
WHERE EXISTS (
-- that have employees
SELECT *
FROM employees e
WHERE e.department_id = d.department_id
);

Simple solution:
select * from departments
where department_id in (select department_id from employees)

You need all the departments referenced in the Employees table.
So you need to go over every employee record whose department_id is not null. Then you have a department that has an employee related.
SELECT
d.department_id,
d.department_name
FROM
departments d
JOIN employees e ON e.department_id = d.department_id
WHERE e.department_id IS NOT NULL
GROUP BY d.department_id

SELECT
DISTINCT d.department_id
FROM
departments d
JOIN employees e ON e.department_id = d.department_id

Related

create a query to get employees last_NAME ,departemtn number and all employees that are working in the same department?

here is a question wher ei have to show department number and employess lastname with collagues of employees that are working in the same department
use employeeManagement_db
SELECT e.department_id as department_Number,last_name , COUNT(last_name)as Collagues from employees e
LEFT JOIN departments a
ON e.department_id=a.department_id
GROUP BY last_name,e.department_id
having e.department_id > 1

Is it possible to retrieve number of departments and the number of employees working in that department if given the location Id

Is it possible to retrieve the number of departments and the number of employees working in that department if given the location Id? Location_id column is in the departments table. Employees and Department share department_id column
Cannot get the correct result with this query:
select
count(E.employee_id), count(D.DEPARTMENT_ID)
from
employees e
join
departments d on (e.department_id = d.department_id)
where
D.LOCATION_ID = 1700;
I suspect you need distinct when counting the departments:
SELECT
COUNT( E.employee_id )
, COUNT(DISTINCT d.department_id )
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.location_id = 1700;
or, you need to group by department:
SELECT
COUNT( E.employee_id )
, d.department_id
FROM employees e
JOIN departments d ON e.department_id = d.department_id
WHERE d.location_id = 1700
GROUP BY d.department_id;

SQL-HR Schema, Retrieving the Dept.Names,managers and employees per dept

Hello guys and thank you in advance for your time and help.
So I am trying to get a list of the Department names their manager name and the total number of employees per department.
My code so far looks like this:
select d.department_name,e.first_name,e.last_name
from employees e, departments d
where e.department_id = d.department_id and d.manager_id=e.employee_id
group by d.department_name,e.first_name,e.last_name
order by d.department_name;
which produces the list of the manager per department,but I am still short of the count of employees per department. Any ideas?
You need to use the COUNT function. Try this:
select d.department_name,e.first_name,e.last_name,count(e.employee_id) as `TotalNoOfEmployees`
from employees e JOIN departments d
ON e.department_id = d.department_id and d.manager_id=e.employee_id
group by d.department_name,e.first_name,e.last_name
order by d.department_name;
Also try not to use the old way of Joining the tables ie, comma separated JOINS.
After a lot of experimentation I got it. Posting it in case somebody might find it useful someday:
select distinct d.department_name,
(select e.first_name||', '||e.last_name from employees e
where d.department_id=e.department_id and
d.manager_id=e.employee_id)as "manager_name",
( select count( employee_id ) from employees e
where d.department_id=e.department_id ) as "total_no_of_employees"
from employees e
join departments d on d.department_id=e.department_id
order by d.department_name;
Try this:
select emp.manager_id, mgr.first_name, mgr.last_name, dept.department_name, count(emp.employee_id)
from hr.employees emp
join hr.employees mgr
on emp.manager_id = mgr.employee_id
join hr.departments dept
on mgr.department_id = dept.department_id
group by emp.manager_id, mgr.first_name, mgr.last_name, dept.department_name
order by department_name

Order by subquery

I have the following oracle SQL code, but I can't understand what is the purpose of ordering by a subquery. Anyone can explain it clearly to me ?
SELECT employee_id, last_name
FROM employees e
ORDER BY (
SELECT department_name
FROM departments d
WHERE e.department_id = d.department_id
);
The ordering is done by results from other table. In this case the query returns only results from employees table, but the ordering is done by department_name, which is stored in departments table.
You could achieve identical result by using join, selecting only values from employees table, and ordering by department_name from departments table:
SELECT e.employee_id, e.last_name
FROM employees e INNER JOIN departments d
ON e.department_id = d.department_id
ORDER BY d.department_name
This query is valid if employee must always have a department. If there can be employees without departments then you should use LEFT join instead.
The clear intention of that query is employee_id and last_name from employees should be order by department_name from departments.
Okay, you don't subquery then go for join
select e.employee_id,e.last_name from employees e join departments d on
e.department_id = d.department_id order by d.department_name;

How do I structure this SQL SELECT query?

I have two tables and I need to make a certain select.
First table is Employe that has colums Name *Departament_ID* and Salary
The second table is Departament that has colums Departament ID and *Departament_Name*
My SQL script has to get me the name of the departament, the maximum salay and minimum salary where Departament_ID is '30'
Something like this should do the trick (note: all spellings are exactly as you gave in your first post).
SELECT d.Department_Name AS name,
MAX(e.salary) AS max_salary,
MIN(e.salary) AS min_salary
FROM Department d
LEFT JOIN Employe e ON d.Department_ID = e.Department_ID
WHERE d.Department_ID = 30
SELECT d.department_name,
MIN(e.salary) AS 'Minimum Salary',
MAX(e.salary) AS 'Maximum Salary'
FROM department d,employee e
WHERE d.department_id=30 AND d.department_id=e.department_id
GROUP BY d.department_name
SELECT max(sal) as MaximumSalary
,min(sal) as MinimumSalary
,department.Departament_Name
,employee.Name
FROM employee
INNER JOIN department ON
employee.departmentid = department.department_id
WHERE department.department_id = 31
GROUP BY department.department_id
max and min function will give you the maximum and minimum value of the employee table for the salary column. This will require you to use GROUP BY because they need to calculate the maximum and minimum from a group of data witch in this case is the maximum/minimum by department. We group by department id because it's a unique key of the table. The inner join is here to join the two tables you have. It's an inner join because we want to have the department into the query.
SELECT d.Department_Name AS name,
MAX(e.salary) AS max_salary,
MIN(e.salary) AS min_salary
FROM Department d
Inner JOIN Employee ON d.Department_ID = e.Department_ID
WHERE d.Department_ID = 30