I am new to sql, any help is appreciated.
I have two tables, employees
and jobs
. employees
contain a variable job_id
(multiple employees can have the same job_ID). jobs
contain variables job_id
and job_title
(one job_ID correspond to one job_title, this is the hr schema in oracle if you are interested).
I want the query to return: the job_title, job_ID and the number of people who have the same job_Id.
I tried the following code:
select j.job_title, e.job_ID, count(e.job_ID)
from employees e, jobs j
where e.job_id=j.job_id
group by e.job_Id
the error message is:
ORA-00979: not a GROUP BY expression 00979. 00000 - "not a GROUP BY expression" *Cause:
*Action:
Error at Line: 83 Column: 8
Can you help me fix this?
Using Group By with Inner JoinSQL Inner Join permits us to use Group by clause along with aggregate functions to group the result set by one or more columns. Group by works conventionally with Inner Join on the final result returned after joining two or more tables.
Solution. Select column values in a specific order within rows to make rows with duplicate sets of values identical. Then you can use SELECT DISTINCT to remove duplicates. Alternatively, retrieve rows in such a way that near-duplicates are not even selected.
The error message is a bit misleading. When you select
a bunch of fields and an aggregate, you have to group by
every field you select
and only the fields you select
. So your query has to be:
select j.job_title, e.job_ID, count(e.job_ID)
from employees e, jobs j
where e.job_id=j.job_id
group by e.job_Id, j.job_title
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With