SQL Query -- fetch first and last rows from a table