issue with hierarchical queries in db2 - sql

I have the following tables
Lead
id varchar
employee_id varchar
Employee
id varchar
lead_id varchar
There will be a group of employees assigned to a lead. The Lead table holds the employee id of the lead.
The employee table will have lead_id which will be the id key of the leader.
The table will also contain employees which are not assigned to any lead
I need a query which will display the hierarchical result which will list the leaders and the employees under the leader
leader1 (employee )
employee1
employee 2
Leader 2(employee)
employee 3
employee 4
Any idea how this kind of hierarchical result can be obtained by a db2 query?
Click on the this link to view the table structure

The answer is a join of the two tables like
SELECT l.employee_id as leader_employee_id, e.id as employee_id
FROM LEAD l
INNER JOIN EMPLOYEE e
ON e.lead_id = l.employee_id

Related

INNER JOIN and Count POSTGRESQL

I am learning postgresql and Inner join I have following table.
Employee
Id Name DepartmentId
1 John S. 1
2 Smith P. 1
3 Anil K. 2
Department
Department
Id Name
1 HR
2 Admin
I want to query to return the Department Name and numbers of employee in each department.
SELECT Department.name , COUNT(Employee.id) FROM Department INNER JOIN Employee ON Department.Id = Employee.DepartmentId Group BY Employee.department_id;
I dont know what I did wrong as I am new to database Query.
When involving all rows or major parts of the "many" table, it's typically faster to aggregate first and join later. Certainly the case here, since we are after counts for "each department", and there is no WHERE clause at all.
SELECT d.name, COALESCE(e.ct, 0) AS nr_employees
FROM department d
LEFT JOIN (
SELECT department_id AS id, count(*) AS ct
FROM employee
GROUP BY department_id
) e USING (id);
Also made it a LEFT [OUTER] JOIN, to keep departments without any employees in the result. And COALESCE to report 0 employees instead of NULL in that case.
Related, with more explanation:
Query with LEFT JOIN not returning rows for count of 0
Your original query would work too, after fixing the GROUP BY clause:
SELECT department.name, COUNT(employee.id)
FROM department
INNER JOIN employee ON department.id = employee.department_id
Group BY department.id; --!
That's assuming department.id is the PRIMARY KEY of the table, in which case it covers all columns of that table, including department.name. And you may want LEFT JOIN like above.
Aside: Consider legal, lower-case names exclusively in Postgres. See:
Are PostgreSQL column names case-sensitive?

How to get hive query for one to many relation with in a table

I have a employee hive table with columns name and department. where 1 employee can belongs to multiple departments.
name, department
xxx,finance
xxx,hr
xxx,transport
xxx,sale
yyy,finance
yyy,hr
yyy,transport
zzz,finace
zzz,hr
zzz,transport
zzz,sale
I need to know distinct employee name who does not belongs to "sale" department.
As of hive 0.13
Select name from employee
where employee.name not in
(select name from employee where department = 'sale')
group by name;
Hopefully names are unique across employees.
You could write a subquery to pull all names that are in sales. Then join that query's results back to your table.
select
results.name,
results.department
from
(select e.name
from employee e
where e.department='sale' group by e.name) invalid_names
right join
(select
e.name,
e.department
from employee e) results
on invalid_names.name = results.name
where invalid_names.name is null;
I'd imagine there is a better way to do this, but this should work :)

Query for a self join for employee table

Suppose I have an employee table. I have Name and Manager columns. Say there are 10 employees of which 2 are managers. So Name will have 10 names and Manager name would be in Manager column.
How to use self join? I am just learning self join
To perform a self join, you simply give the same table a different alias.
For example, in your employee table you would have a managerid - which stores the id of the manager.
Then to get the manager's name - you just self join to the employee table on managerid - using a different alias (I have used m in the example below):
For example, your table would look like this:
CREATE TABLE Employees (id INT, Name VARCHAR(20), ManagerId INT);
To get the Employee's Name and his/her Manager's Name, you would do something like this:
SELECT
e.Name AS EmployeeName,
ISNULL(m.Name, 'No Manager') AS ManagerName
FROM employee e
LEFT JOIN employee m on m.id = e.ManagerId
If you want to learn more about self joins - see here

Specified Departments? - SQL

imagine I have two tables, the "departments" table and the "employee" table.
This employee table has a column for "category".
I'd like to make a query for selecting departments that only have a specified type of employees.
Thank you.
You will need to perform a join from your departments and employee table on whatever columns link these two tables together. In the where clause, you will specify what types of employees that you want.
This will return a row for each employee, which might not be what you want. You may use the distinct function on the important columns that you're looking for in the departments table to get the final answer.
select distinct dept_id
from employee
where category = 'cat1'
and dept_id not in (select distinct dept_id
from employee
where dept_id <> 'cat1');
SELECT dept_id
FROM departments
WHERE dept_id NOT IN
(SELECT DISTINCT dept_id
FROM employee
WHERE category_id != #specified_category)
This query assumes there are no departments with no employees, since it will also return those empty departments. If that's a problem, you can add:
AND dept_id IN (SELECT distinct dept_id FROM employee)
Select d.id_department from departments d where not exists
(Select e.id_employee from employees e where e.category!=your_category and e.id_department=d.id_department) you also need to verify that department has employees.

Deriving a column's data from a matching column in SQL

So I have a table that has, employee number, employee name, supervisor number.
I want to run a query that will retrieve employee name, employee number, supervisor name and supervisor number. Only one employee doesn't have a supervisor meaning it will have to display nulls. How would I do this? I'm using Oracle SQL Plus. My attempts haven't worked at all! Any help would be much appreciated.
SELECT ename Employee, empno Emp#, super Manager#
FROM emp;
That gets me three of the columns but to be honest I don't even know where to start to get the supervisors names.
It's for university, but I'm studying for a test it's not for an assignment so no cheating happening here :).
The following should work, and give you nulls if the employee has no supervisor:
SELECT empGrunt.ename Employee
, empGrunt.empno EmpNum
, empSuper.ename SupervisorName
, empSuper.empno SupervisorName
FROM emp empGrunt LEFT OUTER JOIN emp empSuper
ON empGrunt.super = empSuper.empno
Assuming that SupervisorNumber is a foreign key relationship back to the Employee table (where it's the EmployeeNumber of the supervisor's record), then you need to use an outer join.
What you need in this case is a left join:
select
e.EmployeeName,
e.EmployeeNumber,
s.EmployeeName as SupervisorName
from Employee e
left join Employee s on s.EmployeeNumber = e.SupervisorNumber