employee table
empid(integer)
emp_name(varchar)
salary(integer)
location(varchar)
display employees who have same salary.
how to solve this?
You can use the below query to get the results:
Since you need same salary hence i have grouped out the result with respect to salary and then used a listagg function to retrieve all the employee names with a delimiter.
select salary,listagg(emp_name,' | ') within group (order by emp_name) "Employees"
from employee
group by salary;
Select * from employee where salary is not null
Related
CREATE TABLE EMPLOYEE (
NAME VARCHAR(500) UNIQUE,
AGE INT,
DEPT VARCHAR(500),
SALARY INT
)
INSERT INTO EMPLOYEE VALUES('RAMESH',20,'FINANCE',50000);
INSERT INTO EMPLOYEE VALUES('DEEP',25,'SALES',30000);
INSERT INTO EMPLOYEE VALUES('SURESH',22,'FINANCE',50000);
INSERT INTO EMPLOYEE VALUES('RAM',28,'FINANCE',20000);
INSERT INTO EMPLOYEE VALUES('PRADEEP',22,'SALES',20000);
Could someone explain the error in the query
SELECT NAME, AGE, DEPT, AVG(SALARY)
FROM EMPLOYEE
GROUP BY DEPT
ORDER BY AGE
(/* USING NAME,AGE ETC ALSO SHOWS ERROR- "column "employee.name" must appear in the GROUP BY clause or be used in an aggregate function")
Why is there an error; consider both the logic part and the syntax part for explanation?
CAN ANYBODY PROVIDE THE ANSWER USING SUB QUERY?
You can refer to the error on at here : must appear in the GROUP BY clause or be used in an aggregate function
POSTGRESQL
SELECT DISTINCT NAME, AGE, DEPT
, AVG(SALARY)
FROM EMPLOYEES
GROUP BY NAME, DEPT, AGE
ORDER BY AGE
When you are using group by then you should extract only those columns which you used in group by and you can choose aggregate function with this. So you can use like below query.
select DEPT, AVG(SALARY) FROM EMPLOYEE GROUP BY DEPT
Im using Oracle-Apex
I have a table with names and salaries. I want to get the name with the highest salary using MAX(salary).
So the query is like this:
SELECT NAME FROM EMPLOYEE
GROUP BY NAME
HAVING MAX(SALARY) = SALARY;
This doesnt work, error ORA-00979: not a GROUP BY expression, appears. So i use this to stop that error:
SELECT NAME FROM EMPLOYEE
GROUP BY NAME, SALARY
HAVING MAX(SALARY) = SALARY;
And it groups every different salary in one row and returns the max salary in each row, since every salary is different, it returns every row.
How do i group everything in one single big group without modyfing the tables? I mean i want this to work:
SELECT NAME FROM EMPLOYEE
WHERE MAX(SALARY) = SALARY;
But with having. Its simple really, but i cant find the way.
use subquery
SELECT NAME FROM EMPLOYEE
where salary = (select max( salary) from EMPLOYEE)
You need a WHERE clause and compare the salary with the max salary of the table:
SELECT NAME FROM EMPLOYEE
WHERE SALARY = (SELECT MAX(SALARY) FROM EMPLOYEE);
You can get the name of person who has max salary using following nested query.
select NAME
from EMPLOYEE
where SALARY= ( select max(SALARY)
from EMPLOYEE )
I am trying to find the number of employees in a table that earn exactly the maximum salary of all the employees in the table called tblPerson.
Select Max(x.[No of Employees]) as Number, x.Salary as Salary
from
(
Select Count(Id) as [No of Employees], Salary
from tblPerson
Group by Salary
Having Salary = MAX(Salary)
)x
where x.[No of Employees]=3
Now I know this is a kind of long and complex way of doing it, but I was trying to do it using a derived table. But I am getting the error:
"Column 'x.Salary' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"
My question is, why am I getting this particular error since the main query is a simple Select statement with a where clause. Isn't it??
Mainly, aggregate functions work only with other aggregate functions or grouped by columns.
Why? Because an aggregate function needs to know the set of values to do calculation with.
In you case, the max() will want to use all the data available for the calculation and display a single result (single row) and the other column will want to be displayed row by row. So there's a conflict.
Thank you all. Every answer helped me. However, I think I found a pretty simple way to do it:
Select top 1 Count(Id) as [No of Employees], salary
from tblPerson
Group by Salary
Order by [No of Employees] DESC
select count(*) from tblPerson where salary=(select max(salary) from tblPerson)
You get the error because 'max' is an aggregation, while you have nothing to aggregate the number by.
Select Max(x.[No of Employees]) as Number, x.Salary as Salary
from
(
Select Count(Id) as [No of Employees], Salary
from tblPerson
Group by Salary
Having Salary = MAX(Salary)
)x
---------
Group by Salary -- all other items in your select statement
---------
where x.[No of Employees]=3
however, you can also use a temporary table or variable to find the persons.
To solve this via a variable, you could do the following
declare #maxSalary Decimal
set #maxSalary = (Select max(salary) from tblperson) --insert the max value into a variable
Then either aggregate the persons (or do some other logic):
Select ID from tblperson where salary = #maxSalary
The reason for not using a group by is that using a variable is more efficient, as you search the table instead of aggregating over it.
Create a CTE (RESULT), and using DENSE_RANK function, get the highest salary, together with the EmployeeID's.
The first row of the RESULT table will give the highest salary.
Using the aggregate function COUNT, get the number of Employees with the highest Salary.
with RESULT (EmployeeID, Salary, DenseRank) as
(select EmployeeID, Salary,
DENSE_RANK() over (ORDER BY SALARY DESC) AS DenseRank
from Employee)
select TOP 1 Salary,
(select COUNT(EmployeeID)
from Employee
where Salary = (select TOP 1 Salary)
from RESULT
where DenseRank = 1)
)
from RESULT
where DenseRank = 1;
I want to get average for a column
Name Salary
HI 33168,00
Hello 32882,00
When I tried with below query
select Name, Salary, Avg(Salary) as avg from table group by name, Salary
I'm getting Salary and Avg salary both same value.
expected result:
Name Salary
HI 33168,00
Hello 32882,00
avg 33025,00
If you group by [name] and [Salary], assuming your name is unique, you will get one average salary per [name], which will be the same as the salary value
select Avg(Salary) as avg from table
If you want detail and the average row, you could use a union all
select name, Salary from table
UNION ALL
select 'Avg', Avg(Salary) from table
To get overall average of all Salary values, don't group the results at all:
select Avg(Salary) as avg from table
To get average of Salary for each Name, you have to group only by the Name, not by Salary:
select Name, Avg(Salary) as avg from table group by Name
This would calculate salary average for each Name that is present in the table.
If you group by name and salary you will get one row for each such combination. In many cases this means that avg(salary) will be the same as salary. If you want to only get the average(salary) per name:
select Name, Avg(Salary) as avg from table group by name
If you want each individual salary as well you can do this with a union:
select Name, Avg(Salary) as salary from table group by name
union
select Name, Salary from table
A shortcut for the latter is:
select Name, Salary, Avg(Salary) as salary from table
GROUP BY GROUPING SETS((Name, salary), (Name))
If you just want to add an average salary to your existing table, you can use with rollup (or grouping sets):
select coalesce(name, 'avg') as name, avg(salary) as salary
from table
group by name, salary with rollup;
The avg(salary) does nothing for the names, but it does allow the with rollup to add a row with the overall average.
If there are multiple rows per name, and you want the average of averages, you can do:
select coalesce(name, 'avg') as name, avg(salary) as salary
from table
group by name with rollup;
I have a table containing attributes Id, Emp_name, dept_name, salary. Now i want to write an SQL query that will give me the dept_name value for which the overall salary of all employees belonging to that department is the highest, i.e dept for which sum of salaries of all its employees is the highest...? If there is any similar question with answer on stackoverflow, please suggest.. I dint find one. Thanks :)
I tried group by with sum() function, but i could not get how to find the maximum and compare it with sum.
Can you do
SELECT TOP 1 dept_name FROM table GROUP BY dept_name ORDER BY SUM(salary) DESC
Seems like a textbook example for GROUP BY:
select dept_name, total_salary from (
Select dept_name, sum(salary) as total_salary
from my_table
group by dept_name
) order by total_salary desc
try this:
select top 1 dept_name from myTable group by dept_name order by sum(salary) desc
SELECT dept_name FROM table
GROUP BY dept_name
ORDER BY SUM(salary) DESC
LIMIT 1
And you would also better have you departments in another table linked to the first table via foregn keys. Just a note.
I don't know exactly know your requirements, but perhaps there is another point to be considered: Two (or more) departments could have the same sum of salary.
I have not tested the query, but this should give you all departments which have the maximum some of salary:
select dept_name FROM table_name GROUP BY dept_name HAVING SUM(salary)=(select MAX(sum_salary) FROM (select SUM(salary) AS sum_salary FROM table_name GROUP BY dept_name))