Extract a sample of records from a database while ensuring referential integrity