Câu hỏi phỏng vấn SQL
Câu hỏi

What is the difference between INNER JOIN and OUTER JOIN ?

Câu trả lời

The difference between INNER JOIN and OUTER JOIN in SQL lies in how they handle the rows from the tables being joined and the results they produce.

INNER JOIN

  • Definition: An INNER JOIN returns only the rows that have matching values in both tables. It combines rows from two or more tables based on a related column between them.
  • Result Set: Only the rows that meet the join condition from both tables are included in the result set. If a row in one table does not have a matching row in the other table, that row will not be included.
  • Syntax:
    sql Copy
    SELECT columns
    FROM table1
    INNER JOIN table2
    ON table1.column_name = table2.column_name;
  • Performance: Generally faster than OUTER JOINs because it only processes matching rows[1][4][6].

OUTER JOIN

  • Definition: An OUTER JOIN returns all rows from one table and the matching rows from another table. If there is no match, NULL values are included for columns from the non-matching table.
  • Types:
    • LEFT OUTER JOIN: Returns all rows from the left table and the matched rows from the right table. If no match is found, NULL values are returned for columns from the right table.
    • RIGHT OUTER JOIN: Returns all rows from the right table and the matched rows from the left table. If no match is found, NULL values are returned for columns from the left table.
    • FULL OUTER JOIN: Returns all rows when there is a match in either left or right table. If there is no match, NULL values are included for columns from the non-matching table.
  • Syntax:
    sql Copy
    -- LEFT OUTER JOIN
    SELECT columns
    FROM table1
    LEFT OUTER JOIN table2
    ON table1.column_name = table2.column_name;
    
    -- RIGHT OUTER JOIN
    SELECT columns
    FROM table1
    RIGHT OUTER JOIN table2
    ON table1.column_name = table2.column_name;
    
    -- FULL OUTER JOIN
    SELECT columns
    FROM table1
    FULL OUTER JOIN table2
    ON table1.column_name = table2.column...
middle

middle

Gợi ý câu hỏi phỏng vấn

junior

What is DEFAULT ?

middle

What is the difference between WHERE clause and HAVING clause?

middle

What is the difference between INNER JOIN , OUTER JOIN , FULL OUTER JOIN ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào