0% found this document useful (0 votes)
26 views11 pages

Tugas Praktikum 3 Dengan PostgreSQL

This document discusses various PostgreSQL aggregation functions and joins. It shows examples counting, summing, finding minimum, maximum and average values from tables. It demonstrates using GROUP BY to aggregate results and HAVING to filter groups. It also creates sample customer, employee, part and salesorder tables, inserts data, and performs three and four table joins.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views11 pages

Tugas Praktikum 3 Dengan PostgreSQL

This document discusses various PostgreSQL aggregation functions and joins. It shows examples counting, summing, finding minimum, maximum and average values from tables. It demonstrates using GROUP BY to aggregate results and HAVING to filter groups. It also creates sample customer, employee, part and salesorder tables, inserts data, and performs three and four table joins.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Tugas Praktikum 3 dengan PostgreSQL

Dibuat oleh : Muhamad Satria Panji Victory 1103159

PROGRAM DIPLOMA III TEKNIK INFORMATIKA POLITEKNIK POS INDONESIA BANDUNG 2011

Contoh Aggregates
test=> SELECT * FROM friend ORDER BY firstname;

test=> SELECT COUNT(*) FROM friend;

test=> SELECT SUM(age) FROM friend;

test=> SELECT MIN(age) FROM friend;

test=> SELECT AVG(age) FROM friend;

Penggunaan Group By

=> SELECT state, COUNT(*) -> FROM friend -> GROUP BY state;

=> SELECT state, MIN(age), MAX(age), AVG(age) -> FROM friend -> GROUP BY state -> ORDER BY 4 DESC;

Penggunaan HAVING

test=> SELECT state, COUNT(*) test-> FROM friend test-> GROUP BY state test-> HAVING COUNT(*) > 0 test-> ORDER BY state;

Aggregates dan Null Values


> CREATE TABLE aggtest (col INTEGER);

> SELECT SUM(col) FROM aggtest;

> SELECT MAX(col) FROM aggtest;

> SELECT COUNT(*) FROM aggtest;

> SELECT COUNT(col) FROM aggtest;

> INSERT INTO aggtest VALUES (3);

> SELECT COUNT(*) FROM aggtest;

> SELECT COUNT(col) FROM aggtest;

GROUP BY WITH TWO COLOUMNS


=> SELECT city, state, COUNT(*) -> FROM friend -> GROUP BY state, city -> ORDER BY 1, 2;

JOINING TABLES
=> SELECT firstname FROM friend WHERE state = 'PA';

=> SELECT friend.firstname FROM friend WHERE friend.state = 'PA';

=> SELECT f.firstname FROM friend f WHERE f.state = 'PA';

CREATION OF COMPANY TABLES


=> CREATE TABLE customer ( (> customer_id INTEGER, (> name CHAR(30), (> telephone CHAR(20), (> street CHAR(40), (> city CHAR(25), (> state CHAR(2), (> zipcode CHAR(10), (> country CHAR(20) (> );

=> CREATE TABLE employee ( (> employee_id INTEGER, (> name CHAR(30), (> hire_date DATE (> );

=> CREATE TABLE part ( (> part_id INTEGER, (> name CHAR(30), (> cost NUMERIC(8,2), (> weight FLOAT (> );

=> CREATE TABLE salesorder ( (> order_id INTEGER, (> customer_id INTEGER, -- joins to ustomer.customer_id (> employee_id INTEGER, -- joins to employee.employee_id (> part_id INTEGER, -- joins to part.part_id (> order_date DATE, (> ship_date DATE, (> payment NUMERIC(8,2) (> );

Insertion into company tables


=> INSERT INTO customer VALUES ( (> 648, (> 'Fleer Gearworks, Inc.', (> '1-610-555-7829', (> '830 Winding Way', (> 'Millersville', (> 'AL', (> '35041', (> 'USA' (> ); INSERT 19815 1

=> INSERT INTO employee VALUES ( (> 24, (> 'Lee Meyers', (> '10/16/1989' (> ); INSERT 19816 1

=> INSERT INTO part VALUES ( (> 153, (> 'Garage Door Spring', (> 6.20 (> ); INSERT 19817 1

=> INSERT INTO salesorder VALUES( (> 14673, (> 648, (> 24, (> 153, (> '7/19/1994', (> '7/28/1994', (> 18.39 (> ); INSERT 19818 1

Three Table Join


=> SELECT customer.name, employee.name -> FROM salesorder, customer, employee -> WHERE salesorder.customer_id = customer.customer_id AND -> salesorder.employee_id = employee.employee_id AND -> salesorder.order_id = 14673;

Four Table Join


=> SELECT customer.name AS customer_name, -> employee.name AS employee_name, -> part.name AS part_name -> FROM salesorder, customer, employee, part -> WHERE salesorder.customer_id = customer.customer_id AND -> salesorder.employee_id = employee.employee_id AND -> salesorder.part_id = part.part_id AND -> salesorder.order_id = 14673;

You might also like