+ All Categories
Home > Software > PostgreSQL Advanced Queries

PostgreSQL Advanced Queries

Date post: 08-Jan-2017
Category:
Upload: nur-hidayat
View: 770 times
Download: 0 times
Share this document with a friend
23
PostgreSQL Advanced Queries COMELABS - MARGONDA DEPOK 14 NOPEMBER 2015
Transcript
Page 1: PostgreSQL Advanced Queries

PostgreSQL Advanced QueriesCOMELABS - MARGONDA DEPOK14 NOPEMBER 2015

Page 2: PostgreSQL Advanced Queries

About Me

• Editor PojokProgrammer.net

• Writers welcome!

• CEO BiruniLabs

• Trainers welcome!

• CEO Cronos Studio

• Developers welcome!

Page 3: PostgreSQL Advanced Queries

What You Need• Laptop or Computer

• PostgreSQL Installed

• SQL Interface• HeidiSQL

• SQLyog

• phpMyAdmin

• Whatever.....

• Your Brain

Page 4: PostgreSQL Advanced Queries

SQL Basics

Page 5: PostgreSQL Advanced Queries

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

Page 6: PostgreSQL Advanced Queries

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)

Page 7: PostgreSQL Advanced Queries

SQL Anatomy

• Statements

• Queries

• Clauses

• Predicates

• Expressions

Page 8: PostgreSQL Advanced Queries

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.

Page 9: PostgreSQL Advanced Queries

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) ;

Page 10: PostgreSQL Advanced Queries

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

Page 11: PostgreSQL Advanced Queries

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)

Page 12: PostgreSQL Advanced Queries

Visual Representation

Page 13: PostgreSQL Advanced Queries

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

Page 14: PostgreSQL Advanced Queries

Ready for Advanced Queries?

Page 15: PostgreSQL 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

Page 16: PostgreSQL Advanced Queries

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

Page 17: PostgreSQL Advanced Queries

Hierarchical Query using CTE

Page 18: PostgreSQL Advanced Queries

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()

Page 19: PostgreSQL Advanced Queries

Windowing Function

Page 20: PostgreSQL Advanced Queries

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

Page 21: PostgreSQL Advanced Queries

Pivot Sample

Page 22: PostgreSQL Advanced Queries

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

Page 23: PostgreSQL Advanced Queries

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


Recommended