SlideShare uma empresa Scribd logo
1 de 31
SQL
Adilson Mendonca
select i_ITEM_id,i_item_desc,i_category,i_class,i_CURRENT_price, sum(cs_EXT_sales_price) as
itemrevenue, sum(cs_ext_sales_price) *100/sum(sum(cs_ext_sales_price)) over (partition by
I_class) as revenueratio
From catalog_sales,item,date_dim
where cs_item_sk = i_item_sk AND i_category in ('Jewelry', 'Sports', 'Books')
and cs_sold_date_sk = d_date_sk and cast(d_date as timestamp) between cast('2001-01-12' as
timestamp)
and (cast('2001-01-12' as timestamp) + interval 30 days)
group by i_item_id,i_item_desc,i_category,i_class,i_current_price
order by i_category,i_class,i_item_id,i_item_desc,revenueratio
limit 100;
Code Visibility
select
i_ITEM_id,
I_item_desc,
I_category,
I_class,
i_CURRENT_price,
sum(cs_EXT_sales_price) as itemrevenue,
sum(cs_ext_sales_price) *100/sum(sum(cs_ext_sales_price))
over (partition by I_class) as revenueratio
FROM catalog_sales,item,date_dim
where cs_item_sk = i_item_sk
AND i_category in ('Jewelry', 'Sports', 'Books')
and cs_sold_date_sk = d_date_sk
and cast(d_date as timestamp) between cast('2001-01-12' as timestamp)
and (cast('2001-01-12' as timestamp) + interval 30 days)
group by i_item_id,i_item_desc,i_category,i_class,i_current_price
order by i_category,i_class,i_item_id,i_item_desc,revenueratio
limit 100;
select i_ITEM_id,
I_item_desc,
I_category,
I_class,
i_CURRENT_price,
sum(cs_EXT_sales_price) as itemrevenue,
sum(cs_ext_sales_price) *100/sum(sum(cs_ext_sales_price))
over (partition by I_class) as revenueratio
FROM catalog_sales,item,date_dim
where cs_item_sk = i_item_sk
AND i_category in ('Jewelry', 'Sports', 'Books')
and cs_sold_date_sk = d_date_sk
and cast(d_date as timestamp)
between cast('2001-01-12' as timestamp)
and (cast('2001-01-12' as timestamp) + interval 30 days)
group by i_item_id,
I_item_desc,
I_category,
I_class,
i_current_price
order by i_category,
I_class,
I_item_id,
I_item_desc,
revenueratio
limit 100;
SELECT i_item_id,
,i_item_desc
,i_category
,i_class
,i_current_price
,SUM(cs_ext_sales_price) AS item_revenue
,SUM(cs_ext_sales_price) * 100
/ SUM(SUM(cs_ext_sales_price))
OVER (PARTITION BY i_class) AS revenue_ratio
FROM catalog_sales
JOIN item ON cs_item_sk = i_item_sk
JOIN date_dim ON cs_sold_date_sk = d_date_sk
WHERE i_category IN ('Jewelry', 'Sports', 'Books')
AND CAST(d_date AS TIMESTAMP)
BETWEEN CAST('2001-01-12' AS TIMESTAMP)
AND CAST('2001-01-12' AS TIMESTAMP) + INTERVAL 30 DAYS
GROUP BY 1, 2, 3, 4, 5
ORDER BY 3, 4, 1
LIMIT 100
SELECT item.i_item_id,
item.i_item_desc,
item.i_category,
item.i_class,
item.i_current_price,
SUM(catalog_sales.cs_ext_sales_price) AS item_revenue,
SUM(catalog_sales.cs_ext_sales_price) * 100
/ SUM(SUM(catalog_sales.cs_ext_sales_price))
OVER (PARTITION BY item.i_class) AS revenue_ratio
FROM catalog_sales
JOIN item
ON catalog_sales.cs_item_sk = item.i_item_sk
JOIN date_dim
ON catalog_sales.cs_sold_date_sk = date_dim.d_date_sk
WHERE item.i_category IN ('Jewelry', 'Sports', 'Books')
AND CAST(date_dim.d_date AS TIMESTAMP)
BETWEEN CAST('2001-01-12' AS TIMESTAMP)
AND CAST('2001-01-12' AS TIMESTAMP) + INTERVAL 30 DAYS
GROUP BY 1, 2, 3, 4, 5
ORDER BY 3, 4, 1
LIMIT 100
Over commenting
-- get id, name and open only once
SELECT DISTINCT t1.id , t1.name , t3.open
-- select names from table 1, order them by date and return first 3
FROM (SELECT id , name FROM table1 ORDER BY date DESC LIMIT 3) AS t1
-- get open from table 2 IF id is there, open = 1 and type =2
LEFT JOIN (
( SELECT open , name_id FROM table2 WHERE open=1 AND type=2 ) AS t3 )
ON t1.id = t3.name_id
-- order by name from A-Z
ORDER BY t1.name ASC
Follow Patterns
Indentation
Don’t over comment - just clear code
Remove commented lines of code
Use alias on all column when joining
Know your PKs and Unique keys
Make easy future maintenance
Execution time
JUST select the tables/columns which be in USE
Code block
WITH
patient_data AS
(SELECT patient_id,
patient_name,
hospital,
drug_dosage
FROM hospital_registry
WHERE (COALESCE(last_visit,NOW()) > NOW() - INTERVAL '14 days')
AND city = "Los Angeles"
),
average_dosage AS
(SELECT hospital,
AVG(drug_dosage) AS Average
FROM patient_data
GROUP BY hospital
)
SELECT count(hospital)
FROM average_dosage
WHERE drug_dosage > 1000
Master the use of:
Functions
Window Functions (OLAP functions)
CTE - Common Table Expression
Views
UDF - User Defined Function
Lost
Data Structure
Build an ERD if you don’t have
Primary Keys
Unique Keys
Table Size
Number of columns (columnar databases)
KNOW YOUR DATA
Data Modelling
Modelling techniques
Transactional
3NF
Star Schema - Data Marts
Integration - Data Vault
Data Lake - Big Data
Flat tables
Data Lake or Data swamp
AVOID the journey to a SWAMP
Organize your data and contents
Use Name conventions - rules
Be aware of object creation
Document them on same way
Verbose
Customer
id
name
date
Sales
id
date
Cust_id
amount
Customer
id_customer
name
date_of_birth
Sales
id_sales
sales_date
fk_customer
amount_inc_tax
SELECT c.name AS customer_name,
c.date AS customer.dob,
s.date AS sales_date,
COUNT(1) AS no_of_sales,
SUM(amount) AS amount
FROM customer c
JOIN sales s
ON c.id = s.id
SELECT c.name AS customer_name,
c.date_of_birth,
s.sales_date,
COUNT(1) no_of_sales,
SUM(amount_inc_tax) AS amount_inc_tax
FROM customer c
JOIN sales s
ON c.id_customer = s.fk_customer
Minimise usage of non standard abbreviations
Don’t use too long names - you will need to type them one day
PK & FK should be a pattern
ID & table name, FK and link table
Maybe:
Data types definition on names like: price_amt, tax_pct
Know technologies
Know you databases & tools
Differences, limitations, strength and
weakness
Columnar databases
Functionalities
Access & Security
Help make a better
world with
beautiful code!!!

