I am new to this - The below query is showing an error - employees and departments are tables and departmnet_id is the column name.
SELECT
name,
department_id,
role
FROM
employee_data.employees
INNER JOIN
employee_data.departments
ON
'august-emitter-173906.employee_data.employees.department_id' = 'august-emitter-173906.employee_data.departments.department_id'
This is because your department_id in your select could refer to either department id from departments or employees.
Try using an alias like:
SELECT
emp.name, emp.department_id, emp.role
FROM employee_data.employees emp
INNER JOIN employee_data.departments dep
ON emp.department_id = dep.department_id
Note: not sure which one is the correct table to use in your case but simply swap the alias as needed.
Example error:
As you can see here, I am not using an alias, similar to your question, and there is an ambiguity error:
Example solution:
With alias the ambiguity error is resolved
Related
I am a beginner, but I am a little stumped as to why I keep getting errors. I thought specifying the table would work. Do I have to specify the database as well? If I have to add my project name 'April-17' also I will loose my mind.
SELECT departments.department_id
FROM april-17.employee_data.departments d
FULL JOIN april-17.employee_data.employees e
ON d.department_id=e.department_id
You probably want
SELECT d.department_id
FROM departments d
FULL JOIN employees e
ON d.department_id=e.department_id
If you only have SELECT department_id it won't know which table you want department_id from (could be d or could be e), hence the "ambiguous" error message. But you have aliased the table to d so you also can't use SELECT departments.department_id, hence the "unrecognized name" error message.
Do you mean your database name? What errors are you getting? I will re-write your query, I hope you find this useful
SELECT <insert list of field you want select here> FROM departments d JOIN employee_data e on d.department_id = e.department_id
I understand how to print manager's name if it's in the same table, doing a self join.
However, how to print manager's name if it's in a different table?
I have 2 tables:
employee - list of all employees (employee_id, employee_first_name),
employee_assignment - list of all (employee_id and manager_id)
I try to do it like this, and I obtain only manager's id in the output. However, I want to show manager's name. but I can't use ea.employee_first_name since there is no such reference.
SELECT DISTINCT e.employee_first_name, ea.manager_id
FROM employee as e
LEFT JOIN employee_assignment ea ON e.employee_id = ea.employee_id
It seems that you are missing another table.
The table employee_assingment is used to keep the relation between the tables employee and manager. You can take a look here to get some tips about relationships.
Please, check your schema to find a manager table, where the manager_id, available in employee_assignment, is the PRIMARY KEY.
The final query should be something like this:
SELECT DISTINCT e.employee_first_name, man.manager_first_name
FROM employee as e
LEFT JOIN employee_assignment ea ON e.employee_id = ea.employee_id
LEFT JOIN manager as man ON man.manager_id = employee_assignment.manager_id
Whenever I add this line of code it always throw the exception
OFFSET 0 ROWS
FETCH NEXT 10 ROWS ONLY;
Complete code:
SELECT
EMPLOYEES.EMPLOYEE_ID,
EMPLOYEES.FIRST_NAME, EMPLOYEES.LAST_NAME, EMPLOYEES.EMAIL,
EMPLOYEES.PHONE_NUMBER, EMPLOYEES.HIRE_DATE, EMPLOYEES.JOB_ID,
EMPLOYEES.DEPARTMENT_ID, EMPLOYEES.MANAGER_ID, EMPLOYEES.SALARY,
JOBS.JOB_ID, JOBS.JOB_TITLE, DEPARTMENTS.DEPARTMENT_ID,
DEPARTMENTS.DEPARTMENT_NAME, DEPARTMENTS.MANAGER_ID, M.EMPLOYEE_ID,
M.FIRST_NAME
|| ' '
|| M.LAST_NAME AS MANAGER_NAME
FROM
EMPLOYEES
INNER JOIN
JOBS ON EMPLOYEES.JOB_ID = JOBS.JOB_ID
INNER JOIN
DEPARTMENTS ON EMPLOYEES.DEPARTMENT_ID = DEPARTMENTS.DEPARTMENT_ID
INNER JOIN
EMPLOYEES M ON DEPARTMENTS.MANAGER_ID = M.EMPLOYEE_ID
ORDER BY
EMPLOYEES.EMPLOYEE_ID ASC
I want to display data from multiple tables in my site and limit it only to ten output but it always throw the exception. What should I do? I've tried adding aliases but it didn't work.
The simple answer to your question is:
According to The Oracle 12c documentation about FETCH:
If the select list contains columns with identical names and you
specify the row_limiting_clause, then an ORA-00918 error occurs. This
error occurs whether the identically named columns are in the same
table or in different tables. You can work around this issue by
specifying unique column aliases for the identically named columns.
Cheers!!
Give alias to duplicate names
M.FIRST_NAME as "Manager name"
Or without as
DEPARTMENTS.DEPARTMENT_ID "Department ID",
Say I have a query:
select
Last_Name,
First_Name,
e.EmployeeID
from
employee e
join
Employee_his eh on e.EmployeeID = eh.EmployeeID
First name and Last name are part of the employee table while employeeId is part of both.
Does adding the table alias e in front of last_name, first_name alter the statement in any way? I thought not but am told it will give me the wrong answer. Sorry about the simple question, it's a hard thing to look up.
This is your query:
select Last_Name, First_Name, e.EmployeeID
from employee e join
Employee_his eh
on e.EmployeeID = eh.EmployeeID;
In this case, the qualified names are needed, because EmployeeId --
with no qualification -- would be ambiguous. Which table would the column be referring to?
When you have multiple tables in a query, you should qualify all column names, so it is clear what the query is doing -- for others reading the query and for you months later.
I should note that ANSI SQL has the using clause. In many databases, the query could be written as:
select Last_Name, First_Name, EmployeeID
from employee e join
Employee_his eh
using (EmployeeId);
However, SQL Server does not support the using clause.
If both tables have a field with the same name, then yes, you need to have the table name or an alias in front of the field name.
I have this query:
SELECT to_number(gr.code) group_index,
gr.NAME group_name, f.*,
gr.description gr_desc
FROM obj$groups gr, obj$group_objects gro,
obj$group_objects gro2, tdf$flex_fields f,
inv$requests w, inv$Direction_Criterions c
WHERE gr.NO = gro.object_no
AND gro.group_no = obj$group_service.path_to_no(
'Some Condition String',
tdf$flex_field_service.get_local_list_group_no)
AND gro2.group_no = gr.NO
AND f.NO = gro2.object_no
AND w.no = 11593597
AND c.direction_no = w.direction_no
AND f.no = c.criterion_no
ORDER BY to_number(gr.code), f.name
Why are two same tables (group_objects) present here? I tried to reverse-engineer this, but couldn't myself, maybe anyone here already know of this trick?
This happens in Oracle database.
It's an operation called self-join. When you want to join records from the same table.
It usually happens when you have records related to records in the same table. Example:
create table tree
(
id number primary key,
parent_id number,
value varchar2(100)
);
So, if you want to retrieve nodes and their parents you would do:
select c.id, c.value, p.value as parent_value
from tree c inner join tree p on (c.parent_id = p.id)
Something similar is happening in the query you posted.
group_objects is being joined to groups in the clause gro2.group_no = gr.NO and to flex_fields in the clause f.NO = gro2.object_no. I suspect this covers the case where obe set of group object isn't exactly the same as the other set and this limits the rows in the two joins where one join removes a group_object that would then not be available to join to the other table.
It's hard to divine the original programmer's intent from this snippet, especially without a description of what the various tables hold.
However, it appears to me as if the final result of this query is supposed to report information from two different records from the group_objects table — one in which the group no matches "Some Condition String" and the other in which the group no matches a column value from the groups table. If I had to guess, it's retrieving an operation in which an item was transferred between two groups, or substituted to be used in place of an object from a second group, or something like that.
For illustration, the equivalent with the standard EMP table would be:
select e.ename, m.ename manager_name
from emp e, emp m
where m.empno = e.mgr;
Or in more modern syntax:
select e.ename, m.ename manager_name
from emp e
join emp m on m.empno = e.mgr;
i.e. show the names of employees with managers and the name of their managers.
The point is that the same table is used twice in the query in a different "role". It need not be a self-join, there could be another table (or more) in between like this:
select e.ename, pm.ename projmanager_name
from emp e
join project_assignments pa on p.empno = pa.empno
join projects p on p.proj_id = pa.proj_id
join emp pm on pm.empno = p.projmanager_empno;
i.e. show the names of employees assigned to projects and show the name of the project manager of that project.