IT Questions and Answers :)

Thursday, June 3, 2021

If you wanted to retrieve all the records from TableA and only those records from TableB that matched, which query would you use?

 If you wanted to retrieve all the records from TableA and only those records from TableB that matched, which query would you use?

  • SELECT TableA.*, TableB.* FROM TableA INNER JOIN TableB ON TableA.PK = TableB.FK
  • SELECT TableA.*, TableB.* FROM TableA RIGHT JOIN TableB ON TableA.PK = TableB.FK
  • SELECT TableA.*, TableB.* FROM TableA LEFT JOIN TableB ON TableA.PK = TableB.FK
  • SELECT * FROM TableA UNION ALL SELECT * FROM TableB         

If you wanted to retrieve all the records from TableA and only those records from TableB that matched, which query would you use?

EXPLANATION

In this question we are joining TableA to TableB

An INNER JOIN will give you the results where there is a match between TableA and TableB on the join criteria.

A LEFT JOIN will return every record from the table on the left (TableA in this case) and only those records from TableB where there is a match on the join criteria.

A RIGHT JOIN does the exact opposite of the LEFT JOIN. It will return every record from the table on the right (TableB in this case) and only those records from TableA where there is a match on the join criteria.

A UNION ALL statement takes the results of two or more queries and returns them as one dataset. The only caveat is that the columns being returned must match for all queries.

SOURCE

Share:

0 comments:

Post a Comment

Popular Posts