Finding average highest salary - mysql

Write a query to find the highest average sales among all the salespersons using the given table.
Table: Sales
Field Type
InvoiceNo Integer
SalesPerson Text
TotalSale Integer
Sample
InvoiceNo SalesPerson TotalSale
1 Acheson 50
2 Bryant 25
3 Bennett 250
4 Acheson 50
5 Bryant 100
6 Bennett 250
Sample output
max(totalsale)
250.0000
I tried using: SELECT MAX(TotalSale) FROM Sales. My output for sample test case was 250 but it was given wrong answer verdict in an online judge.
Also, when I changed the query to : SELECT MAX(TotalSale*1.0000) FROM Sales,I got correct answer for sample test case but still wrong answer for other test cases.
What is meant by average here? What should be the query?
Also, this isn't from any on-going competition. It is from a practice test which you can attempt here: https://www.hackerearth.com/challenge/test/kredx-data-analyst-test/

1.First you have to calculate the average TotalSale for each SalesPerson using the AVG function.
SELECT SalesPerson, AVG(TotalSale) AS 'TotalSale'
FROM Sales
GROUP BY SalesPerson
2.Find out the max value from the table generated by the above query.
SELECT MAX(avgSales.TotalSale)
FROM (SELECT AVG(TotalSale) AS 'TotalSale' FROM Sales GROUP BY SalesPerson) AS avgSales

SELECT avg(TotalSale) as MaxTotal
FROM Sales
GROUP BY Salesperson
ORDER BY MaxTotal desc
LIMIT 1
Output:
MaxtTotal
250.0000

The average sale would use the avg() function:
select salesperson, avg(totalsale) as avg_totalsale
from sales
group by salesperson;
The maximum of the average can be obtained using order by and limit:
select avg(totalsale) as avg_totalsale
from sales
group by salesperson
order by avg_totalsale desc
limit 1;

Here is another way to answer this:
select avg(TotalSale)
from Sales
group by SalesPerson
order by totalSale desc limit 1;
output:
avg(TotalSale)
250.0000

Minor change to #gordon-linoff 's answer to pass the sample test case.
select CAST(avg(totalsale) AS DECIMAL(16,4)) as avg_totalsale
from sales
group by salesperson
order by avg_totalsale desc
limit 1;
CAST(avg(totalsale) AS DECIMAL(16,4))
The above part will restrict the zero's in decimal place up to 4. Convert 250.00000000 to 250.0000

SELECT max(avgsale.TotalSale)
FROM (Select Cast(Round(Avg(TotalSale),2) as dec(10,4)) 'TotalSale' from Sales
GROUP BY salesPerson) avgsale

Related

How to find Maximum of average in group by clause in SQL

Hi I have a query like this
select avg(TotalSale) as value from Sales group by SalesPerson;
which gives me the output as
value
50.0000
250.0000
62.5000
I want to just get the max out of the values. What should i do?
I tried
select max(avg(TotalSale)) as value from Sales group by SalesPerson; which is wrong.
select avg(TotalSale) as value from Sales group by SalesPerson order by value desc limit 1; which works partially.
What would be better approach to do this?
Wrap your query....
select max(avgvalue)
from
(
select avg(totalsale) as avgvalue
from sales
group by salesperson
)z
Use order by and fetch one row:
select avg(TotalSale)
from Sales
group by SalesPerson
order by avg(TotalSale) desc
limit 1;
You can also get the SalePerson involved with this as well.
I guess it can be done in a single query .Try this :
SELECT
Max(Totalsale) as Value
FROM
Sales
ORDER BY avg(Totalsale)
LIMIT 1;

Need to understand the solution to this query

