SQL Query Involving Data From Different Tables - sql

I have two different tables with records I need to join together in a way I can't quite figure out how to make work. My data looks like this.
Table A
Columns: Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager ... many other columns
Sample Values:
12345 Department1 Team1 67890 Yes/No
.
.
.
One employee per row, several thousand rows comprising the entire company
Table B
Employee_ID, Manager_ID ... other columns
The exact same data set as Table A
Currently I'm combining those two tables (and three others) with a simple join on Employee_ID, which I'm then using as a data source in Tableau to visualize the data.
What I'd like to do with a SQL script is as follows:
Check to see whether an employee in Table A is a manager or not based on the Is_a_Manager column
If they are, find an employee in Table B who is one of their direct reports by matching the employee ID in Table A to the Manager ID in Table B.
Lookup that direct report's department and team in Table A by matching the Employee_ID in Table B to Employee_ID in Table A and displaying the Employee_Department and Employee_Team columns.
Add the direct report's department and team to two new columns in the original manager's Table A row
I'd like the final output in Table A to be something like
Employee_ID, Employee_Department, Employee_Team, Manager_ID, Is_a_Manager? ... Direct_Report_Department, Direct_Report_Team
Also, an important point is that some managers will have employees who are on different teams, so values in the Direct_Report_Department and Direct_Report_Team are not distinct. I only actually need any one employee's Department and Team to display, it doesn't matter which employee's it is.
Finally, I am able to do step 1 fairly easily in Tableau, so if the SQL script could do steps 2-4 and simply return a null value if the employee was not a manager, that would work for me as well.
Any ideas on how to accomplish this would be greatly appreciated. Thank you!

This should work based on the requirement provided. You don’t have to do any of the steps in Tableau and can simply export the output from the SQL as your data source
Select Tb1.Employee_ID, Tb1.Employee_Department, Tb1.Employee_Team, Tb1.Manager_ID, Tb1.Is_a_Manager, Tb3. Direct_Report_Department, Tb3. Direct_Report_Team
from Table_A Tb1
join (Select Manager_id, max(Employee_id) as emp_id from Table_B group by Manager_id) Tb2
on Tb1.Employee_id = Tb2.Manager_id
left join (Select Employee_ID, Employee_Department as Direct_Report_Department, Employee_Team as Direct_Report_Team from Table_A group by Employee_ID, Employee_Department, Employee_Team) Tb3
on Tb2.emp_id = Tb3.Employee_ID
where Tb1.Is_a_Manager = 'Yes';

Related

Pulling information from two database tables

So, what I want to do with these tables is sort employee names and department names by salary, but they are on seperate tables, so I assume I have to somehow use the WORKDEPT column and connect it to the DEPT table, but I'm not really sure how.
SELECT * FROM tabA JOIN tabB ON tabA.DEPTNO = tabB.WORKDEPT ORDER BY salary

Assistance with joining two tables - not sure which joins to use

I am currently trying to bump up an application export against our enterprise HR listing to identify any current employees in the application that have been terminated (that will come from HR).
Table 1 - Application Listing. Contains Emp_name and Emp_ID
Table 2 - HR Listing. Contains Emp_name, Emp_ID, Termination_Date (important).
Do you know what code I need to end up with a column added into Table 1 with each employee's respective termination date? If NULL, they are still employed, if there's a date populated, that is our observation.
It should be this simple, unless there are other conditions you haven't discussed.
SELECT table1.*, table2.Termation_Date --list out columns instead of *
FROM table1
INNER JOIN table2
ON table1.Emp_ID = table2_EmpID
WHERE table2.termination_Date IS NOT NULL

How to get information back from tables in Oracle?

