how to use distinct in sql with join two tables