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

What is the difference between JOIN and UNION ?

Câu trả lời

To answer the interview question "What is the difference between JOIN and UNION in SQL?", you can provide the following detailed explanation:

Difference Between JOIN and UNION in SQL

JOIN

  1. Purpose: The JOIN clause is used to combine rows from two or more tables based on a related column between them. It is typically used to retrieve data that is spread across multiple tables.
  2. Result Structure: JOIN combines data into new columns. The result set includes columns from both tables that are joined.
  3. Types of JOINs: There are several types of JOINs, including:
    • INNER JOIN: Returns only the rows that have matching values in both tables.
    • LEFT JOIN (or 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 JOIN (or 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 JOIN (or FULL OUTER JOIN): Returns all rows when there is a match in either left or right table. Rows without a match in one of the tables will have NULL values for columns from that table.
  4. Column Requirements: The number of columns selected from each table may not be the same, and the data types of corresponding columns can be different.
  5. Duplicates: JOINs may return duplicate rows if the tables contain duplicate values.

UNION

  1. Purpose: The UNION clause is used to combine the result sets of two or more SELECT statements. It is used to append rows from one result set to another.
  2. Result Structure: UNION combines data into new rows. The result set includes rows from both SELECT statements.
  3. Types of UNIONs:
    • UNION: Returns distinct rows from both SELECT statements, removing duplicates.
    • UNION ALL: Returns all rows from both SELECT statements, including duplicates.
  4. **...
middle

middle

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

senior

What is the difference among UNION , MINUS and INTERSECT ?

entry

What is PRIMARY KEY ?

entry

Define a Temp Table

Bình luận

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

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