Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

PostgreSQL Advanced Queries

3,332 views

Published on

PostgreSQL Advanced Queries session for ComeLabs Workshop Margonda, 14 Nop 2015

Published in: Software
  • Login to see the comments

PostgreSQL Advanced Queries

  1. 1. PostgreSQL Advanced Queries COMELABS - MARGONDA DEPOK 14 NOPEMBER 2015
  2. 2. About Me • Editor PojokProgrammer.net • Writers welcome! • CEO BiruniLabs • Trainers welcome! • CEO Cronos Studio • Developers welcome!
  3. 3. WhatYou Need • Laptop or Computer • PostgreSQL Installed • SQL Interface • HeidiSQL • SQLyog • phpMyAdmin • Whatever..... • Your Brain 
  4. 4. SQL Basics
  5. 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. 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. 7. SQL Anatomy • Statements • Queries • Clauses • Predicates • Expressions
  8. 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. 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. 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. 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)
  12. 12. Visual Representation
  13. 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. 14. Ready for Advanced Queries?
  15. 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. 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
  17. 17. Hierarchical Query using CTE
  18. 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()
  19. 19. Windowing Function
  20. 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
  21. 21. Pivot Sample
  22. 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
  23. 23. ThankYou QUESTIONS AND ANSWERS

×