Sql hackerrank solution