• Tidak ada hasil yang ditemukan

P E R T E M U A N 3 - S Q L Q U E RY

N/A
N/A
Protected

Academic year: 2021

Membagikan "P E R T E M U A N 3 - S Q L Q U E RY"

Copied!
118
0
0

Teks penuh

(1)

K O N S E P S I S T E M I N F O R M A S I B

(2)

OBJECTIVES

• Struktur SQL Query – Pertemuan M3

• Operator Aritmatika

• Penggunaan Kolom Alias

• Operator pembanding & Operasi Himpunan

• Operator Boolean

• Pencarian String

• Penggunaan Distinct

(3)
(4)

SQL – OPERATOR ARITMATIKA

Pada ekspresi SQL dengan tipe data Number dan Date dapat digunakan operator aritmatika.

Operator Precedence Deskripsi

+ Tambah

- Kurang

* Kali

(5)

SQL – OPERATOR ARITMATIKA

(6)

SQL – OPERATOR ARITMATIKA

• Contoh:

Jika menggunakan statement :

SELECT

LAST_NAME, SALARY, SALARY+300

(7)

SQL – OPERATOR ARITMATIKA

(8)

SQL – OPERATOR ARITMATIKA

(9)

SQL – OPERATOR ARITMATIKA

• Contoh 2 : (Jika menggunakan dua operator sekaligus)

SELECT

LAST_NAME, SALARY, 12*SALARY+100

FROM

EMPLOYEES

;

SELECT

LAST_NAME, SALARY, 12*(SALARY+100)

(10)

SQL – OPERATOR ARITMATIKA

(11)

SQL – OPERATOR ARITMATIKA

(12)

SQL – OPERATOR ARITMATIKA

(13)
(14)

SQL – ALIASES

• SQL aliases are used to temporarily rename a table or a column heading.

• SQL aliases are used to give a database table, or a column in a table, a temporary name.

(15)

SQL – ALIASES

• SQL Alias Syntax for Columns

SELECT column_name AS alias_name

FROM table_name;

(16)

SQL – ALIASES

• Contoh: Kasus 1

