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.
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
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.
Write a query to display the customer name who visited the second highest number of times
select customer_id,count(*) from booking group by customer_id ;
using this query i got the count of number of visits for each customer as shown below
CUSTOMER_ID,COUNT(*)
C001,6
C002,1
C003,1
C004,1
C005,4
but i want to display only c005 since he has visited the second maximum time
SELECT customer_id, COUNT(*)
FROM booking
GROUP BY customer_id
HAVING COUNT(*) <> (SELECT MAX(t.custCount)
FROM (SELECT COUNT(*) AS custCount
FROM booking
GROUP BY customer_id) t )
ORDER BY COUNT(*) DESC
LIMIT 1
As a side note, this won't work if there are ties for second place. In this case, you use the above query as a condition in the WHERE clause, e.g.
SELECT customer_id
FROM booking
GROUP BY customer_id
HAVING COUNT(*) = (query given above)
You can use a outer query and filter the same like
select customer_id from (
select customer_id,
count(*) as datacount
from booking
group by customer_id ) xxx
order by datacount desc
limit 1;
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;
Basically I have a review table for product. The attributes are reviewID, reviewCustName, reviewText, productID. So I wonder is there any ways to count the product with most reviews? Here is my SQL statement:
SELECT productID, count(*) AS mostReviews, MAX(mostReviews) FROM sm_review GROUP BY productID;
I wonder is it possible to write such SQL statement? Or i there any better way?
Thanks in advance.
You can use the following to get the result. This gets the total count for each product but when you order the count in a descending order and apply LIMIT 1 it returns only the product with the most reviews:
select count(*) total
from sm_review
group by productId
order by total desc
limit 1
It should just be;
SELECT count(*) AS num_reviews FROM sm_review
GROUP BY productID ORDER BY num_reviews DESC LIMIT 1;
Note the ORDER BY num_reviews and the LIMIT 1 which limits the number of results.