Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to perform 'SELECT TOP X FROM TABLE' type queries with DB2 / dashDB

Tags:

dashdb

I would like to perform the equivalent of SELECT TOP 1 ... query in db2 / dashDB:

 SELECT TOP 1 * FROM customers

How can I achieve this?

like image 311
Chris Snow Avatar asked Jun 12 '15 13:06

Chris Snow


People also ask

How do I SELECT top 10 rows in IBM Db2?

1) Using Db2 FETCH clause to get the top-N rows The ORDER BY clause sorts books by ratings from high to low. The FETCH clause picks only the first 10 rows, which have the highest ratings.

How do I limit rows in Db2?

You can specify the fetch clause in a SELECT statement to limit the number of rows in the result table of a query.

Which query is used to find the number of rows in a given table in Db2?

The SQL COUNT() function returns the number of rows in a table satisfying the criteria specified in the WHERE clause. It sets the number of rows or non NULL column values.


2 Answers

You can achieve this query using the FETCH FIRST x ROWS ONLY statement, E.g.

SELECT * FROM customers FETCH FIRST 1 ROWS ONLY
like image 165
Chris Snow Avatar answered Oct 22 '22 02:10

Chris Snow


Another way on dashDB, and more easy for my opinion is to use the 'limit n', E.g.

SELECT * FROM customers LIMIT 1
like image 37
AVP Avatar answered Oct 22 '22 03:10

AVP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!