R and SQL queries