Thoughtworks interview question

Write SQL queries and pseudo code