SQL - getting record with maximum value - sql

I am trying to retrieve the product with the highest price:
SELECT ProductName, Price
FROM [Products]
ORDER BY Price DESC
LIMIT 1
I wanted to know if there is another way of doing this in a more efficient way, with MAX for example.

Use MAX and GROUP BY
SELECT ProductName, MAX(Price) [Price]
FROM [Products]
GROUP BY ProductName
ORDER BY MAX(Price) DESC
LIMIT 1;

I've always done it with the following
SELECT top 1 Name
FROM tableName
ORDER BY Price DESC

You can use TOP 1 but you always have to consider the possibility of having a tie, so:
SELECT TOP 1 WITH TIES ProductName, Price
FROM [Products]
ORDER BY Price DESC

select top 1 * from [Products] order by Price desc

Related

Fixing Nested aggregated function

I am trying to display the productid for the product that has been sold the most (i.e, that has been sold in the highest quantity)
I have tried multiple different versions of code but every time it says cannot nest aggregated operations
SELECT productid
FROM soldvia
GROUP BY productid
WHERE productid IN (SELECT MAX(SUM(noofitems)) FROM soldvia GROUP BY productid);
I expect the output to be
PRODUCTID
3x3
4x4
You can't nest aggregations.
Use ORDER BY with TOP :
SELECT TOP 1 productid
FROM soldvia
GROUP BY productid
ORDER BY SUM(noofitems) DESC
Please try below query for your exact answer.
select productid, sum(noofitems) as max_sold,
convert(varchar,productid) +' x '+ convert(varchar,sum(noofitems)) as
output_sold from soldvia group by productid order by sum(noofitems) desc
Output will be
ProductId NoOfItemSold Output_Sold
1 7 1x7
2 4 2x4
3 1 3x1
In Teradata, you can use the qualify clause:
SELECT productid
FROM soldvia
GROUP BY productid
QUALIFY ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) = 1;
This is handy. You can get duplicates by changing ROW_NUMBER() to RANK(). Actually, RANK() is more consistent with the code in your question.
The answer by #forpas is probably the way to go but this one is a little closer to yours:
SELECT productid
FROM soldvia
GROUP BY productid
HAVING SUM(noofitems) = (
SELECT MAX(items)
FROM (
SELECT SUM(noofitems) AS items
FROM soldvia
GROUP BY productid
) x
)

Get the top 10 rows only

i want to get only the first 10 records from this select statement
SELECT SUM([Order].[Quantity]) As Quantity , [Order].ProductSKU_FK
FROM [Order]
WHERE [Order].[Status] !='Fulfilled'
GROUP BY [Order].[ProductSKU_FK]
ORDER BY Quantity DESC;
Use TOP?
SELECT TOP 10
SUM([Quantity]) As Quantity,
ProductSKU_FK
FROM [Order]
WHERE [Status] != 'Fulfilled'
GROUP BY ProductSKU_FK
ORDER BY Quantity DESC;

Create multiply function SQL

I don't know if you can call it a multiply function, or function in function
I want to create output of productname number 5,6,7,8 from the small to the big one.
this output is from the big to the small
And i want to create the reverse output , create function that output the productname 5,6,7,8 asc
and later create another function that output 5,6,7,8 order by price desc
How to do it ? thanks !
you just add column name desc order and limit to get number of record
select * from products order by unitprice desc limit 5,4
RowNumber() will fix your issue
Row Number
WITH OrderedProducts AS
(
SELECT product_id, unit_price
ROW_NUMBER() OVER (ORDER BY unit_price DESC) AS RowNumber
)
SELECT product_id, unit_price
FROM OrderedProducts
WHERE RowNumber BETWEEN 4 AND 8;
If you want to skip the first, second, third and fourth items, then you can use the NOT IN clause. Somewhat like this :
Select Top 8 product_id, price, other_fields etc from Table1 Where product_id not in (select Top 4 product_id from Table 1 where filter_goes_here Order By product_id asc) Order By Price desc

SQL Query: SELECT MAX SUM quantity

How do i combine a SUM and MAX in a single query?
Lets say i have a orderrule:
ProductID \ Quantity
I Could say:
Select ProductID,SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
ORDER BY SUM(Quantity) Desc
However that would return all sales, and not just the most sold product (with quantity).
Try this
SELECT TOP(1)
*
FROM
(
Select
ProductID,
MAX(Quantity) As MaxQuantity,
SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
)AS X
ORDER BY Sold DESC
So there are two ways to do it - first to have a limit on the number of results, something likes:
select * from (your_select) where rownum = 1
the other one is to pick the one with the the highest value, which will require a subselect, something like:
having sum(quantity) =
(select max(sum_quan) from (select sum(Quantity) from orderrule group by Product_id))
SELECT TOP 1 ProductID, Sold FROM
(
SELECT ProductID, SUM(Quantity) AS Sold
FROM Orderrule
GROUP BY ProductID
) totals
ORDER BY Sold DESC

Why getting second highest list product from this query is not working

I am using adventureworks database. I want to get product from the database whose price is second highest. I am using co-related query for this purpose. I am not quite sure what I might be missing here, in following query.
SELECT ProductID, Name, ListPrice
FROM SalesLT.Product p1
WHERE 2 =
(
SELECT COUNT(p2.ProductID)
FROM SalesLT.Product p2
WHERE p2.ListPrice >= p1.ListPrice
)
Here is the SQL Fiddle that demonstrates the following query:
SELECT TOP(1) ProductID, Name, ListPrice
FROM
(
SELECT TOP(2) ProductID, Name, ListPrice
FROM Product
ORDER BY ListPrice DESC
) mq
ORDER BY ListPrice
In the above example I am getting the top 2 records ordered by price descending. Then to get the second highest price I take those results and order by ascending and select top 1.
SQL Fiddle example of the following:
SELECT ProductID, Name, ListPrice
FROM
(
SELECT top(2) ProductID, Name, ListPrice,
ROW_NUMBER() OVER(ORDER BY ListPrice DESC) rowNum
FROM Product
) twoEntries
WHERE rowNum = 2