We define an employee's total earnings to be their monthly worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as space-separated integers.
This is link to the question for better understanding https://www.hackerrank.com/challenges/earnings-of-employees/problem
I am a beginner in SQL and couldn't understand the solution which was given
1.SELECT (months*salary) as earnings,
2.COUNT(*) FROM Employee
3. GROUP BY earnings
4. ORDER BY earnings DESC
5.LIMIT 1;
I understood the first step where we are giving months*salary an Alias which is earnings
in the 2nd step we are counting the no. of employees from employee table. I didn't understand why we are using group by here, 4th and 5th step is also clear, we used order by clause in order to rank earnings from highest to lowest, and limit 1 will fetch me the highest value. But why GROUP BY?
Ignore 1,2,3,4,5. These are just numbers I used for better clarity
You have splitted the query errorneously.
Must be:
SELECT (months*salary) as earnings, -- 2 and 4
COUNT(*) -- 4
FROM Employee -- 1
GROUP BY earnings -- 3
ORDER BY earnings DESC -- 5
LIMIT 1; -- 6
Step 1 - table Employee is used as data source
Step 2 - the value of the expression (months*salary) for each record in the table is calculated
Step 3 - the records which have the same value of the expression from (2) are treated as a group
Step 4 - for each group the value of the expression from (2) is put into output buffer, and the amount of records in a group is calculated and added to output buffer
Step 5 - the rows in output buffer are sorted by the expression from (2) in descending order
Step 6 - the first row from the buffer (i.e. which have greatest value of the expression from (2)) is returned.
Step 3: GROUP BY earnings was used to GROUP TOGETHER same value earnings. If you have, for example, earnings of $3,000, and there were 3 of them, they will be grouped together. GROUP BY is also required in combination with the aggregate function COUNT(*). Otherwise, COUNT(*) will not work and return an error.
Step 4: ORDER BY earnings DESC was used to order the GROUPED EARNINGS in DESCENDING order. Meaning, from HIGHEST EARNINGS down to the LOWEST EARNINGS.
Step 5: LIMIT 1 limits the returned row count to only 1.
Hope this helps! :)
GROUP BY aggregate your Results. When there are multiple numbers of "earnings" with a same value is just a single one in your table.
SELECT TOP 1 (months * salary), COUNT( * )
FROM Employee GROUP BY (months * salary)
ORDER BY (months * salary) DESC;
use above one for MS SQL server.
Use MySQL
select (months * salary) as earnings, -- select employee's total earnings to be their monthly worked
count(*) -- select all data
from Employee
group by earnings -- group by earnings to find the count of the number of employees who have earned max
order by earnings desc -- order by descending to get the height earning to lowest earning
limit 1; -- get the height earning and total number of employees who have maximum total earnings
select (salary * months) as max_earning -- for selecting max_earning
,count(*) -- for counting total employees with max_earning
from employee --table from which data is fetched
Group by (salary * months) desc --grouping the max_earning in desc order
limit 1; -- selecting the 1 record with max_earnings.
This code is for MYSQL
Try this query:
SELECT months*salary,COUNT(*) FROM Employee GROUP BY months*salary having count(*) = 7 ORDER BY months*salary DESC;
SELECT
MAX(salary * months), COUNT(*)
FROM
Employee
GROUP BY
(salary * months)
ORDER BY
(salary* months) desc limit 1;
select max(salary*months) ,count(*)
from employee
where (salary*months) in (
select max(salary*months) from employee
)
this is implemented in mysql

max average value in MySql

How to get the maximum from averages values in MySQL? The following query returns average values of amounts from table orders grouped by customers.
SELECT AVG(amount)
FROM orders
GROUP BY cust;
I want to receive a maximum value from average values using a single query with aggregate functions. Using ORDER BY ... DESC LIMIT 1 surely works, but what I am interested in is getting the maximum average value using aggregate functions solely. Is it possible at all? Thanks
select max(avg_value)
from
(
SELECT AVG(amount) avg_value FROM orders GROUP BY cust
) tmp
I would do this with order by and limit:
SELECT AVG(o.amount) as avg_value
FROM orders o
GROUP BY cust
ORDER BY avg_value DESC
LIMIT 1;
This allows you to get the cust for the maximum as well.

MYSQL - SUM of a column based on common value in other column

I'm stuck on crafting a MySQL query to solve a problem. I'm trying to iterate through a list of "sales" where I'm trying to sort the Customer IDs listed by their total accumulated spend.
|Customer ID| Purchase price|
10 |1000
10 |1010
20 |2111
42 |9954
10 |9871
42 |6121
How would I iterate through the table where I sum up purchase price where the customer ID is the same?
Expecting a result like:
Customer ID|Purchase Total
10 |11881
20 |2111
42 |16075
I got to: select Customer ID, sum(PurchasePrice) as PurchaseTotal from sales where CustomerID=(select distinct(CustomerID) from sales) order by PurchaseTotal asc;
But it's not working because it doesn't iterate through the CustomerIDs, it just wants the single result value...
You need to GROUP BY your customer id:
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID;
Select CustomerID, sum(PurchasePrice) as PurchaseTotal FROM sales GROUP BY CustomerID ORDER BY PurchaseTotal ASC;
Just by having a little Google search, I managed to find a page doing exactly what you're doing (I think). I have tailored the query below to fit your circumstance.
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID
ORDER BY PurchaseTotal ASC
Link to Page with Tutorial on SQL Groups

How to form sql structure to get max total value

I have a table "sales"
transactionId salepersonId amount
1 1 100
2 1 200
3 2 50
4 3 60
5 3 200
I like to find out how to get the salesperson with the highest total sale.
I know I need to use max and sum but I don't know how to combine them.
This is what I got
select salespersonId, sum(amount) from sales group by salesperson;
This would give me the total sales of each person, but I am not sure the next step to get the max. can someone help me ?
The standard SQL way is to use order by and limit or top
select salespersonid, sum(amount) as total
from sales
group by salespersonid
order by total desc
limit 1;
In SQL Server, you would use top 1 instead of limit 1:
select top 1 salespersonid, sum(amount) as total
from sales
group by salespersonid
order by total desc;
select salespersonId, sum(amount) as total
from sales group by salespersonID ORDER BY total DESC LIMIT 1;
This should work
select salepersonId, sum(amount) from sales group by amount DESC limit 1
OR
select rec.id, max(rec.amt) from (select salepersonId id, sum(amount) amt from sales group by salepersonId) AS rec