Sie sind auf Seite 1von 47

Query

Tim MBD IF ITS 2020


Outline
1 Review about Basic Query

2 Complex Query

3 UNION, INTERSECT, and EXCEPT

4 NULL values

5 Aggregation Operator
REVIEW of BASIC
QUERY

3
Structure of Basic Query
For eliminating duplicate rows or results

SELECT [DISTINCT] select-list


FROM from-list
WHERE qualification

Usually sub query is occurred in from or


where
EXAMPLE
How to show the name of tenant
(penyewa) which reserves a facility
with kode_fasilitas F001?

SELECT P.nama_pengenal
FROM penyewa P
WHERE P.id_pengenal in
(SELECT R.id_pengenal
FROM reservasi R
WHERE R.kode_fasilitas = ‘F001’)

SELECT P.nama_pengenal
FROM penyewa P
WHERE exists
(SELECT *
FROM reservasi R
WHERE R.kode_fasilitas = ‘F001’
and R.id_pengenal = P.id_pengenal)
Complex Query

6
Complex Query
Complex Query or nested query is queries that :
1) involves several operations;
2) Involves several tables.
Complex Query
Question : Who has higher salary than Izdihar Farahdina?
Answer :
SELECT name
FROM employee
, EXCEPTS
WHERE salary > T ER SE CT
IN
(SELECT salary UNION,ed in
d
FROM employee are inclu ery
l ex Q u
WHERE name = “Izdihar Farahdina”);
Comp
UNION, INTERSECT,
and EXCEPT

9
UNION vs INTERSECT vs EXCEPT

UNION INTERSECT EXCEPT

R1 R2 R1 R2 R1 R2

R1  R2 R1  R2 R1  R2


