Sql query for getting details by id