Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sql – does join create a new table?

when I use join in mysql (or sql) does it create a new table?
Or does it create a virtual table like the view command?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Each query result is a table. It consists of columns and rows and can be treated like any other table in SQL. E.g.:

select *
from departments
join
(
  select department_id, count(*) as number_of_employees
  from employees
  group by department_id
) department_info using (department_id);

Here we create a table we call department_info in our query and join this table to the existing departments table. This creates another table now consisting of departments plus the number of employees in it. This is the query result we show.

It is a query (select ...) which creates a table. The join is just a part of it.

These tables, however, are only temporary. While departments and employees in above example are stored tables, department_info and the final query result are not. They don’t get stored. If you want to store a query’s result table, use CREATE TABLE AS. E.g.:

create table department_employees as
select *
from departments
join
(
  select department_id, count(*) as number_of_employees
  from employees
  group by department_id
) department_info using (department_id);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading