How to join my tables? - sql

I'm using a Sqlite3 database and I have 3 tables which I want to join. My program basically has documents. And you can select multiple employees that belong to the document. But employees can also belong to other documents.
Below are my tables:
Table Employee:
id:Integer primary key
Table Document:
id:Integer primary key
Table DocEmp:
id:Integer primary key
docId:Integer
empId:Integer
state:Integer
The DocEmp table can be empty only the Employee and Document table is always filled. Most important is that i always want to load all employees!
So for example:
I have 3 employees with id's: 1,2 and 3. Have 2 documents with id 1 and 2.
The DocEmp has the following records:
Row1: 1,1,1,0
Row2: 2,1,2,1
So this means only document 1 has 2 employees with id 1 and 2. So when i'm in document 1 i want to load all 3 employees, and also want to know the state of the 2 employees that are filled in for that document in the DocEmp table. And because document 2 has no employees i need just all the employees.

If I understand your question correctly, I think you might be looking for a left join:
select e.id as employeeid,
d.id as documentid,
de.state as state
from employee e
--this could be an inner join but your example is confusing
left join document d on e.id = d.id
left join docemp de on e.id = de.empid
and d.id = de.docid;

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 inner join of array type in postgres?

employees table with columns:
id (pk) BIGINT, name TEXT, email TEXT, work_locations BIGINT[].
work_locations columns contains the location table ids.
location table with columns:
id (pk) BIGINT, lat DECIMAL(12,9), long DECIMAL(12,9).
I want a result like
id name email lat, long
1 name email#email.com 23.345 54.3678
I am not able to join two table on work_locations and ids.
How to join these two tables?
You can join using the ANY operator:
select e.*,
l.lat,
l.long
from employees e
join locations l on l.id = any(e.work_locations)
order by e.id;
In general I would recommend to not store foreign keys in arrays like that. You should think about a properly normalized model with a many-to-many link table between employees and locations
You can unnest():
select e.*, l.*
from employees e cross join lateral
unnest(work_locations) wl(work_location) left join
locations l
on wl.work_location = l.id;

How to get the unmatched records from two tables using Joins

I have two tables one is teacher and another is Department which is mentioned below.
Teacher Table
Id Name
1 xyz
2. Gjd
3. Dftr
4 dhdk
Department Table
Id Name EMPID
1 SQL. 2
2. PHP. 4
3. JAVA. 1
4 PEARL. 5
QUESTION
i want those records of teacher which are not link with any Department.
you can use following statement using left join then filter Teacher that not matched
SELECT t.*
FROM Teacher t
left join Department d on d.EMPID = t.Id
where d.id is null
SELECT * FROM teachers WHERE
id NOT IN (SELECT DISTINCT EMPID FROM departments) ;
Hope this helps.!!
you can do it by inner query..
select * from teacher where id not in (select empid from department);

issue with hierarchical queries in db2

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

Linq to SQL Left Outer Join Not

I have two Tables "Employees" and "EmployeesCompanies", Employees contains a list of all employees, and employeescompanies contains a list of all companies associated with an employee:
Table 1 (Employees)
EmployeeID
1
2
3
Table 2 (EmployeesCompanies)
EmployeeID
1
2
I want to return
3 which is the missing record from EmployeesCompanies, here is the linq code I'm using:
var queryOrphanedEmployees = (from a in db.Employees
join b in db.EmployeesCompanies
on a.EmployeeID equals b.EmployeeID
into outer
from c in outer.DefaultIfEmpty()
select new { a.EmployeeID}).ToList();
However this returns:
1
2
Which is exactly opposite to what I want.
You should be able to do something like this if you've set up the foreign keys properly
from e in Employees
where !e.EmployeesCompanies.Any()
select e