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;
Related
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
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.
I have a table ORDERS which has something like this value ,
customerNumber | orderNumber(PK)
40 1
30 2
40 3
20 4
30 5
So, this table has customerNumbers 40 and 30 placing the max orders. Can anyone tell me a MySQL query to return the customerNumber (numbers), i dont want the count of the orders, just want the customer (cutomers) with the max order placed .
Thanks.
You can use below statement to get the Customer who placed maximum orders.
SELECT customerNumber FROM orders
GROUP BY customerNumber
ORDER BY COUNT(orderNumber) DESC LIMIT 1;
I should get deservedly flamed for this, but hey, the sun's out and it's feeling like a good day...
SELECT x.customernumber
FROM
( SELECT customernumber
, COUNT(*) total
FROM my_table
GROUP
BY customernumber
) x
JOIN
( SELECT COUNT(*) total
FROM my_table
GROUP
BY customernumber
ORDER
BY total DESC
LIMIT 1
) y
ON y.total = x.total;
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
How can I query for the MAXIMUM COUNT Number of transaction...
My code is as follows:
SELECT customer_id, COUNT(customer_id)
FROM rental
GROUP BY customer_id
HAVING MAX(COUNT(customer_id)); //I need to get the MAXIMUM COUNT of the list
If you are looking for the customer_id with largest number of rows in the rental table, you could use:
SELECT customer_id, COUNT(customer_id) as CustomerRowCount
FROM rental
GROUP BY customer_id
ORDER BY COUNT(customer_id) DESC
LIMIT 1
The above query will count all of the records for each customer_id, sort them in descending order, and then select only the top row. The maximum count for a customer will be present in CustomerRowCount.
EDIT
Conrad brought up the point that two or more customer_id's may have the same number of records. If the business requirement is that multiple records should be returned in this case, then his query will give you the result you're looking for. If only one record should be returned, then a simple, consistent way to break ties would just be to take the user with the lowest customer_id. To do that, you can just change the ORDER BY statement to:
ORDER BY COUNT(customer_id) DESC, customer_id
Try:
SELECT customer_id, MAX(COUNT(customer_id))
FROM rental
GROUP BY customer_id;
or
SELECT top 1 customer_id, COUNT(customer_id)
FROM rental
GROUP BY customer_id
ORDER BY COUNT(customer_id) DESC
Since more than one customer can have the same Maximum of the count you should do as follows
SELECT customer_id,
COUNT(customer_id) AS customerrowcount
FROM rental
GROUP BY customer_id
HAVING COUNT(customer_id) = (SELECT COUNT(customer_id)
FROM rental
GROUP BY customer_id
ORDER BY COUNT(customer_id) DESC
LIMIT 1)
However if you're ok with an arbitrary customer being selected than you should use rsbarro's answer
Query for MySQL
SELECT
*
FROM (SELECT
customer_id,
COUNT(customer_id) AS CountOfCustomer
FROM rental
GROUP BY customer_id) q1
ORDER BY CountOfCustomer DESC
LIMIT 1