UNION
•   R1 R2 = R1 or R2
• R1 and R2 must be union-compatible, which are:
1) Both of them have same fields;
2) All related fields has same domain.
EXAMPLE OF UNION
How to show the date and
total payment of reserving
“Lapangan Sepakbola” or
“Lapangan Basket” by
Izdihar Farahdina?
EXAMPLE OF UNION
Question : How to show the date and total payment of reserving “Lapangan Sepakbola” or
“Lapangan Basket” by Izdihar Farahdina?
Answer :
SELECT R.tanggal_reservasi, R.biaya_total SELECT R.tanggal_reservasi, R.biaya_total
FROM reservasi R, penyewa P, fasilitas F FROM reservasi R, penyewa P, fasilitas F
WHERE R.kode_fasilitas = F.kode_fasilitas WHERE R.kode_fasilitas = F.kode_fasilitas
and R.id_pengenal = P.id_pengenal and R.id_pengenal = P.id_pengenal
and P.nama_penyewa = ‘Izdihar Farahdina’ and P.nama_penyewa = ‘Izdihar Farahdina’
and (F.nama_fasilitas = ‘Lapangan sepakbola’ or and F.nama_fasilitas = ‘Lapangan sepakbola’
F.nama_fasilitas = ‘Lapangan basket’) UNION
SELECT R.tanggal_reservasi, R.biaya_total
FROM reservasi R, penyewa P, fasilitas F
WHERE R.kode_fasilitas = F.kode_fasilitas
and R.id_pengenal = P.id_pengenal
and P.nama_penyewa = ‘Izdihar Farahdina’
and F.nama_fasilitas = ‘Lapangan basket’)
INTERSECTION
•   R1 R2 = R1 and R2
• R1 and R2 must be union-compatible, which are:
1) Both of them have same fields;
2) All related fields has same domain.
EXAMPLE OF INTERSECTION
How to show the date and
total payment of
reservation by Izdihar
Farahdina and served by
Hendy Harfianto?
EXAMPLE OF INTERSECTION
Question : How to show the date and total payment of reservation by Izdihar Farahdina and served
by Hendy Harfianto?
Answer :
SELECT R.tanggal_reservasi, R.biaya_total SELECT R.tanggal_reservasi, R.biaya_total
FROM reservasi R, penyewa P, pegawai PG FROM reservasi R, penyewa P, pegawai PG
WHERE R.nip = PG.nip WHERE R.nip = PG.nip
and R.id_pengenal = P.id_pengenal and R.id_pengenal = P.id_pengenal
and P.nama_penyewa = ‘Izdihar Farahdina’ and P.nama_penyewa = ‘Izdihar Farahdina’
and PG.nama_pegawai = ‘Hendy Harfianto’ INTERSECT
SELECT R.tanggal_reservasi, R.biaya_total
FROM reservasi R, penyewa P, pegawai PG
WHERE R.nip = PG.nip
and R.id_pengenal = P.id_pengenal
and PG.nama_pegawai = ‘Hendy Harfianto’
EXCEPT
•   R1 R2 = all R1 but not in R2
• All fields should be related
EXAMPLE OF EXCEPT
How to show the name of
tenant (penyewa) and the
date of reservation which
reserves Lapangan
Sepakbola, but never
serves Lapangan Basket ?
EXAMPLE OF EXCEPT
Question : How to show the name of tenant (penyewa) and the date of reservation which reserves
Lapangan Sepakbola, but never serves Lapangan Basket ?
Answer :
SELECT P.nama_penyewa, R.tanggal_reservasi SELECT P.nama_penyewa, R.tanggal_reservasi
FROM reservasi R, penyewa P, fasilitas F FROM reservasi R, penyewa P, fasilitas F
WHERE R.kode_fasilitas = F.kode_fasilitas WHERE R.kode_fasilitas = F.kode_fasilitas
and R.id_pengenal = P.id_pengenal and R.id_pengenal = P.id_pengenal
and F.nama_fasilitas = ‘Lapangan Sepakbola’ and F.nama_fasilitas = ‘Lapangan Sepakbola’
and P.nama_penyewa not in and NOT EXISTS
(SELECT P.nama_penyewa (SELECT P2.nama_penyewa
FROM reservasi R, penyewa P, fasilitas F FROM reservasi R2, penyewa P2, fasilitas F2
WHERE R.kode_fasilitas = F.kode_fasilitas WHERE R2.kode_fasilitas = F2.kode_fasilitas
and R.id_pengenal = P.id_pengenal and R2.id_pengenal = P2.id_pengenal
and F.nama_fasilitas = ‘Lapangan Basket’) and F2.nama_fasilitas = ‘Lapangan Basket’
and P2.nama_penyewa = P.nama_penyewa)
IN vs EXIST ?
IN vs EXIST
IN : check if the element is in the given set of elements
EXIST : check if the given set is empty or not
LIST containing NULL values

NOT IN NOT EXIST

RETURN FALSE RETURN TRUE


SELECT p1.nid, p1.catatanFROM Pemeriksaan SELECT p1.nid, p1.catatanFROM Pemeriksaan p1WHERE
p1WHERE p1.catatan NOT IN (SELECT DISTINCT NOT EXISTS (SELECT DISTINCT p.catatan from Pemeriksaan p
p.catatan from Pemeriksaan p); WHERE p1.catatan = p.catatan);
NULL values

23
NULL values
• Null values is used to determine data that has not value (null).
• Operator for null values are:
• IS NULL
• IS NOT NULL
NULL values
nip
nama_pegawa
email_pegawai alamat_pegawai Question : Who did not added
i
his/her address in application?
Answer :
1 Afina Lina afina@its.ac.id Jl. Keputih II No. 14
SELECT * FROM table where
address is null;
Ahmad Charis
2 Fauzan charis@its.ac.id Jl. Arief Rachman Hakim
No.9

Dewi
3 dewi@its.ac.id Jl. Keputih II C No. 15
Rachmawati

4 Yutika Amelia yutika@its.ac.id


