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
Related
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
There is a task: develop a fragment of the Web site that provides work with one table.
Attributes of the table:
Day of the week,
Time of the beginning of the lesson,
Subject name,
Number of the audience,
Full name of the teacher.
We need to make a query: determine the day of the week with the largest number of entries, if there are more than one maximum (ie, they are the same), then output them all. I did the query as follows:
SELECT COUNT (*) cnt, day
FROM schedule
GROUP BY day
ORDER BY cnt DESC
LIMIT 1;
But if there are several identical maxima, then only one is displayed. How to write a query which returns them all?
You can use your query as a subquery in the HAVING clause, e.g.:
SELECT day, count(*) as cnt
FROM schedule
GROUP BY day
HAVING count(*) = (
SELECT count(*) as cnt
FROM schedule
GROUP BY day
ORDER BY cnt DESC
LIMIT 1
)
ORDER BY day
I want to find out the company with the maximum capital[price*quantity] (Including all entries for that company in the whole table). Any suggestions?
I've done this:
SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol
If FETCH FIRST WITH TIES is supported:
SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol
ORDER BY total DESC
FETCH FIRST 1 ROW WITH TIES
If not:
SELECT symbol, SUM(amount*price) AS total
FROM orders
GROUP BY symbol
HAVING SUM(amount*price) = (SELECT SUM(amount*price) AS total
FROM orders
GROUP BY symbol
ORDER BY total DESC
LIMIT 1)
Your query should look like this.
SELECT symbol,SUM(amount*price) AS total FROM orders GROUP BY symbol order by total desc limit 1;
I am adding limit 1 as you want to find out the company with maximum capital.
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.
I have this table :
Orders(Orderid,CustomerID,Amount)
I ask id's of top 10 customers (total amount of order)
I wrote the query which is print all customer (highest amount to min amount)
select CustomerID, sum(Amount) as Total from orders group by CustomerID order by Total desc;
How can I get the first 10 line of this result ? Or is my way to obtain top 10 wrong?
Here is what you want:
select CustomerID, sum(Amount) as Total
from orders
group by CustomerID
order by Total desc
LIMIT 10;