views:

134

answers:

5

i have a database about several companies which include their name, price of share, %age change:

NAME           PRICE              % CHANGE
-------------------------------------------------
a              67                  11
b              23                  6
c              456                 5.89  
d              112                 23.98       
e              31                  17            
f              78                  3.09              
g              2678                12.56                   
h              357                 4.6

i want to select top 5 rows %age wise,means which hav maximum %age to be displayed at the top...n preceeding by jst 4 more value in desc. order...

OUTPUT SHOULD BE

NAME           PRICE              % CHANGE
-------------------------------------------------
d              112                 23.98   
e              31                  17            
g              2678                12.56  
a              67                  11   
b              23                  6
+1  A: 

Unless I am misunderstanding something... This is basic SQL:

SELECT * FROM table ORDER BY change DESC LIMIT 5;
intgr
You forgot the limit ;)
Franz
Already fixed it by the time you answered. ;)
intgr
The `LIMIT` clause isn't basic SQL - that syntax is only supported on MySQL and Postgres
OMG Ponies
I would say 'lol', but that's less than 15 characters :P
Franz
A: 

How about this?

SELECT * FROM table ORDER BY change DESC LIMIT 5

EDIT: Note that changeshould be the name of the column that you listed as % CHANGE...

Franz
A: 

Order by and limit
SELECT * FROM company ORDER BY change DESC LIMIT 5;

Cryophallion
+7  A: 

Using MySQL/Postgres:

  SELECT t.name,
         t.price,
         t.change
    FROM TABLE t
ORDER BY t.change DESC
   LIMIT 5

LIMIT clause:

Using SQL Server:

  SELECT TOP 5
         t.name,
         t.price,
         t.change
    FROM TABLE t
ORDER BY t.change DESC

TOP is supported on SQL Server 2000+ at least

Oracle:

SELECT x.*
  FROM (SELECT t.name,
               t.price,
               t.change
          FROM TABLE t
      ORDER BY t.change DESC) x
 WHERE ROWNUM <= 5

Oracle's ROWNUM

OMG Ponies
I want the Oracle syntax too! :-) Which, actually, is closest to ISO SQL 2008 (as ROW_NUMBER() is one of the ISO SQL “window functions”).
Arthur Reutenauer
Updated to provide links to documentation.
OMG Ponies
Thanks :-) I already upvoted you, so I can't do it once more.
Arthur Reutenauer
A: 
select top 5 * from tblname order by change desc
priyanka.sarkar