Hi i am using oracle sqlplus on the sample HR database tables.
I have 2 tables, employees and jobs in which i have job_id in both of them.
I want to be able to enter the input of the job_ID and then it displays the first_name, last_name (which is inside employees table)
I am new to sql and learning. Thanks in advance
When you want to query data out of an SQL Database, you need the SELECT Statement. In your case, that would be:
SELECT first_name, last_name FROM Employees WHERE job_ID=:job_ID;
SELECT ... Keyword of "SELECT" Statements
firstname, lastname ... Columns you want the query to return
FROM Employees ... Keyword + Tablename you want to query on
WHERE job_ID=:job_ID ... Condition which will be used to select specific rows
:job_ID ... Your input of which job_ID you want to get
Your searched Query was the userinput mixed with the Jobs:
SELECT first_name, last_name FROM Employees JOIN Jobs ON Employees.job_ID=Jobs.job_ID WHERE Jobs.min_salary=&min_salary AND Jobs.max_salary=&max_salary;
JOIN Combines the rows of each table to one bigger table, there are many different types of them, this is just the basic one
ON Gives the JOIN the parameter to check whether this column is the same in two rows of each table. If yes, they will be merged

Display all results queried on PostgreSQL where the JOINING value is missing

I have two tables on PostgreSQL, namely: employees and employers.
There is data in each of these tables.
Columns in employees: employee_id, employee_name, employer_id.
Columns in employers: employer_id, employer_name.
I want to display all employee_name's that don't have an associating employer_name.
I used the below query:
SELECT DISTINCT a.employee_name, b.employer_name
FROM employees a
NATURAL JOIN employers b
WHERE a.employee_name LIKE 'Jack';
NB!
I have also tried adding in the below to my query:
COALESCE(b.employer_name, '') = ''
Problem:
If there is no record in the employer table containing the associating employee_id value, the query returns nothing all. I am assuming this is because there is nothing for the two tables to join on?... But I would like to at least find all employees that don't have an employer. I would ideally like the employer_name value in my result to either return: blank/''/NULL.
Your assistance is greatly appreciated.
select employees.employee_name ,employers.employer_name
from employees
left join employers
on employees.employee_id = employees.employee_id
where employers.employer_name is NULL
Use a LEFT JOIN:
SELECT a.employee_name,
COALESCE(b.employer_name, 'NA') AS employer_name
FROM employees a
LEFT JOIN employers b
ON a.employer_id = b.employer_id
WHERE b.employer_id IS NULL
Your current query has several problems, first of which is that you are using a NATURAL JOIN, which should behave link an INNER JOIN, discarding employee records which do not match to any employer. Instead, by using LEFT JOIN, we can retain all employee records, regardless of whether or not they have an employer. The WHERE clause checks for NULL in the joined table, with NULL indicating that the employee did not match to any employer.
Your table name employees table is a little odd, IMO. There is a design 'rule of thumb' that says a table models either an entity or the relationship between entities but not both. To demonstrate this oddness, consider the relational operation is
employees MINUS employers
...which suggests something is off.
Another symptom of this design problem is that the employer_id in the employees table must have some kind of placeholder to represent the predicate employee has no employer, possibly a null (and nulls are to be avoided, IMO).
I suggest you fix this design by introducing a third table to model the relationship between an employee and their employer. Herein lies another design problem: shouldn't this new table be named employees? In other words, isn't the very definition of an employee a person who has an employer?
Consider this design instead:
People: person_id, person_name
Employers: employer_id, employer_name
Employees: employer_id, person_id
To find the names of people who are not employees in pseudo-relational notation:
People MINUS Employees { person_name }
SQL is quite a bit more verbose:
SELECT DISTINCT person_name
FROM People
NATURAL JOIN
( SELECT person_id FROM People
EXCEPT
SELECT person_id FROM Employees ) t;
Note your original query needlessly uses range variables a and b. One of the benefits of NATURAL JOIN is the elminiation of duplicate column names. Range variables with NATURAL JOIN always looks odd to me!

ORACLE SQL SELECT statement from multiple tables

I am a bit new to the world of oracle and sql and need some guidance. The problem at hand asks me to display name, nickname, and department name for all employees not in department number 20 or 30.
These categories come from different tables as follows.
name from table names01
nickname from table nicks01
deptname from depts01
deptnumber from table depts01
How do you create a SELECT statement to display these 3 field from different tables?
select a.name, b.nickname, .... from names01 a, nicks01 b, .....
The rest is left an exercise for the OP
Note that you referenced 4 tables but your description said 3 tables :)