Effendi
NULL values
nip
nama_pegawa
email_pegawai alamat_pegawai Question : Who has address in
i
system?
Answer :
1 Afina Lina afina@its.ac.id Jl. Keputih II No. 14
SELECT * FROM table where
address is not null;
Ahmad Charis
2 Fauzan charis@its.ac.id Jl. Arief Rachman Hakim
No.9

Dewi
3 dewi@its.ac.id Jl. Keputih II C No. 15
Rachmawati

4 Yutika Amelia yutika@its.ac.id


Effendi
Aggregation Operator
Aggregation
• Aggregation function summarizes the result of an expression or several rows
and obtains a result.
• Common aggregation syntax is:
aggregate_function ([DISTINCT|ALL] expression)
Aggregation
Common Function of Aggregation

SUM

COUNT

AVG

MIN

MAX
Example
There is a table Employee
EMPLOYEE ( EMP_ID, NAME, DEPT_NAME, SALARY)

CREATE TABLE EMPLOYEE


(
EMP_ID NUMBER,
NAME VARCHAR2(50),
DEPT_NAME VARCHAR2(50),
SALARY NUMBER
);
Example
Then, inserting the data in Table Employee:
INSERT INTO EMPLOYEE VALUES (100,'ABC','ENG',50000);
INSERT INTO EMPLOYEE VALUES (101,'DEF','ENG',60000);
INSERT INTO EMPLOYEE VALUES (102,'GHI','PS',50000);
INSERT INTO EMPLOYEE VALUES (103,'JKL','PS',70000);
INSERT INTO EMPLOYEE VALUES (104,'MNO','SALES',75000);
INSERT INTO EMPLOYEE VALUES (105,'PQR','MKTG',70000);
INSERT INTO EMPLOYEE VALUES (106,‘STU','SALES',null);
COMMIT;
Example

SELECT *
FROM Employee
SUM
1st Query : Finding a total of salary
SELECT SUM(SALARY)
FROM Employee

2nd Query : Finding a total of salary for each department


SELECT SUM(SALARY)
FROM Employee
GROUP BY DEPT_NAME
IS IT CAN IF WE FIND

A TOTAL OF A GROUP (ENG)


BY USING

WHERE?
SUM

SELECT DEPT_NAME,SUM(SALARY)
FROM EMPLOYEE GROUP BY DEPT_NAME
WHERE DEPT_NAME = 'ENG';

The result is error


ORA-00933: SQL Command not properly ended
SUM
SOLUTION:
SELECT DEPT_NAME,SUM(SALARY) FROM EMPLOYEE
GROUP BY
DEPT_NAME
HAVING DEPT_NAME = 'ENG';
AVG
1st Query : Finding the average of salary of all employees

SELECT AVG(SALARY)
FROM Employee

The result is 62500. However, there are seven records in Table Employee and the average should be :
50.000
60.000
50.000
70.000 2. 5 0 0 ?
u l t is 6
75.000
he re s
70.000 WH Y t
null
375.000 / 7 = 53.571
AVG

Answer : AVG, SUM, MIN, MAX and the others (except COUNT) do not consider null values. Because
there is a null value, the right calculation of average is :
50.000
60.000
50.000
70.000
75.000
70.000
375.000 / 6 = 62.500
AVG

To get the right answer, COUNT is used.

SELECT SUM(SALARY)/COUNT(*)
FROM Employee
MIN and MAX

Finding minimum and maximum salary for each department


SELECT MIN(SALARY),MAX(SALARY),DEPT_NAME
FROM EMPLOYEE
GROUP BY DEPT_NAME;
Task
Detai l Kam ar

KodeKam ar
DataKam ar
varchar(9) <pk>
PDM of Medical Center
IDDeta i l Ka m ar varchar(2) <pk>
Kod eKama r varchar(9) <fk>
Lokasi varchar(25)
NoBed char(1)
Kel as varchar(13)

Pe me ri ksaan
RawatInap
IDPeri ksa char(4) <pk>
IDDetai l Kam ar varcha r(2 ) <fk1>
NID char(4) <fk2>
IDPasi en char(7) <fk2>
IDPasi en char(7) <fk1>
T gl M asuk date
T gl Peri ksa d ate
T gl Kel uar date
Di ag nosa varcha r(100 )
Bi aya n um eri c
Cata ta n varcha r(80)

Data Pasi en
IDPasi en char(7) <pk>
Dokter No KT P varcha r(20)
NID char(4) <pk> Na ma Pa si en varcha r(75)
KodePol i char(3) <fk> Al am atPasi e n varcha r(100 )
Nam a varchar(70) Je ni sKel char(1)
JK char(1) Gol Darah varcha r(2)
Al am atDokte r varchar(150) T g l La hi r d ate
T el pDokter varchar(14) T el p varcha r(14)
T gl M ul ai Praktek date

Pol i
KodePol i char(3 ) <pk>
Nam aPol i varch ar(7)
Task Class NRP Ganjil
1) Insert the data
DataPasien
IDPasien NoKTP Nama Pasien Alamat Pasien JK Gol. Tgl Lahir Telp
Darah
PS00001 3506031602880001 Izdihar Perumdos ITS P O 27-Jul-95 081234777888
Farahdina Blok IF No.13
PS00002 3606031002890003 Biandina Perumdos ITS P B 4-May-94 085700885577
Meidyani Blok BM No.4
PS00003 3101092000060004 Aranda Rizki Perumdos ITS L O 24-Feb-94 081654321000
Soedjono Blok ARS-7
PS00004 3171092910770002 Ratih Ayu Wisma Nyempil P O 19-Sep-94 089845680800
Indaswari W-8
PS00005 3224071910850005 Hafidh Azmi Keputih Gang L AB 7-Sep-94 085987654321
Buntu UC-100
Task Class NRP Ganjil
Dokter
NID KodePoli Nama JK Alamat Telp TglMulaiPraktek
D001 P04 dr.Iskandar L Jl.Meranti 37 Sidoarjo 081234500500 22-Mar-01
D002 P04 dr.Murni Sasmita P Jl.Rambutan 22 Surabaya 081233567567 10-Jan-07
D003 P01 dr.Angga Kusuma L Jl.Diponegoro 23 Sidoarjo 081246555789 21-Apr-08
D004 P02 dr.Charles Sungkono L Jl.Majapahit 89 Surabaya 08564682277 14-Jun-10
D005 P03 dr.Melani Andini P Jl.Jalak Gg III/15 Pasuruan 089845477000 2-Mar-11
D006 P04 dr.Sri Wedari P Jl.Kenjeran 200 Surabaya 081331454000 22-Jun-10

