[Solution] Create a combined table

Click here to view the question


Solution:

Use a union to combine records vertically / add rows from two separate tables together. The combined tables must have the same number of columns with the same data types.

We also added an order by employee_id to control the ordering of the output. Note that order by clauses must be added to the last select statement of a union clause.

select * from managers
union
select *, null from associates
order by employee_id

Concepts Covered: Unions

Explanation:

Unions vertically append rows from separate tables. Columns must match up in order by data type. In the example above, we have an integer, followed by a string, followed by an integer, followed by another integer field. The associates table being appended onto the managers table must also have the same format. Since we are missing a number_reports field from the associates table, we use "null" as a stand in

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
Like Follow
  • 5 mths agoLast active
  • 140Views
  • 1 Following