How to find the top 5 values using max() in sql

sql where max value
sql max date
sql min and max in same query
select top 3 max values sql
sql select 10 highest values
find 3rd highest salary in sql w3schools
sql top
order by sql

I want the top 5 products from a table

If i use this

SELECT MAX(ProductCode) AS ProductName 
FROM OrderDetails

I get only one result I want the top 5 result

Try this for MySQl

SELECT ProductCode FROM OrderDetails
ORDER BY ProductCode Desc 
LIMIT 5

MySQL MAX Function, The MAX() function comes in handy in many cases such as finding the greatest number, the most expensive product, and the largest payment from customers. SQL HOME SQL Intro SQL Syntax SQL Select SQL Select Distinct SQL Where SQL And, Or, Not SQL Order By SQL Insert Into SQL Null Values SQL Update SQL Delete SQL Select Top SQL Min and Max SQL Count, Avg, Sum SQL Like SQL Wildcards SQL In SQL Between SQL Aliases SQL Joins SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL Self Join SQL

solved..by using this query

SELECT TOP 5 ProductName, COUNT(ProductName) AS value_occurrence FROM OrderDetails GROUP BY ProductName ORDER BY value_occurrence DESC

SQL SELECT TOP, LIMIT, ROWNUM, The MAX() function returns the largest value of the selected column. 48 - 6 oz jars, 22. 5, Chef Anton's Gumbo Mix, 2, 2, 36 boxes, 21.35 Use the MIN function to select the record with the smallest value of the Price column. Get Certified »​  In this article we have discussed how SQL HAVING CLAUSE can be used along with the SQL MAX() to find the maximum value of a column over each group. The SQL HAVING CLAUSE is reserved for aggregate function. The usage of WHERE clause along with SQL MAX() have also described in this page. The SQL IN OPERATOR which checks a value within a set of

it depends on what exactly you need top 5 based on .. it can be sorting by what it can be like numbers first and then the characters one .. as you said it is varchar then please have a look at this question and limit them with how many u need.

How do I sort a VARCHAR column in SQL server that contains numbers?

Hope this helps :)

SQL MIN() and MAX() Functions, Similar to the MIN() function, the MAX() function ignores NULL values and The following statement uses the MAX() function to find the highest list price of all  Select Rows with Maximum Value on a Column Example 2. In this example, we will show how to select rows with max value along with remaining columns. It is useful if you want to return the remaining columns (non-group by columns). For this SQL Server example, we used the Inner Join to join the employee table with itself.

SQL Server MAX Function Demonstrated By Practical Examples, Finding the minimum and maximum values for particular column can help you use the max and the min functions to find the maximum and minimum values. MAX() with Count function. In this part, you will see the usage of SQL COUNT() along with the SQL MAX(). Example: To get the maximum number of agents as column alias 'mycount' from the 'orders' table with the following condition -

Getting Minimum and Maximum Values, MAX sql command will return the record with maximum or highest value in the SQL table. Same way we can get the minimum value of a range of records by using SQL We stored these number in t1 ( VARCHAR ) column , 1,2,3,4,5,6,12,​13 was formatted a VARCHAR,therefore trying to return a max() on this field was  I want to select the top 5 most hit pages from the database, but can't seem to find the right method to do so using just SQL. In particular I'm looking for one which doesn't involve selecting every entry and scanning through it afterwards using PHP.

SQL MAX command: MySQL on Maximum or highest value in a field, This function is useful to determine the largest of all selected values of a column. Syntax: SQL MAX() with addition of two columns. To get the  However, we should avoid using solutions based on row number unless the business logic is happy to arbitrarily truncate the result set in the event of a tie. There is a difference between asking for the five highest values and the first five records sorted by high values. There is also a non-analytic solution using the ROWNUM pseudo-column.

Comments
  • Please provide sample data, desired results, and a definition of what "most ordered" means. I have removed the incompatible database tags.
  • tag you dbms name
  • I don't understand how a column named "ProductCode" is any indication on how often that product was ordered.
  • Perhaps it should be top 5 ordered by count(distinct productCode), but without more information, any attempt to answer this question is nothing but a guess.
  • Please read the first paragraph of the sql tag info and edit your question accordingly.
  • my product code is varchar by using this query may i get the exact reult that i want?
  • in this query you seperated OrderDetails by a space, so i joined them
  • its not what i wanted.. from my table i want to figure out the top 5 products that orderd by the customers.. its orderdetail table its contain products orderd by diffrent customers only i can figure out by the largest product from the list by its count
  • I gave you solution according to your question. But you are asking something else, However I still couldn't undestand what you need. Please provider sample data and expected output.