JOIN

JOIN is used to combine rows from two or more tables based on a related column between them. It allows you to extract data from multiple tables and present it as a single result set.

There are several types of JOIN clauses:

  • INNER JOIN: Returns only the rows that have matching values in both tables.
  • LEFT JOIN: Returns all the rows from the left table and the matching rows from the right table.
  • RIGHT JOIN: Returns all the rows from the right table and the matching rows from the left table.
  • FULL OUTER JOIN: Returns all the rows from both tables.

Examples

Here are some examples of using JOIN clauses:

sql

  1. -- Select all rows from the system_metrics table and idc_info table where the idc_id matches
  2. SELECT a.*
  3. FROM system_metrics a
  4. JOIN idc_info b
  5. ON a.idc = b.idc_id;
  6. -- Select all rows from the idc_info table and system_metrics table where the idc_id matches, and include null values for idc_info without any matching system_metrics
  7. SELECT a.*
  8. FROM idc_info a
  9. LEFT JOIN system_metrics b
  10. ON a.idc_id = b.idc;
  11. -- Select all rows from the system_metrics table and idc_info table where the idc_id matches, and include null values for idc_info without any matching system_metrics
  12. SELECT b.*
  13. FROM system_metrics a
  14. RIGHT JOIN idc_info b
  15. ON a.idc = b.idc_id;