Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6572515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:00:57+00:00 2026-05-25T15:00:57+00:00

I need some help with oracle sql. The problem: I have 2 tables employee

  • 0

I need some help with oracle sql. The problem: I have 2 tables employee and department. I got the average department salary from one query and i want to use it to see how many employees make more money than the average of their department. I have this so far.

This query returns the avg of the department:

select ROUND(AVG(Salary), 2) Dept_avg_sal
 from employee, department
 where department.department_id = employee.department_id
 group by department_name

What i am trying to do is:

select employee_name,
       salary,
       d.department_name
from   employee e,
       department d
where  salary > (select ROUND(AVG(Salary), 2) Dept_avg_sal
                 from   employee,
                        department
                 where  department.department_id = employee.department_id
                 group  by department_name)  

The error that im getting is :01427. 00000 – “single-row subquery returns more than one row”

I know that 2 employees in the same department make more money than the average and i think this is what is causing the issue.

EMPLOYEE_NAME       - SALARY -                -DEPARTMENT_NAME-      DEPT_AVG_SAL           
-------------------- ---------------------- -------------------- ------------ 
FISHER              - 3000.00  -              SALES      -          2500.00                   
JONES          -      3000.00               - ACCOUNTING         -  2750.00                   
KING             -    5000.00           -     EXECUTIVE     -       4500.00                   
**SCOTT           -     2500.00           -     IT             -      2100.00                   
SMITH           -     2900.00          -      IT     -              2100.00**                   
WILSON           -    3000.00          -      RESEARCH      -       2633.33 

Any help would be really appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:00:57+00:00Added an answer on May 25, 2026 at 3:00 pm

    Your initial query is missing any join condition on the outer query and any correlation condition in the inner query that would limit that to just the row for the department of interest. Also generally you do not want to group by name as presumably id is the primary key.

    Resolving these issues to fix your correlated subquery gives

    SELECT e.employee_name,
           e.salary,
           d.department_name
    FROM   employee e
           JOIN department d
             ON d.department_id = e.department_id
    WHERE  e.salary > (SELECT ROUND(AVG(Salary), 2) Dept_avg_sal
                       FROM   employee e2
                       WHERE  e2.department_id = e.department_id)  
    

    But you may find ditching the scalar correlated sub-query and replacing with a derived table works better.

    SELECT e.employee_name,
           e.salary,
           d.department_name
    FROM   employee e
           JOIN department d
             ON d.department_id = e.department_id
           JOIN (SELECT ROUND(AVG(Salary), 2) Dept_avg_sal,
                        department_id
                 FROM   employee
                 GROUP  BY department_id) e2
             ON e2.department_id = e.department_id
                AND e.salary > e2.Dept_avg_sal  
    

    For Oracle the following should also work I believe

    SELECT employee_name,
           salary,
           d.department_name
    FROM   (SELECT employee_name,
                   salary,
                   d.department_name,
                   AVG(Salary) OVER (PARTITION BY e.department_id) AS AvgSalary
            FROM   employee e
                   JOIN department d
                     ON d.department_id = e.department_id)
    WHERE  salary > AvgSalary  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
I need some help from the shell-script gurus out there. I have a .txt
I need some help with Oracle's RLS feature. I have never used it before
I need some help in auditing in Oracle. We have a database with many
I need some help in creating an Oracle SQL which I will execute in
I need some help from Oracle/Security experts. I'm going to make functions for encryption/decryption
Alright, need some help created a query for an Oracle 10g DB. I have
Need some help about with Memcache. I have created a class and want to
Need some help to solve this. I have a gridview and inside the gridview

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.