Mais conteúdo relacionado

Semelhante a SQL coding at Sydney Measure Camp 2018

Customer Clustering for Retailer Marketing
Customer Clustering for Retailer MarketingCustomer Clustering for Retailer Marketing
Customer Clustering for Retailer MarketingJonathan Sedar
 
James Colby Maddox Business Intellignece and Computer Science Portfolio
James Colby Maddox Business Intellignece and Computer Science PortfolioJames Colby Maddox Business Intellignece and Computer Science Portfolio
James Colby Maddox Business Intellignece and Computer Science Portfoliocolbydaman
 
Data Exploration with Apache Drill: Day 2
Data Exploration with Apache Drill: Day 2Data Exploration with Apache Drill: Day 2
Data Exploration with Apache Drill: Day 2Charles Givre
 
2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_uploadProf. Wim Van Criekinge
 
Final Project SQL - Elyada Wigati Pramaresti.pptx
Final Project SQL - Elyada Wigati Pramaresti.pptxFinal Project SQL - Elyada Wigati Pramaresti.pptx
Final Project SQL - Elyada Wigati Pramaresti.pptxElyada Wigati Pramaresti
 
Meetup Beleza na Web - Funções analíticas com SQL
Meetup Beleza na Web - Funções analíticas com SQLMeetup Beleza na Web - Funções analíticas com SQL
Meetup Beleza na Web - Funções analíticas com SQLBruno Paulino, MBA
 
