(DDL) Create / Alter / Drop / Rename Table
(DDL) Create / Alter / Drop / Rename Table
(DDL) Create / Alter / Drop / Rename Table
COM
SQL
create table test_datatype(
col1 byteint,
col2 smallint,
col3 integer,
col4 bigint,
col5 char(10),
col6 varchar(10),
col7 date,
col8 timestamp,
col9 time,
col10 decimal(10, 2),
col11 number(10, 2),
col12 float
);
SQL
create table test_datatype2(
col1 byteint,
col2 smallint,
col3 integer,
col4 bigint,
col5 char(10),
col6 varchar(10),
col7 date,
col8 timestamp,
col9 time,
col10 decimal(10, 2),
col11 number(10, 2),
col12 float
) primary index(col2);
SQL
create table test_datatype3(
col1 byteint,
col2 smallint,
col3 integer,
col4 bigint,
col5 char(10),
col6 varchar(10),
col7 date,
col8 timestamp,
col9 time,
col10 decimal(10, 2),
col11 number(10, 2),
col12 float
) NO primary index;
SQL
create table test_ctas1 as test_datatype with no data;
5 CREATE table from other table with no-data but different PRIMARY
INDEX
SQL
create table test_ctas2 as test_datatype with no data primary index (col2);
SQL
create table test_ctas3 as test_datatype with data;
7 CREATE table from other table with data but different PRIMARY INDEX
SQL
create table test_ctas4 as test_datatype with data primary index (col2);
SQL
create table test_ctas5 as (
SELECT
w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE
WHERE
w_state = 'KS'
) with no data;
9 CREATE table from SELECT with no data but different PRIMARY INDEX
SQL
create table test_ctas6 as
SELECT
w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE
WHERE
w_state = 'KS' with no data primary index (w_warehouse_id);
SQL
create table test_ctas7 as (
SELECT
w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE
WHERE
w_state = 'KS'
) with data;
11 CREATE table from SELECT with data but different PRIMARY INDEX
SQL
create table test_ctas8 as (
SELECT
w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE
WHERE
w_state = 'KS'
) with data primary index (w_warehouse_id);
SQL
create table test_ctas9 as (
SELECT
w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE
WHERE
1=0
) with no data;
SQL
create VOLATILE table vt_datatype(
col1 byteint,
col2 smallint,
col3 integer,
col4 bigint,
col5 char(10),
col6 varchar(10),
col7 date,
col8 timestamp,
col9 time,
col10 decimal(10, 2),
col11 number(10, 2),
col12 float
) primary index(col2) ON COMMIT PRESERVE ROWS;
SQL
create GLOBAL TEMPORARY table gt_datatype(
col1 byteint,
col2 smallint,
col3 integer,
col4 bigint,
col5 char(10),
col6 varchar(10),
col7 date,
col8 timestamp,
col9 time,
col10 decimal(10, 2),
col11 number(10, 2),
col12 float
) primary index(col2);
SQL
ALTER table
warehouse
ADD
w_warehouse_new varchar(30),
ADD
w_warehouse_new2 varchar(30);
SQL
ALTER table
warehouse
DROP
w_warehouse_new,
DROP
w_warehouse_new2;
SQL
RENAME TABLE warehouse to warehouse_new;
SQL
DROP
TABLE warehouse;
SELECT
20 SELECT all columns and all rows from table
SQL
SELECT
*
from
WAREHOUSE;
SQL
SELECT
w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE;
SQL
SELECT
*
from
WAREHOUSE sample 10;
23 SELECT all columns and few rows from table – another approach
SQL
SELECT
Top 10 *
from
WAREHOUSE;
SQL
SELECT
*
FROM
WAREHOUSE
WHERE
w_state = 'KS';
SQL
SELECT
*
FROM
WAREHOUSE
WHERE
w_state = 'TN'
and w_city = 'Fairview';
26 SELECT all columns and FILTER rows , multiple conditions (OR) from
table
SQL
SELECT
*
FROM
WAREHOUSE
WHERE
w_state = 'TN'
OR w_state = 'VA';
SQL
SELECT
*
FROM
WAREHOUSE
WHERE
w_state IN ('TN', 'VA');
28 SELECT DISTINCT rows from table
SQL
SELECT
DISTINCT *
from
WAREHOUSE;
SQL
SELECT
DISTINCT w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE;
SQL
SELECT
w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE
ORDER BY
w_warehouse_id asc,
w_city desc;
SQL
SELECT
w_warehouse_sk,
w_warehouse_id,
w_city
FROM
WAREHOUSE
ORDER BY
2 asc,
3 desc;
SQL
SELECT
*
FROM
WAREHOUSE
WHERE
w_state IS NOT NULL
and w_city is NULL;
SQL
SELECT
MAX(w_warehouse_sq_ft),
MIN(w_warehouse_sq_ft),
COUNT(w_warehouse_sq_ft),
AVG(w_warehouse_sq_ft),
SUM(w_warehouse_sq_ft)
FROM
WAREHOUSE;
SQL
SELECT
COUNT(*)
FROM
WAREHOUSE;
SQL
SELECT
COUNT(*)
FROM
(
select
distinct *
from
WAREHOUSE
) tb1;
SQL
SELECT
*
FROM
WAREHOUSE
WHERE
w_state like 'T % ';
SQL
select
*
from
date_dim
where
d_date between '2019 - 01 - 01'
and '2019 - 01 - 31';
SQL
SELECT
w_warehouse_sk as w_sk,
w_warehouse_id as w_id,
w_city as w_city
FROM
WAREHOUSE;
SQL
SELECT
tb1.w_warehouse_sk,
tb1.w_warehouse_id,
tb1.w_city
FROM
WAREHOUSE tb1;
SQL
SELECT
w_warehouse_sk as w_sk,
w_warehouse_id as w_id,
COALESCE(w_city, 'Not Available') as w_city
FROM
WAREHOUSE;
42 Use CASE to create new derived table
SQL
SELECT
w_warehouse_sk as w_sk,
w_warehouse_id as w_id,
w_city as w_city,
CASE when w_state = 'California' then 'CA' when w_state = 'Florida' then 'FL' when w_state =
'Michigan' then 'MI' when w_state = 'New Jersey' then 'NJ' when w_state = 'New York' then 'NY'
when w_state = 'Texas' then 'TX' when w_state = 'Virginia' then 'VA' else 'Other States' END as
w_state_abbr
FROM
WAREHOUSE;
43 INNER JOIN
SQL
select
tb2.d_date,
tb1.ss_quantity,
tb1.ss_wholesale_cost,
tb1.ss_list_price,
tb1.ss_sales_price
from
store_sales tb1
INNER JOIN date_dim tb2 on tb1.ss_sold_date_sk = tb2.d_date_sk
where
tb2.d_date between '2020 - 01 - 01'
and '2020 - 12 - 31';
SQL
select
tb2.d_date,
tb1.ss_quantity,
tb1.ss_wholesale_cost,
tb1.ss_list_price,
tb1.ss_sales_price
from
store_sales tb1
LEFT OUTER JOIN date_dim tb2 on tb1.ss_sold_date_sk = tb2.d_date_sk
where
tb2.d_date between '2020 - 01 - 01'
and '2020 - 12 - 31';
SQL
select
tb2.d_date,
tb1.ss_quantity,
tb1.ss_wholesale_cost,
tb1.ss_list_price,
tb1.ss_sales_price
from
store_sales tb1
RIGHT OUTER JOIN date_dim tb2 on tb1.ss_sold_date_sk = tb2.d_date_sk
where
tb2.d_date between '2020 - 01 - 01'
and '2020 - 12 - 31';
SQL
select
tb2.d_date,
tb1.ss_quantity,
tb1.ss_wholesale_cost,
tb1.ss_list_price,
tb1.ss_sales_price
from
store_sales tb1 FULL
OUTER JOIN date_dim tb2 on tb1.ss_sold_date_sk = tb2.d_date_sk
where
tb2.d_date between '2020 - 01 - 01'
and '2020 - 12 - 31';
47 SELF JOIN
SQL
select
tb1.w_warehouse_sk,
tb1.w_warehouse_name
from
warehouse tb1,
warehouse tb2
where
tb1.w_warehouse_id = tb2.w_warehouse_id
and tb1.w_state = tb2.w_state
and tb1.w_warehouse_sk = 1;
48 LEFT OUTER JOIN to find rows which does not exists in LEFT table
SQL
select
tb1.w_warehouse_name,
_tb1.w_state,
tb1.w_city
from
warehouse tb1
left outer join inventory tb2 on tb1.w_warehouse_sk = tb2.inv_warehouse_sk
where
tb2.inv_warehouse_sk is null;
49 LEFT OUTER JOIN to find rows which does exists in LEFT table (same
as INNER JOIN)
SQL
select
tb1.w_warehouse_name,
_tb1.w_state,
tb1.w_city
from
warehouse tb1
left outer join inventory tb2 on tb1.w_warehouse_sk = tb2.inv_warehouse_sk
where
tb2.inv_warehouse_sk is not null;
SQL
select
w_warehouse_name,
w_state,
w_city,
row_number() over(
partition by w_zip
order by
w_warehouse_sq_ft desc
) as w_warehouse_no
from
warehouse;
SQL
select
w_warehouse_name,
w_state,
w_city,
rank() over(
partition by w_zip
order by
w_warehouse_sq_ft desc
) as w_warehouse_no
from
warehouse;
SQL
SELECT
w_warehouse_sk as w_sk,
w_warehouse_id as w_id,
w_city as w_city
FROM
WAREHOUSE
where
w_state = 'Virginia'
UNION
SELECT
w_warehouse_sk as w_sk,
w_warehouse_id as w_id,
w_city as w_city
FROM
WAREHOUSE
where
w_state = 'Texas';
SQL
SELECT
CURRENT_DATE;
SQL
SELECT
CURRENT_DATE + INTERVAL '5' DAY;
SQL
SELECT
CURRENT_DATE + INTERVAL '5' MONTH;
SQL
SELECT
CURRENT_DATE + INTERVAL '5' YEAR;
57 Subtract 2 dates
SQL
SELECT
cast('2019 - 12 - 31' as date) – cast('2019 - 04 - 01' as date);
SQL
select
*
from
dbc.tablesV
where
tablename = 'warehouse'
and databasename = 'tpcds';
SQL
select
*
from
dbc.tablesV
where
databasename = 'tpcds'
and tablekind in ('T', 'O');
SQL
HELP database tpcds;
SQL
SHOW table warehouse;
SQL
HELP table warehouse;
SQL
select
databasename,
tablename,
sum(currentperm) as total_size_bytes
from
dbc.tablesize
where
tablename = 'warehouse'
and databasename = 'tpcds'
group by
databasename,
tablename;
SQL
select
*
from
dbc.tablesV
where
databasename = 'tpcds'
and tablekind = 'V';
SQL
select
*
from
dbc.dbqlogtbl
where
sessionid = (
select
session
);
68 Explain plan for the Query
SQL
EXPLAIN
select
tb2.d_date,
tb1.ss_quantity,
tb1.ss_wholesale_cost,
tb1.ss_list_price,
tb1.ss_sales_price
from
store_sales tb1
INNER JOIN date_dim tb2 on tb1.ss_sold_date_sk = tb2.d_date_sk
where
tb2.d_date between '2020 - 01 - 01'
and '2020 - 12 - 31';
SQL
INSERT into employee(
Name, City, County, State, Zip, Country
)
values
(
'Mark', 'Paris', 'NA',
'NA', 75000, 'France'
);
SQL
INSERT into employee(
Name, City, County, State, Zip, Country
)
SELECT
Name,
City,
County,
State,
Zip,
Country
from
stg_employee
where
zip = 75000
and country = 'France';
SQL
UPDATE
employee
set
Country = 'France',
City = 'Paris'
where
zip = 75000;
SQL
UPDATE
tb1
from
employee tb1,
dim_add tb2
set
Country = tb2.Country,
City = tb2.City
where
tb1.zip = tb2.zip;
SQL
UPDATE
tb1
from
employee tb1,
(
select
country,
city,
zip
from
dim_add
where
zip between 75000
and 75020
) tb2
set
Country = tb2.Country,
City = tb2.City
where
tb1.zip = tb2.zip;
SQL
UPDATE
employee
set
Country = 'France',
City = 'Paris'
where
zip = 75000 ELSE INSERT into employee(
Name, City, County, State, Zip, Country
)
values
(
'Mark', 'Paris', 'NA',
'NA', 75000, 'France'
);
SQL
MERGE into employee tb1 using stg_employee tb2 on tb1.zip = tb2.zip WHEN MATCHED THEN
UPDATE
SET
Country = tb2.Country,
City = tb2.City WHEN NOT MATCHED THEN INSERT
VALUES
(
tb2.Name, tb2.City, tb2.County, tb2.State,
tb2.Zip, tb2.Country
);
SQL
DELETE from
employee;
SQL
DELETE from
employee
where
zip = 75000;
SQL
DELETE FROM
employee
where
zip = stg_employee.zip;
If you are working on Teradata IMPORT scripts , you may want to check our free web utility to
generate:
1. BTEQ Import
2. Fastload
3. Multiload
4. TPT Load
https://etl-sql.com/td-import-script-generator/