PostgreSQL Advanced Queries

Post on 08-Jan-2017

773 views 0 download

Transcript of PostgreSQL Advanced Queries

PostgreSQL Advanced QueriesCOMELABS - MARGONDA DEPOK14 NOPEMBER 2015

About Me

• Editor PojokProgrammer.net

• Writers welcome!

• CEO BiruniLabs

• Trainers welcome!

• CEO Cronos Studio

• Developers welcome!

What You 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 ThumbFrom 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

Change Your Mindset!

• Apa yang akan kalian lakukan jika mendapatkan tugas seperti di bawah ini

• Tampilkan angka 1 sampai dengan 100, namunsetiap kelipatan 3 ubah angkanya menjadi kata Rumah, setiap kelipatan 5 ubah angkanya menjadikata Sakit, dan setiap kelipatan 15 ubah angkanyamenjadi 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

JO IN= = = = = =S E L E CT * F R O M A J O I N B O N 1 = 1

U NIO N (U NIO N ALL )= = = = = =S E L E CT * F R O M AU N I O N AL LS E L E CT * F R O M B

JOIN

• INNER JOIN (JOIN)

• LEFT OUTER JOIN (LEFT JOIN)

• RIGHT OUTER JOIN (RIGHT JOIN)

• FULL OUTER JOIN (FULL JOIN)

• LEFT JOIN EXCLUDING INNER JOIN (LEFT EXCLUDING JOIN)

• RIGHT JOIN EXCLUDING INNER JOIN (RIGHT EXCLUDING JOIN)

• OUTER JOIN EXCLUDING INNER JOIN (OUTER EXCLUDING JOIN)

Visual Representation

Change Your 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 Toyota Vios

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

• Using CASE 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

Thank YouQU E S TI O N S AN D AN S W E R S