Anda di halaman 1dari 48

BASIS DATA

Muhammad Nasrun, S.Si, MT

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

Foreign key Primary key


relasi antar tabel dengan nilai data yang sama tepat
Menampilkan Record dengan ON
Clause
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);


Bentuk lain ON clause :

SELECT e.last_name emp, m.last_name mgr


FROM employees e JOIN employees m
ON (e.manager_id = m.employee_id);
Menampilkan Records dengan USING
Clause
SELECT e.employee_id, e.last_name, d.location_id
FROM employees e JOIN departments d
USING (department_id) ;

SELECT employee_id, last_name,


employees.department_id, location_id
FROM employees, departments
WHERE employees.department_id = departments.department_id;
Join lebih dari 2 Tabel
EMPLOYEES DEPARTMENTS LOCATIONS

Join dengan n tabel bersamaam, kita


dapatkan dari minimum n-1 kondisi join.
Membuat Join dengan ON
Clause
SELECT employee_id, city, department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id
JOIN locations l
ON d.location_id = l.location_id;

SELECT employee_id, city, department_name


FROM employees, departments, locations
WHERE employees.department_id = departments.department_id
AND departments.location_id = locations.location_id;
Definisi
Operasi Natural Join
Notasi :
Misal terdapat relasi r dan s pada skema R dan S. Maka, merupakan
sebuah relasi pada skema R U S yang diperoleh dengan cara berikut :
Perhatikan setiap pasangan tuple tr dari relasi r dan ts dari relasi s.
Jika tr dan ts mempunyai nilai yang sama untuk setiap atribut pada R S,
tambahkan sebuah tuple t ke relasi hasil, di mana
t mempunyai nilai yang sama dengan tr pada r
t mempunyai nilai yang sama dengan ts pada s
Contoh :
R = (A, B, C, D)
S = (E, B, D)
Skema relasi hasil = (A, B, C, D, E)
didefinisikan sebagai :
r.A, r.B, r.C, r.D, s.E (r.B = s.B ^ r.D = s.D (r x s))
Retrieving Records with Natural
Joins
SELECT department_id, department_name,
location_id, city
FROM departments
NATURAL JOIN locations ;
Cross Join
CROSS JOIN clause membuat cross-
product dari 2 tabel.
Ini sama dengan Cartesian product antara
dua tabel.
Membuat Cross Join

SELECT last_name, department_name


FROM employees
CROSS JOIN departments ;

Definisi Outer Join


Pengembangan dari operasi join, untuk menghindari
hilangnya informasi atau relasi dengan tabel yang
kemungkinan nilai datanya tidak ada
Operasi join dilakukan dan kemudian ditambahkan
tuple dari satu relasi yang tidak match dengan relasi
lainnya ke relasi hasil.
Penggunaan nilai null :
null menyatakan bahwa nilai tersebut tidak dikenal
(unknown) atau tidak ada (not exist)
Semua perbandingan yang melibatkan nilai null dianggap
salah (false by definition).
Outer Joins

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

RIGHT OUTER JOIN


SELECT e.last_name, e.department_id, d.department_name
FROM employees e
RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;

FULL OUTER JOIN

SELECT e.last_name, e.department_id, d.department_name


FROM employees e
FULL 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

EMPLOYEES (WORKER) EMPLOYEES (MANAGER)

MANAGER_ID pada tabel WORKER sama dengan


EMPLOYEEn _ID pada tabel MANAGER
Join Tabel dengan diri sendiri
SELECT worker.last_name || ' works for '
|| manager.last_name
FROM employees worker JOIN employees manager
ON (worker.manager_id = manager.employee_id) ;

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

SELECT [column,] group_function(column), ...


FROM table
[WHERE condition]
[GROUP BY column]
[ORDER BY column];
Using the AVG and SUM Functions

You can use AVG and SUM for numeric data.


SELECT AVG(salary), MAX(salary),
MIN(salary), SUM(salary)
FROM employees
WHERE job_id LIKE '%REP%';

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.

SELECT MIN(last_name), MAX(last_name)


FROM employees;
Using the COUNT Function

COUNT(*) returns the number of rows in a


table.
SELECT COUNT(*)
FROM employees
WHERE department_id = 50;
Using the COUNT Function

COUNT(expr) returns the number of rows


with non-null values for the expr.
Display the number of department values in
the EMPLOYEES table, excluding the null
values.
SELECT COUNT(commission_pct)
FROM employees
WHERE department_id = 80;
Using the DISTINCT Keyword

COUNT(DISTINCT expr) returns the


number of distinct non-null values of the
expr.
Display the number of distinct department
values in the EMPLOYEES table.
SELECT COUNT(DISTINCT department_id)
FROM employees;
Group Functions and Null Values

Group functions ignore null values in the


column.
SELECT AVG(commission_pct)
FROM employees;
Using the NVL Function
with Group Functions
The NVL function forces group functions to
include null values.

SELECT AVG(NVL(commission_pct, 0))


FROM employees;
NVL Function
substitute a value when a null value is
encountered
SELECT NVL(supplier, n/a)
FROM suppliers;

Return n/a if supplier field contained a null value. Otherwise, it would return
supplier value

SELECT NVL(supplier_desc, supplier_name)


FROM suppliers;

Return supplier_name field if supplier_desc contained a null value. Otherwise, it


would return supplier_desc
Creating Groups of Data
EMPLOYEES
4400

9500
The
average
3500 salary
in
EMPLOYEES
6400 table
for each
department.
10033

Creating Groups of Data:


The GROUP BY Clause Syntax
SELECT column, group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];

Divide rows in a table into smaller groups by


using the
GROUP BY clause.
Using the GROUP BY Clause
All columns in the SELECT list that are not in
group
functions must be in the GROUP BY clause.
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id ;
Using the GROUP BY Clause

The GROUP BY column does not have to be in


the SELECT list.
SELECT AVG(salary)
FROM employees
GROUP BY department_id ;
Grouping by More Than One
EMPLOYEES
Column

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

SELECT department_id, COUNT(last_name)


FROM employees;

SELECT department_id, COUNT(last_name)


*
ERROR at line 1:
ORA-00937: not a single-group group function
Excluding Group Results: The
HAVING Clause
Use the HAVING clause to restrict groups:
1. Rows are grouped.
2. The group function is applied.
3. Groups matching the HAVING clause are displayed.

SELECT column, group_function


FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
Using the HAVING Clause

SELECT department_id, MAX(salary)


FROM employees
GROUP BY department_id
HAVING MAX(salary)>10000 ;
Using the HAVING Clause

SELECT job_id, SUM(salary) PAYROLL


FROM employees
WHERE job_id NOT LIKE '%REP%'
GROUP BY job_id
HAVING SUM(salary) > 13000
ORDER BY SUM(salary);
Nesting Group Functions

Display the maximum average salary.


SELECT MAX(AVG(salary))
FROM employees
GROUP BY department_id;

Anda mungkin juga menyukai