IKG2I4 / Software Project I Mahmud Imrona, M.T. Izzatul Ummah, M.T. Kelompok Keahlian Algoritma dan Komputasi
LECTURE NOTE WEEK 10
1
3/11/2015
WEEK 10 Complex query (aggregate function): COUNT, MAX, MIN, SUM, AVG Klausa GROUP BY. Klausa HAVING. JOIN dan tipe-tipenya: natural join (USING, ON), self-join, outer join (left, right, full), Cartesian Product (cross join).
2
3/11/2015
IKG2I4 – Software Project I
COMPLEX QUERY (AGGREGATE FUNCTION) SQL menyediakan beberapa fungsi agregat (aggregate function: SUM/MAX/MIN/AVG/COUNT) untuk menampilkan resume statistik dari suatu tabel, contohnya nilai tertinggi/terendah, ratarata, jumlah, dsb. Contoh: menampilkan gaji terendah/tertinggi, rata-rata dan jumlah total (dari tabel employees)
3
3/11/2015
IKG2I4 – Software Project I
AGGREGATE FUNCTION + GROUP BY Query Aggregate function terhadap suatu tabel dapat disertai pengelompokan data berdasarkan klasifikasi tertentu, misalnya per departemen.
4
3/11/2015
IKG2I4 – Software Project I
KLAUSA HAVING Klausa HAVING adalah tambahan kondisi untuk query aggregate function. HAVING mirip seperti WHERE. Bedanya HAVING digunakan pada query yang mengandung aggregate function.
5
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Eksekusi query-query berikut, amati hasil yang muncul, dan pahami mengenai arti query tersebut: SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary) FROM employees WHERE job_id LIKE '%REP%'; SELECT MIN(hire_date), MAX(hire_date) FROM employees; SELECT COUNT(*) FROM employees WHERE department_id = 50; SELECT COUNT(DISTINCT department_id) FROM employees; 6
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Eksekusi query-query berikut, amati hasil yang muncul, dan pahami mengenai arti query tersebut: SELECT AVG(commission_pct) FROM employees; SELECT department_id, AVG(salary) FROM employees GROUP BY department_id; SELECT AVG(salary) FROM employees GROUP BY department_id;
7
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Eksekusi query-query berikut, amati hasil yang muncul, dan pahami mengenai arti query tersebut: SELECT department_id dept_id, job_id, SUM(salary) FROM employees GROUP BY department_id, job_id ORDER BY department_id; SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)>10000;
8
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Eksekusi query-query berikut, amati hasil yang muncul, dan pahami mengenai arti query tersebut: SELECT job_id, SUM(salary) FROM employees WHERE job_id NOT LIKE '%REP%' GROUP BY job_id HAVING SUM(salary) > 13000 ORDER BY SUM(salary); SELECT MAX(AVG(salary)) FROM employees GROUP BY department_id;
9
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Buatlah query SQL untuk menampilkan data berikut ini: Find the highest, lowest, sum, and average salary of all employees. Label the columns as Maximum, Minimum, Sum, and Average, respectively. Then, modify the query you have created, to display the minimum, maximum, sum, and average salary for each job type.
10
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI Write a query to display the number of people with the same job.
11
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI Find the difference between the highest and lowest salaries. Label the column DIFFERENCE.
12
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Eksekusi query-query berikut, amati hasil yang muncul, dan pahami mengenai arti query tersebut: SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations; SELECT employee_id, last_name, location_id, department_id FROM employees JOIN departments USING (department_id); 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); 13
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Eksekusi query-query berikut, amati hasil yang muncul, dan pahami mengenai arti query tersebut: 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 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) WHERE e.manager_id = 149;
14
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Eksekusi query-query berikut, amati hasil yang muncul, dan pahami mengenai arti query tersebut: 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); 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);
15
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Eksekusi query-query berikut, amati hasil yang muncul, dan pahami mengenai arti query tersebut: SELECT e.last_name, d.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON (e.department_id = d.department_id); SELECT last_name, department_name FROM employees CROSS JOIN departments;
16
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI – Buatlah query SQL untuk menampilkan data berikut ini: Write a query for the HR department to produce the addresses of all the departments. Use the LOCATIONS and COUNTRIES tables. Show the location ID, street address, city, state or province, and country in the output. Use a NATURAL JOIN to produce the results.
17
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI The HR department needs a report of all employees. Write a query to display the last name, department number, and department name for all the employees.
18
3/11/2015
IKG2I4 – Software Project I
TUGAS PEKAN INI The HR department needs a report of employees in Toronto. Display the last name, job, department number, and the department name for all employees who work in Toronto.
19
3/11/2015
IKG2I4 – Software Project I
THANK YOU