Database Management System - SQL Advanced Training
Database Management System - SQL Advanced TrainingDatabase Management System - SQL Advanced Training
Database Management System - SQL Advanced TrainingMoutasm Tamimi
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with RCasper Crause
 
Oracle Advanced Dml
Oracle Advanced DmlOracle Advanced Dml
Oracle Advanced Dmlssunka01
 
100 sample formulas_v6
100 sample formulas_v6100 sample formulas_v6
100 sample formulas_v6artimaroo1
 
Useful date number and string sql queries
Useful date number and string sql queriesUseful date number and string sql queries
Useful date number and string sql queriesFeras Ahmad
 
An introduction to Machine Learning
An introduction to Machine LearningAn introduction to Machine Learning
An introduction to Machine LearningJulien SIMON
 
Building a data warehouse
Building a data warehouseBuilding a data warehouse
Building a data warehouseEster Daci
 
Data Modeling in Looker
Data Modeling in LookerData Modeling in Looker
Data Modeling in LookerLooker
 
Below is my code- I have an error that I still have difficulty figurin.pdf
Below is my code- I have an error that I still have difficulty figurin.pdfBelow is my code- I have an error that I still have difficulty figurin.pdf
Below is my code- I have an error that I still have difficulty figurin.pdfarmanuelraj
 
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Dan Robinson
 

Semelhante a SQL coding at Sydney Measure Camp 2018 (20)

Customer Clustering for Retailer Marketing
Customer Clustering for Retailer MarketingCustomer Clustering for Retailer Marketing
Customer Clustering for Retailer Marketing
 
James Colby Maddox Business Intellignece and Computer Science Portfolio
James Colby Maddox Business Intellignece and Computer Science PortfolioJames Colby Maddox Business Intellignece and Computer Science Portfolio
James Colby Maddox Business Intellignece and Computer Science Portfolio
 
Data Exploration with Apache Drill: Day 2
Data Exploration with Apache Drill: Day 2Data Exploration with Apache Drill: Day 2
Data Exploration with Apache Drill: Day 2
 
2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload2018 03 27_biological_databases_part4_v_upload
2018 03 27_biological_databases_part4_v_upload
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Final Project SQL - Elyada Wigati Pramaresti.pptx
Final Project SQL - Elyada Wigati Pramaresti.pptxFinal Project SQL - Elyada Wigati Pramaresti.pptx
Final Project SQL - Elyada Wigati Pramaresti.pptx
 
Introtosqltuning
IntrotosqltuningIntrotosqltuning
Introtosqltuning
 
Meetup Beleza na Web - Funções analíticas com SQL
Meetup Beleza na Web - Funções analíticas com SQLMeetup Beleza na Web - Funções analíticas com SQL
Meetup Beleza na Web - Funções analíticas com SQL
 
Database Management System - SQL Advanced Training
Database Management System - SQL Advanced TrainingDatabase Management System - SQL Advanced Training
Database Management System - SQL Advanced Training
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with R
 
Sql wksht-3
Sql wksht-3Sql wksht-3
Sql wksht-3
 
Oracle Advanced Dml
Oracle Advanced DmlOracle Advanced Dml
Oracle Advanced Dml
 
100 sample formulas_v6
100 sample formulas_v6100 sample formulas_v6
100 sample formulas_v6
 
