SQL with Jupyter Notebook Tutorial

50 / 61

Temporary Tables

  • Used to store temporary data.
  • Table and data are deleted when the current client session terminates.
  • Not listed in the output of SHOW TABLES command.

Example 1:

Let us create a temporary table named salessummary_<<your lab username>>.

%%sql

drop temporary table if exists salessummary_<<your lab username>>;

create temporary table salessummary_<<your lab username>> (
   product_name varchar(50) not null,
   total_sales decimal(12,2) not null default 0.00
);

Let us insert some sample sales data for car sales:

%%sql

insert into salessummary_<<your lab username>>(product_name, total_sales) values('Maruti Dzire', 191);

insert into salessummary_<<your lab username>>(product_name, total_sales) values('Hyundai i20', 173);

insert into salessummary_<<your lab username>>(product_name, total_sales) values('Honda City', 189);

Let us select the table to see the data:

%%sql

select * from  salessummary_<<your lab username>>;

Example 2:

We can also create and insert data directly using the select statement. Let us see such an example:

We shall create a table named deptt_clone_<<your lab username>>. It contains the same data as the deptt_<<your lab username>>.

%%sql

drop temporary table if exists deptt_clone_<<your lab username>>; 

create temporary table deptt_clone_<<your lab username>>(
     select * from  deptt_<<your lab username>>
);

select * from deptt_clone_<<your lab username>>;

Finally:

Log out of the session or log back in. Try to select the data. You will see there is no such table or data, becuase the above are temporary tables, designed and used for storing data temporarily..


No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...