I want to get the last row, which I inserted into a table in an Oracle 11g Express database. How can I do this?
The last row according to a strict total order over composite key K(k1, ..., kn):
SELECT *
FROM TableX AS o
WHERE NOT EXISTS (
SELECT *
FROM TableX AS i
WHERE i.k1 > o.k1
OR (i.k1 = o.k1 AND i.k2 > o.k2)
...
OR (i.k1 = o.k1 AND i.k2 = o.k2 AND i.k3 = o.k3 AND ... AND i.kn > o.kn)
)
;
Given the special case where K is simple (i.e. not composite), the above is shortened to:
SELECT *
FROM TableX AS o
WHERE NOT EXISTS (
SELECT *
FROM TableX AS i
WHERE i.k1 > o.k1
)
;
Note that for this query to return just one row the key must order without ties. If ties are allowed, this query will return all the rows tied with the greatest key.