Display Manager name instead of Manager Id in Oracle ADF - sql

Employees table contains -
Employee_Id,
Manager_Id,
First_Name,
Last_Name
I want to display manager name instead of id
LOV cannot be used
Current Query in VO:
SELECT
Employees.EMPLOYEE_ID,
Employees.FIRST_NAME,
Employees.LAST_NAME,
JobObject.JOB_TITLE,
Employees.COMMISSION_PCT,
Departments.DEPARTMENT_NAME,
Departments.DEPARTMENT_ID,
JobObject.JOB_ID,
(First_Name||' '||Last_Name) AS VIEW_ATTR,
Employees.SALARY,
Employees.MANAGER_ID
FROM EMPLOYEES Employees,
DEPARTMENTS Departments,
JOBS JobObject
WHERE Employees.DEPARTMENT_ID = Departments.DEPARTMENT_ID

I assume that you want to show Manager Name along with employee details. Is so then following query will be helpful.
SELECT emp.EMPLOYEE_ID,
emp.FIRST_NAME,
emp.LAST_NAME,
JobObject.JOB_TITLE,
emp.COMMISSION_PCT,
dept.DEPARTMENT_NAME,
dept.DEPARTMENT_ID,
JobObject.JOB_ID,
(emp_manager.First_Name || ' ' || emp_manager.Last_Name) AS manager_name,
emp.SALARY
FROM EMPLOYEES emp
JOIN DEPARTMENTS dept ON (emp.department_id = dept.department_id)
JOIN JOBS JobObject ON (emp.job_id = JobObject.job_id)
JOIN EMPLOYEES emp_manager ON( emp.manager_id = emp_manager.employee_id )

If I understood correctly ,the reason why you get nothing is related to join,when you use commas in the FROM clause may you get problem.
and for managername if you have a manager table you have to inner join with that or try this:
SELECT Employees.EMPLOYEE_ID,
Employees.FIRST_NAME,
Employees.LAST_NAME,
JobObject.JOB_TITLE,
Employees.COMMISSION_PCT,
Departments.DEPARTMENT_NAME,
Departments.DEPARTMENT_ID,
JobObject.JOB_ID,
(Employees.First_Name+' '+Employees.Last_Name) AS ManagerName,
Employees.SALARY,
Employees.MANAGER_ID
FROM EMPLOYEES Employees,
inner join
DEPARTMENTS Departments on Employees.DEPARTMENT_ID = Departments.DEPARTMENT_ID
left outer join
JOBS JobObject on JobObject.Job_Id=Employees.Job_Id
where Employees.MANAGER_ID is not null and Employees.MANAGER_ID in (EMPLOYEE_ID from employees)

Related

How to write this code using join, Oracle

SELECT departments.department_name, result1.total_amt
FROM departments,
( SELECT employees.department_id, SUM(employees.salary) total_amt
FROM employees
GROUP BY department_id) result1
WHERE result1.department_id = departments.department_id;
You can use the both tables in a single query as follows:
SELECT departments.department_name,
SUM(employees.salary) total_amt
FROM departments
JOIN employees ON employees.department_id = departments.department_id
GROUP BY departments.department_id, departments.department_name
Try this Code -
SELECT dept.department_name, SUM(emp.salary)
FROM departments dept inner join employees emp on dept.department_id =dept.department_id
GROUP BY dept.department_name;

SQLite Multiple Subqueries Logic

The problem is asking for one to write a query to find the names (first_name, last_name) of the employees who have a manager who works for a department based in the United States. Here is a link to the problem, to see the tables, https://www.w3resource.com/sqlite-exercises/sqlite-subquery-exercise-3.php.
For the subquery, I did a left join on the location id between the department and location tables then I selected 'US' for the country_id, and returned the manager_id
For the outer query, I chose the Employee table, selected manager_ids from sub-query list.
SELECT first_name, last_name
FROM Employees
WHERE manager_id IN (SELECT manager_id
FROM Departments d LEFT JOIN Locations l ON d.location_id = l.location_id
WHERE country_id = 'US')
ORDER BY first_name;
With my code, I do not get the correct answer, the same results as the result-set/output shown on the website.
There are three subqueries in total in the correct answer. I do not understand what the purpose of including the subquery involving the employees table (outermost subquery). I understand that is where I messed up but don't understand why.
SELECT first_name, last_name
FROM employees
WHERE manager_id IN
(SELECT employee_id
FROM employees
WHERE department_id IN
(SELECT department_id
FROM departments
WHERE location_id IN
(SELECT location_id
FROM locations
WHERE country_id='US')));
You need to join all the tables:
select e.first_name, e.last_name
from employees e
inner join employees m on m.employee_id = e.manager_id
inner join departments d on d.department_id = m.department_id
inner join locations l on l.location_id = d.location_id
where l.country_id='US'

SQL joins from same table column

Have two tables
employees: employee_id , employee_name, manager_id, department_id
departments: department_id,manager_id
I need to show employee id, employee name, manager name, and manager id, yet I cannot figure out how to show manager name. Here is what I have even though it is very wrong and doesn't show the real manager name
SELECT e.last_name "Employee", e.employee_id "Emp#", e.last_name "Manager",
d.manager_id "Mgr#"
FROM employees e,
departments d;
Figured it out using keyword Join - Only required use of one table twice
SELECT e.last_name "Employee", e.employee_id "Emp#", e2.last_name "Manager",
e.manager_id "Mgr#"
FROM employees e
JOIN employees e2 ON e2.employee_id = e.manager_id;
Or without Keyword Join / using a simple join
SELECT e.last_name "Employee", e.employee_id "Emp#", e2.last_name "Manager",
e.manager_id "Mgr#"
FROM employees e, employees e2
WHERE e2.employee_id = e.manager_id
you can do inner join on manager_id so only the matching rows remain
SELECT last_name as "Employee", employee_id as "Emp#", last_name as "Manager",
manager_id as "Mgr#"
FROM employees as e1
INNER JOIN departments as d2
ON e1.manager_id = d2.manager_id;

Sql query from multiple tables - joins not working

I have tried sub queries and join queries, but I cannot get the right answer, as I need to retrieve data from different tables. This is what I am trying to do:
Show the department Name, Street Address and
City of the department location and the Country Name
where the department is located for an employee
with the name Den Raphaely
I have tables like this.
COUNTRIES TABLE
country_id
country_name
region_id
DEPARTMENT TABLE
department_id
department_name
manager_id
location_id
EMPLOYEES TABLE
first_name
last_name
email
phone
salary
commission
department_id
job_id
hire_date
JOBS_HISTORY TABLE
employee_id
start_date
end_date
job_id
department_id
JOB TABLE
Job_id
Min_salary
Max_Salary
LOCATIONS TABLE
Location_id
Street_address
Postal_code
State
Country_id
REGION TABLE
region_id
region_name
This is the attempted query
SELECT Employees.FIRST_NAME, Employees.LAST_NAME,
Departments.DEPARTMENT_NAME, Locations.Street_Address,
Locations.City, Countries.Country_Name FROM
Countries INNER JOIN Locations ON
Countries.Country_ID=Locations.Country_ID
INNER JOIN Departments ON
Locations.Location_id=Departments.LOCATION_ID
INNER JOIN Employees ON
Departments.DEPARTMENT_ID = (SELECT Employees.DEPARTMENT_ID FROM Employees
WHERE FIRST_NAME LIKE 'Den' AND LAST_NAME LIKE 'Raphaely')
You are confusing your JOINs with your filtering (WHERE) logic:
SELECT
Employees.FIRST_NAME,
Employees.LAST_NAME,
Departments.DEPARTMENT_NAME,
Locations.Street_Address,
Locations.City,
Countries.Country_Name
FROM Countries
INNER JOIN Locations ON Countries.Country_ID=Locations.Country_ID
INNER JOIN Departments ON Locations.Location_id=Departments.LOCATION_ID
INNER JOIN Employees ON Departments.DEPARTMENT_ID = Employees.DEPARTMENT_ID
WHERE Employees.FIRST_NAME LIKE 'Den' AND Employees.LAST_NAME LIKE 'Raphaely'
SELECT e.first_name, e.last_name, d.department_name, l.street_address, l.city, c.country_name
FROM
employees e
INNER JOIN
department d
ON e.department_id = d.department_id
INNER JOIN
location l
ON d.location_id = l.location_id
INNER JOIN country c
ON c.country_id = l.country_id
WHERE
e.first_name = 'Den'
AND
e.last_name = 'Raphaely'

Include rows that contain null values in a particular column with inner join

The goal here is to return the ID, Name, and Manager Name and ID for each Employee. The table does not contain a manager name, only a manager ID for a given employee. The current query works great using an inner join, except that one employee does not have a manager ID (he's the boss), and so none of his information appears. The current query looks like this:
SELECT DISTINCT e.employee_id AS EMPLOYEE_ID,
e.FULL_NAME AS EMPLOYEE_NAME,
m.manager_ID AS REPORTS_TO,
m.FULL_NAME AS MANAGER_NAME
FROM EMPS e
INNER JOIN EMPS m ON e.manager_id = m.employee_id;
How can I include the name and information for this employee despite his lack of a seemingly necessary field? Thanks
If you want to include the employee name when there is no manager do a left join:
SELECT DISTINCT e.employee_id AS EMPLOYEE_ID,
e.FULL_NAME AS EMPLOYEE_NAME,
m.manager_ID AS REPORTS_TO,
m.FULL_NAME AS MANAGER_NAME
FROM EMPS e
LEFT JOIN EMPS m ON e.manager_id = m.employee_id;