top-n

SQL Server: Why do these queries return different result sets ???

Query 1 = select top 5 i.item_id from ITEMS i Query 2 = select top 5 i.item_id, i.category_id from ITEMS i Even if I remove the top 5 clause they still return different rows. if I run "select top 5 i.* from ITEMS i" this returns a completely different result set !! ...

predefined function or method available to get second highest salary from an employee table?

is there any predefined function or method available to get second highest salary from an employee table ...

Oracle SELECT TOP 10 records

Hi, i have an big problem with an SQL Statement in Oracle. I want to select The TOP 10 Records ordered by STORAGE_DB wich arent in a list from an other select statement. This one works fine for all records: SELECT DISTINCT APP_ID, NAME, STORAGE_GB, HISTORY_CREATED, TO_CHAR(HISTORY_DATE, 'DD.MM.YYYY') AS HISTORY_DATE ...

N Top Record Selection Based on Own SQL Statement in MS-Access

I'm re-writing a small ms-access application to take examinations on. What they want is for the tests to grab a set of random questions based on how large the exam's size is. If each exam was a set number of questions, I could just stick the number in the TOP statement and be done with it, but there are a variable number of questions ...

How can I select the "maximum" row from a table?

How can I select the maximum row from a table? What does maximum mean -- well my table has two timestamp columns, TIME1 and TIME2. The maximum column is the one with the latest value for TIME1. If that is not a unique row, then the maximum is the one within those rows with the latest value for TIME2. This is on Oracle if that matters...

Select top N with "for update skip locked" in Oracle

In Oracle, I can select the top 1 message in a sorted table with select messageid from( select messageid, RANK() over (order by messageid asc) as msg_rank from messages ) where msg_rank=1; And as I discovered in a previous question I can select a row exclusively with select * from messages where rownum < 2 ...