Useful date number and string sql queries
Useful date number and string sql queriesUseful date number and string sql queries
Useful date number and string sql queries
 
Data Warehouse Project
Data Warehouse ProjectData Warehouse Project
Data Warehouse Project
 
An introduction to Machine Learning
An introduction to Machine LearningAn introduction to Machine Learning
An introduction to Machine Learning
 
Building a data warehouse
Building a data warehouseBuilding a data warehouse
Building a data warehouse
 
Data Modeling in Looker
Data Modeling in LookerData Modeling in Looker
Data Modeling in Looker
 
Below is my code- I have an error that I still have difficulty figurin.pdf
Below is my code- I have an error that I still have difficulty figurin.pdfBelow is my code- I have an error that I still have difficulty figurin.pdf
Below is my code- I have an error that I still have difficulty figurin.pdf
 
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
Powering Heap With PostgreSQL And CitusDB (PGConf Silicon Valley 2015)
 

Último

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 

Último (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 

SQL coding at Sydney Measure Camp 2018

  • 2.
  • 3. select i_ITEM_id,i_item_desc,i_category,i_class,i_CURRENT_price, sum(cs_EXT_sales_price) as itemrevenue, sum(cs_ext_sales_price) *100/sum(sum(cs_ext_sales_price)) over (partition by I_class) as revenueratio From catalog_sales,item,date_dim where cs_item_sk = i_item_sk AND i_category in ('Jewelry', 'Sports', 'Books') and cs_sold_date_sk = d_date_sk and cast(d_date as timestamp) between cast('2001-01-12' as timestamp) and (cast('2001-01-12' as timestamp) + interval 30 days) group by i_item_id,i_item_desc,i_category,i_class,i_current_price order by i_category,i_class,i_item_id,i_item_desc,revenueratio limit 100;
  • 5.
  • 6. select i_ITEM_id, I_item_desc, I_category, I_class, i_CURRENT_price, sum(cs_EXT_sales_price) as itemrevenue, sum(cs_ext_sales_price) *100/sum(sum(cs_ext_sales_price)) over (partition by I_class) as revenueratio FROM catalog_sales,item,date_dim where cs_item_sk = i_item_sk AND i_category in ('Jewelry', 'Sports', 'Books') and cs_sold_date_sk = d_date_sk and cast(d_date as timestamp) between cast('2001-01-12' as timestamp) and (cast('2001-01-12' as timestamp) + interval 30 days) group by i_item_id,i_item_desc,i_category,i_class,i_current_price order by i_category,i_class,i_item_id,i_item_desc,revenueratio limit 100;
  • 7. select i_ITEM_id, I_item_desc, I_category, I_class, i_CURRENT_price, sum(cs_EXT_sales_price) as itemrevenue, sum(cs_ext_sales_price) *100/sum(sum(cs_ext_sales_price)) over (partition by I_class) as revenueratio FROM catalog_sales,item,date_dim where cs_item_sk = i_item_sk AND i_category in ('Jewelry', 'Sports', 'Books') and cs_sold_date_sk = d_date_sk and cast(d_date as timestamp) between cast('2001-01-12' as timestamp) and (cast('2001-01-12' as timestamp) + interval 30 days) group by i_item_id, I_item_desc, I_category, I_class, i_current_price order by i_category, I_class, I_item_id, I_item_desc, revenueratio limit 100;
  • 8. SELECT i_item_id, ,i_item_desc ,i_category ,i_class ,i_current_price ,SUM(cs_ext_sales_price) AS item_revenue ,SUM(cs_ext_sales_price) * 100 / SUM(SUM(cs_ext_sales_price)) OVER (PARTITION BY i_class) AS revenue_ratio FROM catalog_sales JOIN item ON cs_item_sk = i_item_sk JOIN date_dim ON cs_sold_date_sk = d_date_sk WHERE i_category IN ('Jewelry', 'Sports', 'Books') AND CAST(d_date AS TIMESTAMP) BETWEEN CAST('2001-01-12' AS TIMESTAMP) AND CAST('2001-01-12' AS TIMESTAMP) + INTERVAL 30 DAYS GROUP BY 1, 2, 3, 4, 5 ORDER BY 3, 4, 1 LIMIT 100
  • 9. SELECT item.i_item_id, item.i_item_desc, item.i_category, item.i_class, item.i_current_price, SUM(catalog_sales.cs_ext_sales_price) AS item_revenue, SUM(catalog_sales.cs_ext_sales_price) * 100 / SUM(SUM(catalog_sales.cs_ext_sales_price)) OVER (PARTITION BY item.i_class) AS revenue_ratio FROM catalog_sales JOIN item ON catalog_sales.cs_item_sk = item.i_item_sk JOIN date_dim ON catalog_sales.cs_sold_date_sk = date_dim.d_date_sk WHERE item.i_category IN ('Jewelry', 'Sports', 'Books') AND CAST(date_dim.d_date AS TIMESTAMP) BETWEEN CAST('2001-01-12' AS TIMESTAMP) AND CAST('2001-01-12' AS TIMESTAMP) + INTERVAL 30 DAYS GROUP BY 1, 2, 3, 4, 5 ORDER BY 3, 4, 1 LIMIT 100
  • 10. Over commenting -- get id, name and open only once SELECT DISTINCT t1.id , t1.name , t3.open -- select names from table 1, order them by date and return first 3 FROM (SELECT id , name FROM table1 ORDER BY date DESC LIMIT 3) AS t1 -- get open from table 2 IF id is there, open = 1 and type =2 LEFT JOIN ( ( SELECT open , name_id FROM table2 WHERE open=1 AND type=2 ) AS t3 ) ON t1.id = t3.name_id -- order by name from A-Z ORDER BY t1.name ASC
  • 11.
  • 12.
  • 13. Follow Patterns Indentation Don’t over comment - just clear code Remove commented lines of code Use alias on all column when joining Know your PKs and Unique keys Make easy future maintenance Execution time JUST select the tables/columns which be in USE
  • 15.
  • 16. WITH patient_data AS (SELECT patient_id, patient_name, hospital, drug_dosage FROM hospital_registry WHERE (COALESCE(last_visit,NOW()) > NOW() - INTERVAL '14 days') AND city = "Los Angeles" ), average_dosage AS (SELECT hospital, AVG(drug_dosage) AS Average FROM patient_data GROUP BY hospital ) SELECT count(hospital) FROM average_dosage WHERE drug_dosage > 1000
  • 17. Master the use of: Functions Window Functions (OLAP functions) CTE - Common Table Expression Views UDF - User Defined Function
  • 18. Lost
  • 19.
  • 20. Data Structure Build an ERD if you don’t have Primary Keys Unique Keys Table Size Number of columns (columnar databases) KNOW YOUR DATA
  • 22. Modelling techniques Transactional 3NF Star Schema - Data Marts Integration - Data Vault Data Lake - Big Data Flat tables
  • 23. Data Lake or Data swamp
  • 24. AVOID the journey to a SWAMP Organize your data and contents Use Name conventions - rules Be aware of object creation Document them on same way
  • 26.
  • 27. Customer id name date Sales id date Cust_id amount Customer id_customer name date_of_birth Sales id_sales sales_date fk_customer amount_inc_tax SELECT c.name AS customer_name, c.date AS customer.dob, s.date AS sales_date, COUNT(1) AS no_of_sales, SUM(amount) AS amount FROM customer c JOIN sales s ON c.id = s.id SELECT c.name AS customer_name, c.date_of_birth, s.sales_date, COUNT(1) no_of_sales, SUM(amount_inc_tax) AS amount_inc_tax FROM customer c JOIN sales s ON c.id_customer = s.fk_customer
  • 28. Minimise usage of non standard abbreviations Don’t use too long names - you will need to type them one day PK & FK should be a pattern ID & table name, FK and link table Maybe: Data types definition on names like: price_amt, tax_pct
  • 30. Know you databases & tools Differences, limitations, strength and weakness Columnar databases Functionalities Access & Security
  • 31. Help make a better world with beautiful code!!!