Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the 100 lowest values in SQL?

I've been looking around for a while, and it seems it cant be found anywhere. I want to know how do you select the 100 highest and the 100 lowst values in a column? The MIN-function only chooses the lowest one and the MAX the highest one.

Anyone out there who knows how you do this?

like image 719
SterlinkArcher Avatar asked Sep 05 '13 06:09

SterlinkArcher


People also ask

How do you select the least value in SQL?

The MIN() function returns the smallest value of the selected column.

How do you order from lowest to highest in SQL?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.

What is select top 100 percent SQL Server?

So if we see TOP (100) PERCENT in code, we suspect that the developer has attempted to create an ordered view and we check if that's important before going any further. Chances are that the query that uses that view (or the client application) need to be modified instead.

How do you select the top 3 maximum value in SQL?

To get the maximum value from three different columns, use the GREATEST() function. Insert some records in the table using insert command. Display all records from the table using select statement.


4 Answers

SQL Server

  • Top 100 Highest

    SELECT TOP 100 * FROM MyTable
    ORDER BY MyCol DESC
    
  • Top 100 Lowest

    SELECT TOP 100 * FROM MyTable
    ORDER BY MyCol ASC
    

MySQL

  • Top 100 Highest

    SELECT * FROM MyTable
    ORDER BY MyCol DESC LIMIT 100
    
  • Top 100 Lowest

    SELECT * FROM MyTable
    ORDER BY MyCol ASC  LIMIT 100
    
like image 60
Himanshu Jansari Avatar answered Oct 23 '22 15:10

Himanshu Jansari


You can do it as below,

Highest

select * from
tablename
order by
column DESC
limit 0,100

Lowest

select * from
tablename
order by
column ASC
limit 0,100

EDIT

For SQL Server replace select * from with select TOP 100 * from

The SELECT TOP clause is used to specify the number of records to return.

like image 22
Dipesh Parmar Avatar answered Oct 23 '22 15:10

Dipesh Parmar


Use sorting in ascending and descending order and limit output to 100

like image 1
Umang Mehta Avatar answered Oct 23 '22 14:10

Umang Mehta


if you use Sql server you can order query desc and select top 1000 like :

select top(1000) * from mytable order by value desc
like image 1
msm2020 Avatar answered Oct 23 '22 13:10

msm2020