0% found this document useful (0 votes)
151 views5 pages

Ulang Subquery18 04 13

This document contains 14 SQL queries with various SELECT statements to retrieve data from database tables like employees, vendors, products, customers, orders, etc. The queries perform operations like filtering on conditions, aggregations, joins, correlated subqueries and string/date comparisons to select, group and retrieve relevant information from the tables.

Uploaded by

Icha Frisca
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views5 pages

Ulang Subquery18 04 13

This document contains 14 SQL queries with various SELECT statements to retrieve data from database tables like employees, vendors, products, customers, orders, etc. The queries perform operations like filtering on conditions, aggregations, joins, correlated subqueries and string/date comparisons to select, group and retrieve relevant information from the tables.

Uploaded by

Icha Frisca
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Using SubQuery

-----------------------Select last_name
From employees
Where salary > 11000;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
Select last_name
From employees
Where job_id =
(Select job_id
From employees
Where employee_id = 141);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
Kalau ada having selalu ada group by
Select department_id,MIN(salary)
From employees
Group by department_id
Having MIN(salary) >
(Select MIN(salary)
From employees
Where department_id = 50)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
1. carilah nama vendor yang harga barangnya termurah
Select v.vend_name,p.prod_price
From vendors v,products p
Where v.vend_id = p.vend_id
And p.prod_price = (Select MIN(prod_price)from products);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
2. tampilkan kota-kota dari vendor produk jetpack harganya dibawah 55
Select vend_city,prod_name,prod_price
From vendors v,products p
Where v.vend_id=p.vend_id
And prod_name like '%JetPack%'
And prod_price < (Select prod_price From products Where prod_price=55);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
3. siapa sajakah customer yang melakukan order tanggal 01-09-05
select c.cust_name, o.order_date from customers c join orders o on(o.cust_id=c.c

ust_id) where o.order_date like '%01-09-2005%';


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
4. dimakah alamat lengkap customer (kota,negara,kodepos) yang melakukan pembelia
n barang sama dengann customer no 10001
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
5. siapakah nama customer dan email contact nya,yang paling boros belanja(diliha
t dari pembelian barang paling banyak)
==>select c.cust_name, c.cust_email, c.cust_contact, max(j.jumlah)
from customers c, (select o.cust_id,count(oi.order_item) jumlah from orderitems
oi join orders o on(oi.order_num=o.order_num) group by o.cust_id) j
where c.cust_id = j.cust_id group by c.cust_name, c.cust_email, c.cust_contact
;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
6. siapakah nama customer yang melakukan belanja barang diatas rata-rata jumlah
pembelian barang
select c.cust_name, j.jumlah from (select o.cust_id,count(oi.order_item) jumlah
from orderitems oi join orders o on(oi.order_num=o.order_num) group by o.cust_
id) j, customers c
where c.cust_id = j.cust_id
and j.jumlah > (select avg(count(oi.order_item)) from orderitems oi join orders
o on(oi.order_num=o.order_num) group by o.cust_id);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
7. siapa saja customer dan kapan,yang membeli barang sling c join orders o
Select c.cust_name,o.order_date
From customers c join orders o
ON (c.cust_id=o.cust_id)
Join orderitems i
ON (o.order_num=i.order_num)
Join products p
ON (i.prod_id=p.prod_id)
Where prod_name =(Select prod_name From products Where prod_name='Sling');
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
8. tampilkan nama vendor dan negara vendor di mana quantity barang yang distok s
ama dengan quantity dari order_num 20006
select a.vend_name ,a.vend_country from vendors a join products b
on a.vend_id=b.vend_id
join orderitems c
on b.prod_id=c.prod_id

where order_num=(select order_num from orderitems where order_num='20006');


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
9. barang apakah dan berapa harganya yang mempunyai catatan paling panjang
select p.prod_name, p.prod_price , length(pn.note_text) panjang_karakter
from products p join productnotes pn
on p.prod_id = pn.prod_id
where length(pn.note_text)=(select max(length(note_text)) from productnotes);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
10. tampilkan nama vendor dan kota asal vendor dari barang-barang yang pernah di
order oleh customer 10002
select v.vend_name, v.vend_city, p.prod_name, o.cust_id
from vendors v join products p
on(v.vend_id= p.vend_id)
join orderitems oi
on(oi.prod_id=p.prod_id)
join orders o
on(o.order_num=oi.order_num)
where o.cust_id='10002';
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
11. pada tanggal berapa saja customer dengan email [email protected] melakukan tr
ansaksi
select c.cust_email, o.order_date
from customers c join orders o
on(c.cust_id=o.cust_id)
where c.cust_email = '[email protected]';
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
12. tampilkan customer id dari barang yang jumlah pembeliannya lebih sedikit dar
i pembelian rata-rata dan tidak berasal dari state chicago
select c.cust_id, j.jumlah from (select o.cust_id,count(oi.order_item) jumlah
from orderitems oi join orders o on(oi.order_num=o.order_num) group by o.cust_i
d) j, customers c
where c.cust_id = j.cust_id
and j.jumlah < (select avg(count(oi.order_item)) from orderitems oi join orders
o on(oi.order_num=o.order_num) group by o.cust_id)
and c.cust_city <> 'Chicago';
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
13. sebutkan vendor-vendor yang mensupply barang dengan harga diatas harga baran
g rata-rata
select v.vend_name, h.harga

from vendors v join (select vend_id, sum(prod_price) harga from products group b
y vend_id) h
on(h.vend_id=v.vend_id)
where h.harga > (select avg(prod_price) from products);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
14. sebutkan vendor-vendor yang punya catatan produk paling kecil dari rata-rata
panjang komentar untuk produk dan harga barangnya diatas rata-rata
select distinct(v.vend_name)
from vendors v join products p
on(p.vend_id=v.vend_id)
join productnotes pn
on(pn.prod_id=p.prod_id)
where length(pn.note_text) < (select avg(length(note_text)) from productnotes)
and p.prod_price > (select avg(prod_price) from products);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
15. negara-negara mana saja yang vendornya pernah mensupply produk dengan qty an
tara 3-10
select distinct(v.vend_country)
from vendors v join products p
on(p.vend_id=v.vend_id)
join orderitems oi
on(oi.prod_id=p.prod_id)
where oi.quantity between 3 and 10;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
16. sebutkan produk-produk yang satu produsen dengan jeetpack 100 dan harganya d
ibawah rata-rata
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
17. sebutkan vendor-vendor yang pernah mensuply product dengan description oil c
an,red dan harganya diatas rata-rata
select v.vend_name, p.prod_desc, p.prod_price
from vendors v join products p
on(p.vend_id=v.vend_id)
where p.prod_desc like '%Oil can, red%'
and p.prod_price > (select avg(prod_price) from products);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
18. sebutkan vendor-vendor yang satu negara dengan customer wascals
SELECT vend_name, vend_country
FROM vendors
WHERE vend_country = (SELECT cust_country
FROM customers

WHERE cust_name = 'Wascals');


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
19. sebutkan customer-customer yang satu negara dengan produsen donator
select cust_name from customers
where cust_country=(select vend_country from vendors);
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
20. tampilkan nama vendor,dan total nilai barang yang pernah disupply dimana tot
al nilai barangnya lebih besar dari harga barang yang paling mahal
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::
21. produk apa saja yang note date nya dilakukan setelah tanggal order produk da
ri customer yosemite
select p.prod_name, pn.note_date
from products p join productnotes pn
on(pn.prod_id=p.prod_id)
where pn.note_date > (select o.order_date from orders o join customers c on(c.cu
st_id=o.cust_id) where c.cust_name='Yosemite Place');
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::

You might also like