Pemeriksaan
IDPeriksa NID IDPasien TglPeriksa Diagnosa Biaya Catatan
PR01 D003 PS00001 05-Nov-14 Demam Berdarah Dengue 150000 PembayaranKredit
PR02 D006 PS00002 08-Nov-14 Sakit Gigi 85000 PembayaranTunai
PR03 D003 PS00003 10-Nov-14 Maag Kronis 250000 PembayaranKredit
PR04 D004 PS00004 10-Nov-14 Infeksi Mata 50000 PembayaranTunai
PR05 D005 PS00005 29-Nov-14 Influenza 30000 PembayaranTunai
PR06 D005 PS00002 30-Dec-14 Influenza 30000 PembayaranTunai
PR07 D001 PS00001 30-Dec-14 Radang Gusi 35000 PembayaranTunai
Task Class NRP Ganjil
2) Find name of patients who got “Influenza” or “Sakit Gigi”
3) Find name of patients who got “Influenza” and was examined on December
4) Find name of patients who was examined by dr.Angga Kusuma, but never got “Radang Gusi”
5) Find the income of each doctor based on the check fees.
6) Show the average of check fees by Credit payment and the average of check fees by cash
7) Show minimum and maximum check fees in November and in December
8) Review using ANY and give an example based on the PDM of Medical Center
9) Review using ALL and give an example based on the PDM of Medical Center
10) Can INTERSECTION generate inappropriate queries? If it can, give the reason.

