1. S E L E C T
h t t p : / / s p a d a . t e k n o k r a t . a c . i d
2. SQL – OPERATOR ARITMATIKA
• Pada ekspresi SQL dengan tipe data Number dan Date dapat digunakan
operator aritmatika.
Operator Precedence Deskripsi
+ Tambah
- Kurang
* Kali
/ Bagi
3. SQL – OPERATOR ARITMATIKA
• Contoh: (jika memiliki kolom pada tabel employees) seperti di bawah ini
4. SQL – OPERATOR ARITMATIKA
• Contoh:
Jika menggunakan statement :
SELECT LAST_NAME, SALARY, SALARY+300
FROM EMPLOYEES;
12. 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.
• Basically aliases are created to make column names more
readable.
13. SQL – ALIASES
• SQL Alias Syntax for Columns
SELECT column_name AS alias_name
FROM table_name;
14. SQL – ALIASES
• Contoh: Kasus 1
• (Jika ingin membuat last_name menjadi name, dan
commission_pct menjadi comm)
15. SQL – ALIASES
• Contoh:
SELECT last_name AS name,
commission_pct AS comm FROM employees;
16. SQL – ALIASES
• Contoh: Hasil (Jika ingin membuat last_name menjadi name,
dan commission_pct menjadi comm)
17. SQL – ALIASES
• Contoh 2: Kasus 2
• (Jika ingin membuat last_name menjadi name, dan kolom
salary dikalikan 12 menjadi Annual Salary)
18. 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;
19. SQL – ALIASES
• Contoh 2: Hasil (Jika ingin membuat last_name menjadi
name, dan kolom salary dikalikan 12 menjadi Annual Salary)
20. SQL – ALIASES
• SQL Alias Syntax for Tables
SELECT column_name(s) FROM table_name AS
alias_name;
21. 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).
22. 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
24. 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;
25. 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;
27. 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
• Contoh : (Kasus 2)
• The following SQL statement selects only the distinct values
from the "City" columns from the "Customers" table
31. 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 – 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
35. 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
36. SQL – OPERATOR PEMBANDING
• Contoh: Statement
SELECT last_name, salary FROM
employees WHERE salary <= 3000;
38. 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
40. SQL – OPERATOR PEMBANDING
• The SQL BETWEEN Operator
• The BETWEEN operator selects values within a range.
• The values can be numbers, text, or dates.
41. SQL – OPERATOR PEMBANDING
• The SQL BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
42. 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
43. SQL – OPERATOR PEMBANDING
• Example of The SQL BETWEEN
• Kasus :
• The following SQL statement selects all products with a price
BETWEEN 10 and 20
44. SQL – OPERATOR PEMBANDING
• Example of The SQL BETWEEN
• Answer:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
45. SQL – OPERATOR PEMBANDING
• Example of The SQL BETWEEN
• Kasus :
• Bagaimana untuk menampilkan products diluar dari range 10
dan 20 ??
46. SQL – OPERATOR PEMBANDING
• Example of The SQL BETWEEN
• Answer:
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
47. 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?
48. 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);
49. 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’?
50. SQL – OPERATOR PEMBANDING
• Example of The SQL BETWEEN
• Answer:
SELECT * FROM Products
WHERE ProductName BETWEEN 'C' AND
'M';
52. 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
WHERE column_name IN
(value1,value2,...);
53. 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
5 Berglunds snabbköp Christina Berguvsvägen 8 Luleå S-958 22 Sweden
54. SQL IN
• Case:
• The Following SQL Statement selects all customers with a
City of ‘London’ or ‘Berlin’?
57. 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.
• _ : menunjukkan sembarang 1 karakter
58. SQL LIKE
• The SQL LIKE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
64. 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
Berguvsvägen 8 Luleå S-958 22 Sweden
65. 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
68. 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
69. 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.
70. LOGICAL CONDITIONS
• Example : Below is a selection from the “Customers” table
Custo
merID
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
71. LOGICAL CONDITIONS
• Example :
• Kasus 1
• How to Selects all customers from country “Germany”
AND the city “Berlin”, in the Customers table?
75. 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?
76. LOGICAL CONDITIONS
• Example 3:
• Answer 3
SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='München');
79. 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
80. SQL – OPERASI PEMBANDING
• Example of Rules of Precedence
81. SQL – OPERASI PEMBANDING
• Example of Rules of Precedence
83. 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.
• Fungsi ini digunakan bersama dengan pernyataan SELECT.
84. 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
85. JENIS FUNGSI AGREGAT
• Example :
• Berikut ini adalah latihan dalam menggunakan fungsi agregat.
• Buatlah tabel buku dengan struktur tabel sebagai berikut : ->
Next Slide
89. 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 |
+-----------+------------------------+------------------+---------------+-------+-----------+------+----------------+
90. 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?
92. JENIS FUNGSI AGREGAT
• Example : Implementasi Fungsi Agregat
1. Fungsi COUNT
Bagaimana cara menghitung jumlah record tabel buku dengan
nama kolom jum_rec?
94. JENIS FUNGSI AGREGAT
• Example : Implementasi Fungsi Agregat
1. Fungsi COUNT
Bagaimana menghitung jumlah record untuh tahun 2013 yang
diberi nama jum_rec?
96. JENIS FUNGSI AGREGAT
• Example : Implementasi Fungsi Agregat
2. Fungsi Agregat SUM
Fungsi SUM digunakan untuk menghitung total nilai dari kolom
tertentu.
97. 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?
99. 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?
101. JENIS FUNGSI AGREGAT
• Example : Implementasi Fungsi Agregat
3. Fungsi Agregat MAX
Digunakan untuk menampilkan nilai tertinggi dari suatu kolom.
102. 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?
104. JENIS FUNGSI AGREGAT
• Example : Implementasi Fungsi Agregat
3. Fungsi Agregat MAX
Bagaimana untuk menampilkan harga tertinggi untuk tahun
2013?
105. 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?
107. 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?
111. 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?
113. 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?
115. 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
• [URL] : http://fairuzelsaid.com/