• (Jika ingin membuat last_name menjadi name, dan

(17)

SQL – ALIASES

• Contoh:

SELECT last_name AS name,

(18)

SQL – ALIASES

• Contoh: Hasil (Jika ingin membuat last_name menjadi name, dan

(19)

SQL – ALIASES

• Contoh 2: Kasus 2

• (Jika ingin membuat last_name menjadi name, dan kolom salary dikalikan 12 menjadi Annual Salary)

(20)

SQL – ALIASES

• Contoh 2: (Jika ingin membuat last_name menjadi name, dan kolom salary dikalikan 12 menjadi Annual Salary)

SELECT last_name “Name”, salary*12 “Annual Salary”

FROM employees;

(21)

SQL – ALIASES

• Contoh 2: Hasil (Jika ingin membuat last_name menjadi name, dan kolom salary dikalikan 12 menjadi Annual Salary)

(22)

SQL – ALIASES

• SQL Alias Syntax for Tables

SELECT column_name(s) FROM table_name AS

alias_name;

(23)

SQL – ALIASES

• Contoh for Table Columns:

• The following SQL Statement selects all the orders from the

Customer with CustomerID=4 (Around the Horn).

• We use the “Customers” and “Orders” tables, and give them tables aliases of “c” and “o” respectively (Here we have used aliases to make the SQL shorter).

(24)

SQL – ALIASES

• Tabel Customers CustomerI D CustomerNam e ContactNam e

Address City PostalCode Country 2 Ana Trujillo

Emparedados y helados

Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico 3 Antonio Moreno Taquería Antonio Moreno

Mataderos 2312 México D.F. 05023 Mexico

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

(25)

SQL – ALIASES

• Tabel Orders

OrderID CustomerID EmployeeID OrderDate ShipperID

10354 58 8 1996-11-14 3

10355 4 6 1996-11-15 1

(26)

SQL – ALIASES

Contoh Statement: (with aliases)

SELECT o.OrderID, o.OrderDate, c.CustomerName

FROM Customers AS c, Orders AS o

WHERE c.CustomerName="Around the Horn" AND

c.CustomerID=o.CustomerID;

(27)

SQL – ALIASES

• Contoh Statement: (without aliases)

SELECT Orders.OrderID, Orders.OrderDate,

Customers.CustomerName

FROM Customers, Orders

WHERE Customers.CustomerName="Around the Horn"

AND Customers.CustomerID=Orders.CustomerID;

(28)
(29)

SQL SELECT DISTINCT

• The SELECT DISTINCT statement is used to return only distinct (diferent) values.

• In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.

• The DISTINCT keyword can be used to return only distinct (different) values.

(30)

SQL SELECT DISTINCT

• SQL SELECT DISTINCT Syntax

SELECT DISTINCT column_name,

(31)

SQL SELECT DISTINCT

(32)

SQL SELECT DISTINCT

• Contoh : (Kasus 2)

• The following SQL statement selects only the distinct values from the "City" columns from the "Customers" table

(33)

SQL SELECT DISTINCT

• Contoh : (Kasus 2) Tabel Customer

CustomerID CustomerName ContactName Address City PostalCode Country 1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo Avda. de la

Constitución 2222

México D.F. 05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico 4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

(34)

SQL SELECT DISTINCT

• Contoh : (Kasus 2)

SELECT DISTINCT City FROM

Customers;

(35)
(36)

SQL – OPERATOR PEMBANDING

• Berikut ini Operator pembanding dalam SQL

Operator

Meaning

=

Equal to

>

Greater than

>=

Greater than or Equal to

<

Less than

<=

Less than or equal to

<>

Not Equal to

(37)

SQL – OPERATOR PEMBANDING

• Contoh: (Jika ingin menampilkan salary kurang dari sama dengan 3000 Last_name salary Matos 2600 Vargas 2500 Jose 3100 Santos 3500 Marquez 3100

(38)

SQL – OPERATOR PEMBANDING

• Contoh: Statement

SELECT last_name, salary FROM

employees WHERE salary <= 3000;

(39)

SQL – OPERATOR PEMBANDING

(40)

SQL – OPERATOR PEMBANDING

Operator Pembanding Kondisi lainnya (Other Comparison

Conditions)

Operator Meaning

BETWEEN … AND …. Between two values (inclusive) IN (set) Match any of a list of values LIKE Match a character pattern IS NULL Is a null value

(41)
(42)

SQL – OPERATOR PEMBANDING

The SQL BETWEEN Operator

• The BETWEEN operator selects values within a range.

(43)

SQL – OPERATOR PEMBANDING

The SQL BETWEEN Syntax

SELECT column_name(s)

FROM table_name

(44)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Below is a selection from the ‘Products’ table

ProductI D

ProductName SupplierID CategoryI D

Unit Price 1 Chais 1 1 10 boxes x 20BAGS 18 2 Chang 1 1 24 - 12 oz bottles 19 3 Aniseed Syrup 1 2 12 - 550 ml bottles 10 4 Chef Anton's Cajun Seasoning 1 2 48 - 6 oz jars 22 5 Chef Anton's Gumbo Mix 1 2 36 boxes 21.35

(45)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Kasus :

• The following SQL statement selects all products with a price BETWEEN 10 and 20

(46)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Answer:

SELECT * FROM Products

(47)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Kasus :

• Bagaimana untuk menampilkan products diluar dari range 10 dan 20 ??

(48)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Answer:

SELECT * FROM Products

(49)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Kasus :

• The following SQL statement selects all products with a price BETWEEN 10 and 20, but products with a CategoryID of 1,2, or 3 should not be displayed?

(50)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Answer:

SELECT * FROM Products

WHERE (Price BETWEEN 10 AND 20)

AND NOT CategoryID IN (1,2,3);

(51)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Kasus:

• The following SQL statement selects all products with a ProductName beginning with any of the letter BETWEEN ‘C’ and ‘M’?

(52)

SQL – OPERATOR PEMBANDING

Example of The SQL BETWEEN

• Answer:

SELECT * FROM Products

(53)
(54)

SQL IN

• The IN operator allows you to specify multiple values in a WHERE clause.

SQL IN Syntax

SELECT column_name(s)

FROM table_name

(55)

SQL IN

• Example : Below is a selection from the ‘Customers’ table

CustomerI D

CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo

Emparedados y helados

Ana Trujillo Avda. de la

Constitución 2222

México D.F. 05021 Mexico

3 Antonio Moreno Taquería

Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

(56)

SQL IN

• Case:

The Following SQL Statement selects all customers with a City of ‘London’ or ‘Berlin’?

(57)

SQL IN

• Answer:

SELECT * FROM Customers

(58)
(59)

SQL LIKE

• The LIKE operator is used in a WHERE clause to search for a specified patern in a column.

• Gunakan kondisi LIKE untuk melakukan pencarian sebagian nilai string.

• Kondisi pencarian dapat menggunakan symbol karakter berikut:

% : Menunjukkan nol/kosong atau sembarang beberapa karakter.

(60)

SQL LIKE

• The SQL LIKE Syntax

SELECT column_name(s)

FROM table_name

(61)

SQL LIKE

(62)

SQL LIKE

• Example:

• Kasus 1:

(63)

SQL LIKE

• Example:

(64)

SQL LIKE

• Example:

• Kasus 2

• Bagaimana menampilkan nama terakhir yang huruf keduanya mengandung huruf o?

(65)

SQL LIKE

• Example:

(66)

SQL LIKE

• Exercise:

CustomerID CustomerName ContactName Address City PostalCode Country 1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Emparedados y helados

Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico 3 Antonio Moreno Taquería Antonio Moreno

Mataderos 2312 México D.F. 05023 Mexico 4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds snabbköp

Christina Berglund

(67)

SQL LIKE

• Exercise:

1. How to selects all customers with a City starting with the letter “s”?

2. How to selects all customers with a City ending with the letter “s”?

3. How to selects all customers with a Country containing the pattern “land”?

4. How to selects all customers with Country NOT containing the pattern “land’?

(68)

SQL – USING THE NULL

CONDITIONS

(69)
(70)

LOGICAL CONDITIONS

• The AND, OR, & NOT operators are used to filter records based on more than one condition.

Operator Arti

AND Returns TRUE, jika kedua kondisi adalah TRUE OR Returns TRUE, jika salah satu kondisi adalah TRUE NOT Returns TRUE, jika kondisi tersebut adalah False

(71)

LOGICAL CONDITIONS

The SQL AND & OR Operators

• The AND operator display a record if both the first condition AND the second condition are true.

• The OR operator displays a record if either the first condition OR the second condition is true.

(72)

LOGICAL CONDITIONS

Example : Below is a selection from the “Customers” table Custom

erID

CustomerName ContactName Address City PostalCode Country 1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany 2 Ana Trujillo Emparedados y

helados

Ana Trujillo Avda. de la Constitución 2222

México D.F. 05021 Mexico 3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico 4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden

(73)

LOGICAL CONDITIONS

Example :

Kasus 1

How to Selects all customers from country “Germany” AND the city “Berlin”, in the Customers table?

(74)

LOGICAL CONDITIONS

Example :

Answer 1

SELECT * FROM Customers

WHERE Country='Germany'

(75)

LOGICAL CONDITIONS

Example :

Kasus 2

How to selects all customers from the city ‘Berlin” OR “Munchen”, in the Customers table?

(76)

LOGICAL CONDITIONS

Example :

Answer 2

SELECT * FROM Customers

WHERE City='Berlin'

(77)

LOGICAL CONDITIONS

Example 3:

Kasus 3

How to selects all customers from the country “Germany” AND the city must be equal to “Berlin” OR “Munchen”, in the Customers table?

(78)

LOGICAL CONDITIONS

Example 3:

Answer 3

SELECT * FROM Customers

WHERE Country='Germany'

(79)

LOGICAL CONDITIONS

(80)

LOGICAL CONDITIONS

(81)

SQL – OPERASI PEMBANDING

Rules of Precedence

Order Evaluated Operators

1 Arithmetic Operators 2 Concatenation operator 3 Comparison conditions

4 IS [NOT] NULL, LIKE, [NOT] IN 5 [NOT] BETWEEN

6 NOT logical condition 7 AND logical condition 8 OR logical condition

(82)

SQL – OPERASI PEMBANDING

(83)

SQL – OPERASI PEMBANDING

(84)
(85)

FUNGSI AGREGAT

• Fungsi Agregat dalam SQL adalah fungsi yang menerima kumpulan atau koleksi dan mengembalikan nilai tunggal sebagai hasilnya, seperti: Jumlah data, nilai minimum, nilai maximum dan nilai rata-rata.

(86)

JENIS FUNGSI AGREGAT

• Standar ISO mendefinisikan lima jenis fungsi agregat, yaitu:

Fungsi Penjelasan

SUM Digunakan untuk menghitung total nilai dari kolom tertentu COUNT Digunakan untuk menghitung jumlah record

AVG Digunakan untuk menampilkan nilai rata-rata dari suatu kolom MAX Digunakan untuk menampilkan nilai tertinggi dari suatu kolom MIN Digunakan untuk menampilkan nilai terendah dari suatu kolom

(87)

JENIS FUNGSI AGREGAT

• Example :

• Berikut ini adalah latihan dalam menggunakan fungsi agregat.

• Buatlah tabel buku dengan struktur tabel sebagai berikut : -> Next Slide

(88)

JENIS FUNGSI AGREGAT

(89)

JENIS FUNGSI AGREGAT

(90)

JENIS FUNGSI AGREGAT

(91)

JENIS FUNGSI AGREGAT

• Example : Jika ditampilkan dari tabel buku

mysql> SELECT * FROM buku;

+---+---+---+---+---+---+---+---+ | kode_buku | judul_buku | pengarang | penerbit | tahun | kategori | harga| tgl_inventaris | +---+---+---+---+---+---+---+---+ | B0001 | Harry Potter | JK Rowling | British Press | 2013 | Fiksi | 50000| 2015-03-01 | | B0002 | Sistem Basis Data | Abdul Kadir | Andi Offset | 2013 | Buku Teks | 40000| 2015-03-01 | | B0003 | Sistem Basis Data | Fathansyah | ITB Press | 2013 | Buku Teks | 30000| 2015-03-01 | | B0004 | Prophet Muhammad | Amir Abdullah | Madina Press | 2014 | Biografi | 45000| 2015-03-01 | | B0005 | Ketika Cinta Bertasbih | Habiburahaman ES | Madina Press | 2014 | Fiksi | 75000| 2015-02-01 | | B0006 | Pemrograman Basis Data | Abdul Kadir | Andi Offset | 2015 | Buku Teks | 67000| 2015-02-01 | +---+---+---+---+---+---+---+---+

(92)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat

1. Fungsi COUNT

Digunakan untuk menghitung jumlah record.

Example: Bagaimana menghitung jumlah jenis buku di dalam tabel buku?

(93)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat

(94)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat

1. Fungsi COUNT

Bagaimana cara menghitung jumlah record tabel buku dengan nama kolom jum_rec?

(95)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat

(96)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat

1. Fungsi COUNT

Bagaimana menghitung jumlah record untuh tahun 2013 yang diberi nama jum_rec?

(97)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat

(98)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 2. Fungsi Agregat SUM

Fungsi SUM digunakan untuk menghitung total nilai dari kolom tertentu.

(99)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 2. Fungsi Agregat SUM

Bagaiamana untuk menghitung total harga dari kolom harga dengan nama kolom menjadi total_harga?

(100)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 2. Fungsi Agregat SUM

(101)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 2. Fungsi Agregat SUM

Bagaimana untuk menghitung total harga untuk tahun 2013 dari kolom kolom harga dengan nama kolom menjadi total_harga?

(102)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 2. Fungsi Agregat SUM

(103)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 3. Fungsi Agregat MAX

(104)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 3. Fungsi Agregat MAX

Example:

Bagaimana cara menampilkan harga tertinggi dari kolom harga dan ditampilkan dengan nama kolom harga_tertinggi?

(105)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 3. Fungsi Agregat MAX

(106)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 3. Fungsi Agregat MAX

(107)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 3. Fungsi Agregat MAX

Bagaimana untuk menampilkan harga tertinggi untuk tahun 2013 dan ditampilkan dengan nama harga_tertinggi?

(108)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 3. Fungsi Agregat MAX

(109)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 4. Fungsi Agregat MIN

Digunakan untuk menampilkan nilai terendah dari suatu kolom Example:

Bagaimana untuk menampilkan nilai terendah dengan nama kolom harga_terendah?

(110)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 4. Fungsi Agregat MIN

(111)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 4. Fungsi Agregat MIN

(112)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 4. Fungsi Agregat MIN

(113)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 5. Fungsi Agregat AVG

Digunakan untuk menampilkan nilai rata-rata dari suatu kolom.

Bagaimana untuk menampilkan nilai rata rata dari harga dalam tabel buku dengan nama kolom harga_rerata?

(114)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 5. Fungsi Agregat AVG

(115)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 5. Fungsi Agregat AVG

Bagaimana menampilkan rata rata harga pada tahun 2013 dengan nama kolom harga_rerata dari tabel buku?

(116)

JENIS FUNGSI AGREGAT

• Example : Implementasi Fungsi Agregat 5. Fungsi Agregat AVG

(117)

SUMBER

• Solichin, Achmad. MySQL 5 : Dari Pemula Hingga Akhir. Buku Komputer Gratis. 2010.

• SQL, Basis Data 1, Chapter 2, PENS ITS

• [URL] : http://www.w3schools.com/sql

(118)

Referensi

Dokumen terkait

Ketidakmampuan manusia dalam menjalankan kehidupan sehari- hari akan mendorong manusia untuk selalu mengadakan hubungan timbal balik dengan sesamanya serta bertujuan

Stleta &amp;rtgada itu lr iega bcrpcrarpn : iir lGfrSat I fuhmn Pcnt*dblrrn lgrs taier aagar I. ' rii ecnfubll tlsdrtrn bEt

Apabila terdapat informasi yang dianggap kurang jelas atau terdapat masalah yang terkait dengan proses pendaftaran, calon peserta USM dapat menghubungi Panitia Pelaksana

Mahasiswa Fakultas Kedokteran Gigi Universitas Syiah Kuala Suku Aceh berejenis kelamin laki-laki memiliki nilai rerata tinggi wajah anterior bawah yang lebih besar dari

Dalam aspek produksi yang lain terutama pengadaan bibit, perkebunan rakyat didukung oleh Pusat Penelitian Kopi dan Kakao Jember, sehingga mutu kopi yang ditanam

Dari hasil penelitian yang telah dilakukan di Puskesmas Ranomuut Kota Manado, maka dapat disimpulkan bahwa, Sebagian besar responden berada pada usia Elderly

(1) Dalam hal kepentingan tertentu Kepal Dinas dapat menunjuk satu atau beberapa laboratorium terakreditasi dan atau rujukan untuk melakukan pengujian kualitas air limbah

5 Saya suka bertanya tentang konsep biologi yang belum jelas 6 Saya senang jika guru memberi reward/hadiah pada saya 7 Saya sedih jika guru member pusihment/hukuman pada saya 8