Selecting two names based on ID stored in different table - sql

Suppose I have two tables:
Table 1: tasks with columns (id, authorId, assigneeId)
Table 2: employee with columns (id, name)
authorId and assigneeId in Table 1 are referenced to the id column in Table 2.
What is the way to select Task ID, Author Name and Assignee Name (Assignee name defaults to 'null' if no assigneeId is present) from these two tables?
I am not sure how to start, thus making it difficult. Could anyone give a hint on how to start the code?

You can join twice:
select t.id, e1.name as author_name, e2.name as assignee_name
from tasks t
left join employees e1 on e1.id = t.author_id
left join employees e2 on e2.id = t.assignee_id

Related

Join tables and add column only with a specific value

I have four tables (a, b, c, d) in PostgreSQL database:
I can connect these tables with subject.id and project.id unique keys. My query is:
SELECT subject_id, firstname, lastname, national_id, project_title, project_id, note_template
FROM project_subject
JOIN subject ON subject.id = project_subject.subject_id
JOIN project ON project.id = project_subject.project_id
JOIN subject_note ON subject_note.subject_id = project_subject.subject_id
WHERE project_id = xxxx
My problem is that in table d (subject_notes) one subject can have several notes (varchar) plus selectable note_template (int). I would like to have joined table with columns subject_id, firstname, lastname, national_id, project_title, project_id and note_template. When I join four tables I get result with several same subject rows with different notes. I would like to have table where each subject is listed only one time and to the last column value is added only when note_template=16. Rest of the subjects would have null value for note_template.
Change the last join to a LEFT join and add the condition note_template = 16 in the ON clause:
LEFT JOIN subject_note ON subject_note.subject_id = project_subject.subject_id
AND subject_note.note_template = 16

Microsoft SMSS - Joining tables on t2.primary key = t1.foreign_key

I need to join two tables together to create a table with columns for employee id, employee name and their boss' name.
The 'hier' table
The 'employees' table
The query I wrote is almost working, putting an employee name in the right spot, but not the right employee:
SELECT em.emp_id, em.emp_name, em.emp_name AS boss_name
FROM employees em
LEFT JOIN hier h ON (h.boss_id = em.emp_name)
Which outputs:
I need to have each person's boss to have the right name, and in the case of Big Boss, 'N/A'. Like so:
You need a self join with Employee table
SELECT em.emp_id, em.emp_name, e1.emp_name AS boss_name
FROM employees em
LEFT JOIN employees em1 ON em.boss_id = em1.emp_id

Joining same table multiple times

I have 2 tables Person an Department - where each person has multiple departments registered against him.
Person
id|name|dept1|dept2|dept3
1 |Jane|100 |102 |106
Dept
id |Name
100|Accounts
...
102|HR
...
106|Admin
Whats the most elegant sql to display Jane's record as follows:
Jane|Accounts|HR|Admin
Use this. And work on your naming convention to make all column names unique independent of table.
SELECT
Person.id, Person.name, dept1.Name, dept2.Name, dept3.Name
LEFT JOIN Dept dept1 ON dept1.id = Person.dept1
LEFT JOIN Dept dept2 ON dept2.id = Person.dept2
LEFT JOIN Dept dept3 ON dept3.id = Person.dept3
Something like this will let you join the same table multiple times. You just give each join a different alias
SELECT *
FROM Person AS P
INNER JOIN Dept AS D1 ON P.dept1 = D1.id
INNER JOIN Dept AS D2 ON P.dept2 = D2.id
WHERE P.name = 'Jane'
Ideally you would normalise the data in your Person table and have a linking table between Person and Dept e.g. PersonDepartmentLinking (or whatever convention you have for linking table naming conventions), assuming you have any control over the schema and it's possible to add the relationship that way.
You can use STUFF in this case
Select name,
Stuff((Select distinct ', ' + cast(Name as varchar(20))
From #Dept t2
Where t2.Id = t1.Id
FOR XML PATH('')),1,1,'')
From Person t1
The only way that I know is a join for each column.
But if you are in the way of designing the tables, i suggest you to normalize the DB.
If a person need an extra dept column, you need to alter the table to add a new property of person.
For me, 3 entities are needed:
- Person
- Department
- person_department_assignation

How to retrieve records having many to many relationship bet two tables?

I have two tables:
1) employee with columns: e_id, e_name, address;
2) project with columns: p_id, p_name, start_Date, End_Date.
There is a many-to-many relationship between these two tables.
How can I store this relationship and how can I query for retrieving "Employee details and project details on which he is working"?
You need a cross reference table inbetween:
EmployeeProjectXref: e_id, p_id
The combination of e_id and p_id can be the primary key for this Xref table.
Then, if #e_id is a variable holding the selected Employee ID:
SELECT E.e_name, P.p_name
FROM EmployeeProjectXref EPX
JOIN Employee E ON E.e_id = EPX.e_id
JOIN Project P ON P.p_id = EPX.p_id
WHERE EPX.e_id = #e_id
ORDER BY P.Name
For more details, add them to the SELECT list. E.g.: SELECT E.e_name, E.address, P.p_name, P.start_date, P.end_date, etc. Here the E is an alias for the Employee table, and P is an alias for the Project table. This query will return one row for each entry in the EmployeeProjectXref table (provided that it references real entries in the Employee and Project tables.) If there is one employee and three projects, you'll get three rows.

Table with multiple columns containing employee keys. How to link to employee master to get names?

My table has columns of data that contain employee keys for different departments. One column, for example would be business office, another admin.
I can't figure out how to write and sql that will join the employee master to return each persons name for each row. I could do it if it only had one column of employee master keys but it beat me
I think from what you have described you need to join to the Employee Master table multiple times:
SELECT A.Col1
, A.BOEmpID
, BO.EmpName
, A.AdminEmpID
, AD.EmpName
, <....>
FROM MyTable A
INNER JOIN Employees BO
ON A.BOEmpID = BO.EmpID
INNER JOIN Employees AD
ON A.AdminEmpID = AD.EmpID