Use two answer (common query and query with UNION/INTERSECTION/EXCEPTS) for number 2 –
4
Upload the .sql and the documentation of the answer (.docx / .doc) in email asisten with subject
Tugas2_Name_NRP
Task Class NRP Genap
1) Insert the data
DataPasien
IDPasien NoKTP Nama Pasien Alamat Pasien JK Gol. Tgl Lahir Telp
Darah
PS00006 3507254111940001 Lidra Trifidya Jl. Teknik P O 01-Nov-94 081320984595
Komputer II No.
41
PS00007 3606125204940003 Yutika Amelia Jl. Keputih Utara P B 12-Apr-94 085763635441
Effendi No.4
PS00008 3125662512940006 Ahmad Charis Jl. Arief Rachman L A 25-Dec-94 085784296187
Fauzan Hakim No.9
PS00009 3502125905940001 Afina Lina Jl. Keputih II No. P B 19-May-94 085728272635
14
PS00010 3226114606940006 Dewi Jl. Keputih II C No. P AB 06-Jun-94 085646683171
Rachmawati 15
Task Class NRP Genap
Dokter
NID KodePoli Nama JK Alamat Telp TglMulaiPraktek
D007 P04 Drg. Fredy Manto L Jl. Jurang Kuping 09 Surabaya 081234500500 22-Mar-01
D008 P04 Drg. Widyasari Lamori P Jl. Manukan Tengah 10 Surabaya 081233567567 10-Jan-04
D009 P01 Dr. Budikusnaedi L Jl. Raya Diponegoro 121 Surabaya 081246555789 21-Apr-01
D0010 P02 Dr. Hariyanto Kusuma L Jl. Ngagel Jaya 05 Surabaya 085646822777 14-Jun-04
D0011 P02 Dr. Sri Herianti P Jl. Wisma Permai 01 Surabaya 089845477000 2-Mar-02
D0012 P04 Drg. Elvin Purwantari P Jl. Kenjeran 160 Surabaya 081331454000 22-Jun-02

Pemeriksaan
IDPeriksa NID IDPasien TglPeriksa Diagnosa Biaya Catatan
C001 D009 PS00006 01-Jan-15 Usus Buntu 8000000 PembayaranKredit
C002 D012 PS00007 02-Jan-15 Gigi Berlubang 300000 PembayaranTunai
C003 D009 PS00008 12-Jan-15 Batu Ginjal 6000000 PembayaranKredit
C004 D010 PS00009 20-Jan-15 Periodontitis 500000 PembayaranTunai
C005 D011 PS00010 21-Jan-15 Sakit Telinga 100000 PembayaranTunai
C006 D011 PS00007 02-Feb-15 Influenza 50000 PembayaranTunai
C007 D012 PS00006 04-Feb-15 Gigi Berlubang 350000 PembayaranTunai
Task Class NRP Genap
2) Find name of patients who got “Gigi Berlubang” or “Usus Buntu”
3) Find name of patients who got “Gigi Berlubang” and was examined on January
4) Find name of patients who was examined by Drg. Elvin Purwantari, but never got “Usus
Buntu”
5) Find the income of each doctor based on the check fees.
6) Show the average of check fees in January and the average of check fees in February
7) Show minimum and maximum check fees by Credit Payment (Pembayaran Kredit) and by
Cash (Pembayaran Tunai)
8) Review using ANY and give an example based on the PDM of Medical Center
9) Review using ALL and give an example based on the PDM of Medical Center
10) Can INTERSECTION generate inappropriate queries? If it can, give the reason.

Use two answer (common query and query with UNION/INTERSECTION/EXCEPTS) for number 2 –
4
Upload the .sql and the documentation of the answer (.docx / .doc) in email asisten with subject
Tugas2_Name_NRP

Das könnte Ihnen auch gefallen