Sesi 14 DML 2
DATA MANIPULATION
LANGUAGE (DML)
Materi JOIN
Gambaran Umum
Kegunaan :
Digunakan untuk menampilkan dari banyak tabel
Mengkombinasikan seluruh kemungkinan data antar banyak tabel
Mendapatkan informasi yang tersimpan pada tabel lain (melakukan
relasi antara satu tabel dengan tabel lainnya)
Cartesian Product
Tipe dari Join
Inner Join
Outer Join
Self Join
Data didapat dari banyak Tabel
EMPLOYEES DEPARTMENTS
Data from Multiple Tables
Sometimes you need to use data from more than one table.
In the slide example, the report displays data from two separate tables.
Employee IDs exist in the EMPLOYEES table.
Department IDs exist in both the EMPLOYEES and DEPARTMENTS tables
Location IDs exist in the DEPARTMENTS table.
To produce the report, you need to link the EMPLOYEES and DEPARTMENTS tables
and access data from both of them.
Cartesian Products
Cartesian-product ( X ), adalah operasi untuk
menghasilkan table hasil perkalian kartesian.
Operasi cartesian-product memungkinkan kita
mengkombinasikan informasi beberapa relasi,
operasi ini adalah operasi biner. Sebagaimana
telah dinyatakan bahwa relasi adalah subset
hasil cartesian-product dan himpunan domain
relasi relasi tersebut. Kita harus memilih
atribut atribut untuk relasi yang dihasilkan
dari cartesian-product.
Membuat Cartesian Product
EMPLOYEES (20 rows) DEPARTMENTS (8 rows)
Cartesian
product:
20x8=160 rows
Tipe dari Join
Inner Join
Equijoin
Natural Join
Cross Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join
Self Join
Apa Equijoin?
EMPLOYEES DEPARTMENTS
Bentuk lain ON clause :
DEPARTMENTS EMPLOYEES
Tidak ada employee di
departemen 190.
LEFT OUTER JOIN
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
Kondisi Tambahan
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
AND e.manager_id = 149 ;
Self Join
relasi dengan diri sendiri
Self Joins
DATA MANIPULATION
LANGUAGE (DML)
Materi AGGREGATION
Outline
Overview
Function Type and Syntax
AVG and SUM
MIN and MAX
COUNT
NVL
GROUP BY
HAVING
Nested Functions
Overview
Oftentimes, we're also interested in summarizing our data to
determine trends or produce top-level reports.
Fortunately, SQL provides aggregate functions to assist with
the summarization of large volumes of data. In this three-
segment article, we'll look at functions that allow us to add
and average data, count records meeting specific criteria and
find the largest and smallest values in a table.
In computer science, an aggregate function is a function that
returns a single value from a collection of input values.
Group Functions Syntax
You can use AVG and SUM functions against columns that can store
numeric data.
Using the MIN and MAX Functions
You can use MIN and MAX for any data type.
SELECT MIN(hire_date), MAX(hire_date)
FROM employees;
You can use the MAX and MIN functions for any data type.
Return n/a if supplier field contained a null value. Otherwise, it would return
supplier value
9500
The
average
3500 salary
in
EMPLOYEES
6400 table
for each
department.
10033
Add up the
salaries in
the EMPLOYEES
table
for each job,
grouped by
department.
Using the GROUP BY Clause
on Multiple Columns
SELECT department_id dept_id, job_id, SUM(salary)
FROM employees
GROUP BY department_id, job_id ;
Illegal Queries
Using Group Functions
Any column or expression in the SELECT list that is not an aggregate function must
be in the GROUP BY clause.
Whenever you use a mixture of individual items (DEPARTMENT_ID) and group
functions (COUNT) in the same SELECT statement, you must include a GROUP
BY clause that specifies the individual items (in this case, DEPARTMENT_ID).