I need to do a query to return the next (or prev) record if a certain record is not present. For instance consider the following table:
ID (primary key) value
1 John
3 Bob
9 Mike
10 Tom.
I'd like to query a record that has id 7 or greater if 7 is not present.
My questions are,
- Are these type of queries possible with SQL?
- What are such queries called in the DB world?
Thanks!
Yes, it's possible, but implementation will depend on your RDBMS.
Here's what it looks like in MySQL, PostgreSQL and SQLite:
In MS SQL-Server, Sybase and MS-Access:
In Oracle:
In Firebird and Informix:
In DB/2 (this syntax is in SQL-2008 standard):
In those RDBMS that have "window" functions (in SQL-2003 standard):
And if you are not sure which RDBMS you have: