SlideShare une entreprise Scribd logo
1  sur  23
Télécharger pour lire hors ligne
PostgreSQL
Advanced Queries
COMELABS - MARGONDA DEPOK
14 NOPEMBER 2015
About Me
• Editor PojokProgrammer.net
• Writers welcome!
• CEO BiruniLabs
• Trainers welcome!
• CEO Cronos Studio
• Developers welcome!
WhatYou Need
• Laptop or Computer
• PostgreSQL Installed
• SQL Interface
• HeidiSQL
• SQLyog
• phpMyAdmin
• Whatever.....
• Your Brain 
SQL Basics
History of PostgreSQL
• INGRES, Berkeley 1982
• POSTGRES, Berkeley 1985
• POSTGRES v1, 1989
• PostQUEL, 1990
•PostgreSQL 6.0 (1995)
• Initial release
•PostgreSQL 7.0 (2000)
• SQL92 compliance
•PostgreSQL 8.0 (2005)
• Multi-Platform support
• Analytical Function
support
•PostgreSQL 9.0 (2010)
• Built-in Replication
• JSON datatype support
Rule of Thumb
From Tom Kyte (Oracle Evangelist)
1. Use single SQL statement whenever
possible
2. Use PL/SQL or Stored Procedure
3. Use Java (or other programming
language)
4. Rethink why you want to do it
(refine your approach)
SQL Anatomy
• Statements
• Queries
• Clauses
• Predicates
• Expressions
ChangeYour Mindset!
• Apa yang akan kalian lakukan jika mendapatkan tugas seperti di
bawah ini
• Tampilkan angka 1 sampai dengan 100, namun
setiap kelipatan 3 ubah angkanya menjadi kata
Rumah, setiap kelipatan 5 ubah angkanya menjadi
kata Sakit, dan setiap kelipatan 15 ubah angkanya
menjadi kata Rumah Sakit.
SELECT
SELECT b,d,e,h
FROM some_table ;
SELECT *
FROM some_table
WHERE x IN (3,5,6,8) ;
SELECT b,d,e,h
FROM some_table
WHERE x IN (3,5,6,8) ;
JOIN vs UNION
JOIN
======
SELECT *
FROM A JOIN B ON 1=1
UNION (UNION ALL)
======
SELECT * FROM A
UNION ALL
SELECT * FROM B
JOIN
• INNER JOIN (JOIN)
• LEFT OUTER JOIN (LEFT JOIN)
• RIGHT OUTER JOIN (RIGHT JOIN)
• FULL OUTER JOIN (FULLJOIN)
• LEFT JOIN EXCLUDING INNER JOIN (LEFT EXCLUDING
JOIN)
• RIGHT JOIN EXCLUDING INNER JOIN (RIGHT EXCLUDING
JOIN)
• OUTER JOIN EXCLUDING INNER JOIN (OUTER
EXCLUDINGJOIN)
Visual Representation
ChangeYour Mindset!
• Apa yang akan kalian lakukan jika diminta membuat output
seperti di bawah ini
Tabel A
ID Description
1 SUV
2 Sedan
3 Truk
4 Bus
5 MPV
Tabel B
A_ID Description
1 Toyota Fortuner
1 BMW X5
2 ToyotaVios
2 Honda City
5 Dahihatsu GranMax
ID Description Jumlah
1 SUV 2
2 Sedan 2
3 Truk 0
4 Bus 0
5 MPV 1
Ready for Advanced Queries?
Common Table Expression
• Standard SQL feature
• Allows you to split a query statement into distinct
parts
• Results of each part will appear as a table
• More maintainable than subqueries
Hierarchical Queries
• Fixed depth hierarchical data can be solved using
simple JOINs
• CTE allows recursive query
• CTE can process data as hierarchical
• CTE can process arbitrarily deep hierarchies with
just one query
Hierarchical Query using CTE
Aggregates and Window Functions
• GROUP BY lets you calculate aggregates of data over a single or
multiple columns in a result set.
• GROUP BY can only aggregate over a single grouping
• GROUP BY only return aggregated data, detail data is not
preserved
• Window functions make it possible
• Indicated by OVER Clause
• ROW_NUMBER() OVER()
• SUM() OVER()
• COUNT() OVER()
• MAX() OVER()
• MIN() OVER()
• AVG() OVER()
Windowing Function
Pivoting data
• Sometimes it’s nice to be able to pivot data in a
properly normalized data model, so that repeating
groups of related entities are folded into parent
entity as columns.
• Pivoting is very useful reporting purposes and ad-
hoc queries.
• PostgreSQL can handle pivoting data using
• Subqueries and Arrays
• UsingCASE clause
Pivot Sample
Other Advanced PostgreSQL
• JSON built-in support
• Pattern matching. Regular expression matching is
supported
• Geolocation queries. PostGIS extension adds
comprehensive support for managing and querying
geospatial data
• Partitioning
• Replication
ThankYou
QUESTIONS AND ANSWERS

Contenu connexe

Tendances

Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360
Carlos Sierra
 

Tendances (20)

PostgreSQL replication
PostgreSQL replicationPostgreSQL replication
PostgreSQL replication
 
Mastering PostgreSQL Administration
Mastering PostgreSQL AdministrationMastering PostgreSQL Administration
Mastering PostgreSQL Administration
 
Linux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performanceLinux tuning to improve PostgreSQL performance
Linux tuning to improve PostgreSQL performance
 
Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360Understanding my database through SQL*Plus using the free tool eDB360
Understanding my database through SQL*Plus using the free tool eDB360
 
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
InfluxDB IOx Tech Talks: Query Engine Design and the Rust-Based DataFusion in...
 
Presto query optimizer: pursuit of performance
Presto query optimizer: pursuit of performancePresto query optimizer: pursuit of performance
Presto query optimizer: pursuit of performance
 
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin HuaiA Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
A Deep Dive into Spark SQL's Catalyst Optimizer with Yin Huai
 
PostgreSQL- An Introduction
PostgreSQL- An IntroductionPostgreSQL- An Introduction
PostgreSQL- An Introduction
 
PostgreSQL HA
PostgreSQL   HAPostgreSQL   HA
PostgreSQL HA
 
Productizing Structured Streaming Jobs
Productizing Structured Streaming JobsProductizing Structured Streaming Jobs
Productizing Structured Streaming Jobs
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
Hive Bucketing in Apache Spark with Tejas Patil
Hive Bucketing in Apache Spark with Tejas PatilHive Bucketing in Apache Spark with Tejas Patil
Hive Bucketing in Apache Spark with Tejas Patil
 
Presto on Apache Spark: A Tale of Two Computation Engines
Presto on Apache Spark: A Tale of Two Computation EnginesPresto on Apache Spark: A Tale of Two Computation Engines
Presto on Apache Spark: A Tale of Two Computation Engines
 
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
 
PostgreSql query planning and tuning
PostgreSql query planning and tuningPostgreSql query planning and tuning
PostgreSql query planning and tuning
 
SQL Tuning 101
SQL Tuning 101SQL Tuning 101
SQL Tuning 101
 
Tanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools shortTanel Poder - Scripts and Tools short
Tanel Poder - Scripts and Tools short
 
Online index rebuild automation
Online index rebuild automationOnline index rebuild automation
Online index rebuild automation
 
Bigquery 101
Bigquery 101Bigquery 101
Bigquery 101
 

En vedette

En vedette (14)

PostgreSQL: Advanced features in practice
PostgreSQL: Advanced features in practicePostgreSQL: Advanced features in practice
PostgreSQL: Advanced features in practice
 
Do IT with SQL
Do IT with SQLDo IT with SQL
Do IT with SQL
 
Formation LPIC-1_SysArch
Formation LPIC-1_SysArchFormation LPIC-1_SysArch
Formation LPIC-1_SysArch
 
LPI Academy Presents Linux Essentials - A Deep Dive
LPI Academy Presents Linux Essentials - A Deep DiveLPI Academy Presents Linux Essentials - A Deep Dive
LPI Academy Presents Linux Essentials - A Deep Dive
 
Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.Deep dive into PostgreSQL statistics.
Deep dive into PostgreSQL statistics.
 
GPGPU Accelerates PostgreSQL (English)
GPGPU Accelerates PostgreSQL (English)GPGPU Accelerates PostgreSQL (English)
GPGPU Accelerates PostgreSQL (English)
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL
 
Lessons PostgreSQL learned from commercial databases, and didn’t
Lessons PostgreSQL learned from commercial databases, and didn’tLessons PostgreSQL learned from commercial databases, and didn’t
Lessons PostgreSQL learned from commercial databases, and didn’t
 
PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs PostgreSQL WAL for DBAs
PostgreSQL WAL for DBAs
 
PostgreSQL + ZFS best practices
PostgreSQL + ZFS best practicesPostgreSQL + ZFS best practices
PostgreSQL + ZFS best practices
 
What’s New in Amazon Aurora for MySQL and PostgreSQL
What’s New in Amazon Aurora for MySQL and PostgreSQLWhat’s New in Amazon Aurora for MySQL and PostgreSQL
What’s New in Amazon Aurora for MySQL and PostgreSQL
 
Full Text Search In PostgreSQL
Full Text Search In PostgreSQLFull Text Search In PostgreSQL
Full Text Search In PostgreSQL
 
5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance5 Steps to PostgreSQL Performance
5 Steps to PostgreSQL Performance
 
Migrating to Amazon RDS with Database Migration Service
Migrating to Amazon RDS with Database Migration ServiceMigrating to Amazon RDS with Database Migration Service
Migrating to Amazon RDS with Database Migration Service
 

Similaire à PostgreSQL Advanced Queries

Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012
Brij Mishra
 
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
SQLDay2013_Denny Cherry - Table indexing for the .NET DeveloperSQLDay2013_Denny Cherry - Table indexing for the .NET Developer
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
Polish SQL Server User Group
 
PostgreSQL 9.0 & The Future
PostgreSQL 9.0 & The FuturePostgreSQL 9.0 & The Future
PostgreSQL 9.0 & The Future
Aaron Thul
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
Jonathan Levin
 

Similaire à PostgreSQL Advanced Queries (20)

Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012Exciting Features for SQL Devs in SQL 2012
Exciting Features for SQL Devs in SQL 2012
 
3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql3 CityNetConf - sql+c#=u-sql
3 CityNetConf - sql+c#=u-sql
 
Sql query performance analysis
Sql query performance analysisSql query performance analysis
Sql query performance analysis
 
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
SQLDay2013_Denny Cherry - Table indexing for the .NET DeveloperSQLDay2013_Denny Cherry - Table indexing for the .NET Developer
SQLDay2013_Denny Cherry - Table indexing for the .NET Developer
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
Cassandra training
Cassandra trainingCassandra training
Cassandra training
 
Sql query performance analysis
Sql query performance analysisSql query performance analysis
Sql query performance analysis
 
nGram full text search (by 이성욱)
nGram full text search (by 이성욱)nGram full text search (by 이성욱)
nGram full text search (by 이성욱)
 
PostgreSQL 9.0 & The Future
PostgreSQL 9.0 & The FuturePostgreSQL 9.0 & The Future
PostgreSQL 9.0 & The Future
 
An Introduction to Elastic Search.
An Introduction to Elastic Search.An Introduction to Elastic Search.
An Introduction to Elastic Search.
 
30334823 my sql-cluster-performance-tuning-best-practices
30334823 my sql-cluster-performance-tuning-best-practices30334823 my sql-cluster-performance-tuning-best-practices
30334823 my sql-cluster-performance-tuning-best-practices
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
 
Sql killedserver
Sql killedserverSql killedserver
Sql killedserver
 
Scaling MySQL Strategies for Developers
Scaling MySQL Strategies for DevelopersScaling MySQL Strategies for Developers
Scaling MySQL Strategies for Developers
 
Modern sql
Modern sqlModern sql
Modern sql
 
Citus Architecture: Extending Postgres to Build a Distributed Database
Citus Architecture: Extending Postgres to Build a Distributed DatabaseCitus Architecture: Extending Postgres to Build a Distributed Database
Citus Architecture: Extending Postgres to Build a Distributed Database
 
Tunning overview
Tunning overviewTunning overview
Tunning overview
 
MySql Practical Partitioning
MySql Practical PartitioningMySql Practical Partitioning
MySql Practical Partitioning
 
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to SphinxMYSQL Query Anti-Patterns That Can Be Moved to Sphinx
MYSQL Query Anti-Patterns That Can Be Moved to Sphinx
 
10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL10 Reasons to Start Your Analytics Project with PostgreSQL
10 Reasons to Start Your Analytics Project with PostgreSQL
 

Plus de Nur Hidayat (8)

Develop a Software, Where to Start?
Develop a Software, Where to Start?Develop a Software, Where to Start?
Develop a Software, Where to Start?
 
Do It With SQL - Journey to the Center of Database Worlds
Do It With SQL - Journey to the Center of Database WorldsDo It With SQL - Journey to the Center of Database Worlds
Do It With SQL - Journey to the Center of Database Worlds
 
How to Design a Good Database
How to Design a Good DatabaseHow to Design a Good Database
How to Design a Good Database
 
How to Design a Good Database for Your Application
How to Design a Good Database for Your ApplicationHow to Design a Good Database for Your Application
How to Design a Good Database for Your Application
 
Seminar Android - Pengenalan PhoneGap
Seminar Android - Pengenalan PhoneGapSeminar Android - Pengenalan PhoneGap
Seminar Android - Pengenalan PhoneGap
 
How to Become Great Programmer
How to Become Great ProgrammerHow to Become Great Programmer
How to Become Great Programmer
 
PHP Oracle
PHP OraclePHP Oracle
PHP Oracle
 
MRI Presentation
MRI PresentationMRI Presentation
MRI Presentation
 

Dernier

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Dernier (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 

PostgreSQL Advanced Queries

  • 1. PostgreSQL Advanced Queries COMELABS - MARGONDA DEPOK 14 NOPEMBER 2015
  • 2. About Me • Editor PojokProgrammer.net • Writers welcome! • CEO BiruniLabs • Trainers welcome! • CEO Cronos Studio • Developers welcome!
  • 3. WhatYou Need • Laptop or Computer • PostgreSQL Installed • SQL Interface • HeidiSQL • SQLyog • phpMyAdmin • Whatever..... • Your Brain 
  • 5. History of PostgreSQL • INGRES, Berkeley 1982 • POSTGRES, Berkeley 1985 • POSTGRES v1, 1989 • PostQUEL, 1990 •PostgreSQL 6.0 (1995) • Initial release •PostgreSQL 7.0 (2000) • SQL92 compliance •PostgreSQL 8.0 (2005) • Multi-Platform support • Analytical Function support •PostgreSQL 9.0 (2010) • Built-in Replication • JSON datatype support
  • 6. Rule of Thumb From Tom Kyte (Oracle Evangelist) 1. Use single SQL statement whenever possible 2. Use PL/SQL or Stored Procedure 3. Use Java (or other programming language) 4. Rethink why you want to do it (refine your approach)
  • 7. SQL Anatomy • Statements • Queries • Clauses • Predicates • Expressions
  • 8. ChangeYour Mindset! • Apa yang akan kalian lakukan jika mendapatkan tugas seperti di bawah ini • Tampilkan angka 1 sampai dengan 100, namun setiap kelipatan 3 ubah angkanya menjadi kata Rumah, setiap kelipatan 5 ubah angkanya menjadi kata Sakit, dan setiap kelipatan 15 ubah angkanya menjadi kata Rumah Sakit.
  • 9. SELECT SELECT b,d,e,h FROM some_table ; SELECT * FROM some_table WHERE x IN (3,5,6,8) ; SELECT b,d,e,h FROM some_table WHERE x IN (3,5,6,8) ;
  • 10. JOIN vs UNION JOIN ====== SELECT * FROM A JOIN B ON 1=1 UNION (UNION ALL) ====== SELECT * FROM A UNION ALL SELECT * FROM B
  • 11. JOIN • INNER JOIN (JOIN) • LEFT OUTER JOIN (LEFT JOIN) • RIGHT OUTER JOIN (RIGHT JOIN) • FULL OUTER JOIN (FULLJOIN) • LEFT JOIN EXCLUDING INNER JOIN (LEFT EXCLUDING JOIN) • RIGHT JOIN EXCLUDING INNER JOIN (RIGHT EXCLUDING JOIN) • OUTER JOIN EXCLUDING INNER JOIN (OUTER EXCLUDINGJOIN)
  • 13. ChangeYour Mindset! • Apa yang akan kalian lakukan jika diminta membuat output seperti di bawah ini Tabel A ID Description 1 SUV 2 Sedan 3 Truk 4 Bus 5 MPV Tabel B A_ID Description 1 Toyota Fortuner 1 BMW X5 2 ToyotaVios 2 Honda City 5 Dahihatsu GranMax ID Description Jumlah 1 SUV 2 2 Sedan 2 3 Truk 0 4 Bus 0 5 MPV 1
  • 14. Ready for Advanced Queries?
  • 15. Common Table Expression • Standard SQL feature • Allows you to split a query statement into distinct parts • Results of each part will appear as a table • More maintainable than subqueries
  • 16. Hierarchical Queries • Fixed depth hierarchical data can be solved using simple JOINs • CTE allows recursive query • CTE can process data as hierarchical • CTE can process arbitrarily deep hierarchies with just one query
  • 18. Aggregates and Window Functions • GROUP BY lets you calculate aggregates of data over a single or multiple columns in a result set. • GROUP BY can only aggregate over a single grouping • GROUP BY only return aggregated data, detail data is not preserved • Window functions make it possible • Indicated by OVER Clause • ROW_NUMBER() OVER() • SUM() OVER() • COUNT() OVER() • MAX() OVER() • MIN() OVER() • AVG() OVER()
  • 20. Pivoting data • Sometimes it’s nice to be able to pivot data in a properly normalized data model, so that repeating groups of related entities are folded into parent entity as columns. • Pivoting is very useful reporting purposes and ad- hoc queries. • PostgreSQL can handle pivoting data using • Subqueries and Arrays • UsingCASE clause
  • 22. Other Advanced PostgreSQL • JSON built-in support • Pattern matching. Regular expression matching is supported • Geolocation queries. PostGIS extension adds comprehensive support for managing and querying geospatial data • Partitioning • Replication