FETCH throwing exception java.sql.SQLSyntaxErrorException: ORA-00918: column ambiguously defined - sql

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",

Related

Big Query issues: Im getting both a column name ambiguous error or unrecognized name if I specify table in SELECT

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

Bigquery - Column name name is ambiguous at [2:3]

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

Backticks preventing simple JOIN query

I'm just starting out..experimenting with simple queries. In the JOIN query below, the backticks in the FROM clause prevent the query from running - error is "Unrecognized name: employees at [9:5]" Remove the backticks, however, and the query runs just fine. Why is this? Thank you!
SELECT
employees.name AS employee_name,
employees.role AS employee_role,
departments.name AS department_name
FROM
`employees_data.employees`
INNER JOIN
employees_data.departments ON
employees.department_id = departments.department_id
If you are using quoted identifiers, you must quote the database and table name separately, thusly:
SELECT
employees.name AS employee_name,
employees.role AS employee_role,
departments.name AS department_name
FROM
`employees_data`.`employees`
INNER JOIN
employees_data.departments ON
employees.department_id = departments.department_id
When you quote an identifier like employees_data.employees that means the whole identifier. from `employees_data.employees` quote the .. It means "from the table employees_data.employees". In contrast to from employees_data.employees or from `employees_data`.`employees` which means "from the table employees in the schema employees_data".
Quote the individual identifiers, employees_data and employees; the . is syntax.
In general, don't quote identifiers if you don't have to. It can introduce other odd behavior. For example, SQL is generally case-insensitive, but quoting identifiers will make it case-sensitive. employees will match employees or Employees or EMPLOYEES but `employees` only matches employees.
Also, use schema qualifiers consistently. In your query sometimes you do and sometimes you don't. For example, your inner join is confusing. You're joining employees_data.departments on employees and departments. Is departments the same as employees_data.departments? Is employees_data.employees the same as employees?
If they're all in the same schema, don't use schemas. Fully qualifying your table names unnecessarily artificially restricts your query to only work on that schema; your query will break if the schema name changes.
SELECT
employees.name AS employee_name,
employees.role AS employee_role,
departments.name AS department_name
FROM
employees
INNER JOIN
departments ON employees.department_id = departments.department_id
This will query tables in the current schema, regardless of its name.

List the department name and the number of employees in each department

Query 4. List the department name and the number of employees (including contractors) in each department.
select Departments_Name AS "Department", Num_of_employ AS "Number of Employees"
from departments, employee_id
group by Departments_Name, Num_of_employ
order by Departments_Name
offset 0 rows fetch first 12 rows only;
I know I have to use aggregate functions however whenever this is executed, The Departments_Name of 'Accounting' repeats in the output
I'm not sure what I'm doing wrong. Can anybody help me?
Code run
If you actually need information from your employee_id table then you need to join it on using your foreign key. That said I can't see that you are using it. Also use proper join syntax not the old style implicit join you were attempting to use. At the moment you will be getting a duplicate department row per employee.
Then depending on whether you store Num_of_employ or whether you are trying to calculate it, either way you don't want to group by. You may want to sum it, but that doesn't make sense unless you have multiple entries per department.
select D.Departments_Name as "Department", sum(D.Num_of_employ) as "Number of employees"
from dbo.departments D
-- Do you actually need to join this on? You don't appear to be using it.
-- inner join dbo.employee_id E on E.DepartmentId = D.DepartmentId
group by D.Departments_Name
order by D.Departments_Name
offset 0 rows fetch first 12 rows only;
Maybe you are attempting to count the staff e.g.
select D.Departments_Name as "Department", count(*) as "Number of employees"
from dbo.departments D
inner join dbo.employee_id E on E.DepartmentId = D.DepartmentId
group by D.Departments_Name
order by D.Departments_Name
offset 0 rows fetch first 12 rows only;
I have changed the query according to the question you have asked, let me know if you find it useful
Here employee_status is column which as data like Contractual/Permanent
select employee_department as "Department", count(*) as "Number of employees",employee_status
from departments
group by Departments_Name,employee_status
order by Departments_Name
offset 0 rows fetch first 12 rows only;

Do table names always have to be in front of fields when using sql joins?

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.