

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write the DB2 SQL query to find the third highest ORDER_TOTAL in a ORDERS DB2 table
We can find the third highest ORDER_TOTAL in the ORDERS DB2 table using the below query.
Example
SELECT ORDER_ID, MIN(ORDER_TOTAL) FROM ORDERS ORDER BY ORDER_TOTAL DESC FETCH FIRST 3 ROWS ONLY
The ‘FETCH FIRST 3 ROWS ONLY’ clause will give only 3 rows in the output and these 3 rows will be in descending order. The first row will have the highest ORDER_TOTAL in the entire ORDERS table, second row will have the second highest ORDER_TOTAL in the entire ORDERS table and so on.
The MIN aggregate function will give the least value of the ORDER_TOTAL among those 3 rows and this will be our third highest ORDER_TOTAL.
For example, if we have below ORDERS table in DB2.
ORDER_ID | ORDER_TOTAL | ORDER_DATE |
Z22345 | 342 | 29-07-2020 |
Z62998 | 543 | 30-07-2020 |
Z56990 | 431 | 12-07-2020 |
Z56902 | 6743 | 29-07-2020 |
Z99781 | 443 | 10-07-2020 |
Z56112 | 889 | 30-07-2020 |
The described query will return the following result.
ORDER_ID | ORDER_TOTAL |
Z62998 | 543 |
- Related Questions & Answers
- Write a DB2 query to find out all the duplicate INVOICE_ID in ORDERS DB2 table?
- Write a SQL query to count the number of duplicate TRANSACTION_ID in an ORDERS DB2 table
- Write the syntax to declare a scrollable cursor on the ORDERS DB2 table.
- How will you find the ORDER_ID of all the orders having ORDER_TOTAL greater than the average of ORDER_TOTAL in ORDER's DB2 table
- How to find the primary key of a DB2 table TAB1?
- How will you create a new TRIGGER on the ORDERS DB2 table? Give the syntax of TRIGGER
- How to get the list of all COBOL-DB2 programs using a DB2 table TAB1?
- How to find all the foreign keys of a DB2 table TAB1?
- How to find out all the indexes for a DB2 table TAB1?
- How can you revert all the DB2 table changes done in a COBOL-DB2 program?
- How to delete a DB2 table TAB1?
- Implementation of a table level locks in a COBOL-DB2 program during SQL execution
- How to store a NULL value in a particular column of a DB2 table using COBOL-DB2 program?
- How to image copy the entire DB2 table TAB1 into a dataset?
- How to find the access path selected by an optimizer for a SQL statement used in a DB2 program?
Advertisements