Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Query Anisa YP
----------------------------------------------------------------------------------------------- --- Gue buat table Dummy biar lu lebih ngerti gimana hasil dari soal-soal yang dari sale stock. --- Buat Running Query-nya, pencet "RUN" yang ada di bawah. ----------------------------------------------------------------------------------------------- /* Table 1 - member Expected Output: member_id email phone_number name address_city 1 kuncoro.barot@btpn.com 081218543812 Kuncoro Wicaksono Jabodetabek 2 anisayunijenius@gmail.com 081290206641 Anisa Yuni Primasari Bandung */ create table [member] (member_id int, email varchar(55), phone_number varchar(55), name varchar(55), address_city varchar(55)); insert into [member] values (1, 'kuncoro.barot@btpn.com', '081218543812', 'Kuncoro Wicaksono', 'Jabodetabek'), (2, 'anisayunijenius@gmail.com', '081290206641', 'Anisa Yuni Primasari', 'Bandung'); /* Table 2 - order Expected Output: order_id member_id coupon_code total grand_total product_id created 1 1 Coupon Code #1 3 3 1 2017-01-24 2 1 Coupon Code #1 2 2 1 2017-11-24 3 1 Coupon Code #1 1 1 1 2017-11-24 4 1 Coupon Code #1 1 1 1 2017-03-24 5 2 Coupon Code #1 4 4 1 2017-11-24 6 2 Coupon Code #1 3 3 1 2017-11-24 7 2 Coupon Code #1 4 4 1 2017-11-24 8 2 Coupon Code #1 2 2 1 2017-11-24 9 2 Coupon Code #1 4 4 1 2017-11-24 */ create table [order] (order_id int, member_id int, coupon_code varchar(55), total int, grand_total int, product_id int, created date); insert into [order] values (1, 1, 'Coupon Code #1', 3, 3, 1, '2017-01-24'), (2, 1, 'Coupon Code #1', 2, 2, 1, '2017-11-24'), (3, 1, 'Coupon Code #1', 1, 1, 1, '2017-11-24'), (4, 1, 'Coupon Code #1', 1, 1, 1, '2017-03-24'), (5, 2, 'Coupon Code #1', 4, 4, 1, '2017-11-24'), (6, 2, 'Coupon Code #1', 3, 3, 1, '2017-11-24'), (7, 2, 'Coupon Code #1', 4, 4, 1, '2017-11-24'), (8, 2, 'Coupon Code #1', 2, 2, 1, '2017-11-24'), (9, 2, 'Coupon Code #1', 4, 4, 1, '2017-11-24'); /* Table 3 - shipping Expected Output: shipping_id order_id address_city 1 1 Jabodetabek 2 2 Bandung 3 3 Jabodetabek 4 4 Sulawesi */ create table [shipping] (shipping_id int, order_id int, address_city varchar(55)); insert into [shipping] values (1, 1, 'Jabodetabek'), (2, 2, 'Bandung'), (3, 3, 'Jabodetabek'), (4, 4, 'Sulawesi'); ------------------------------------------------------------------ -- Nah, Sekarang Masuk ke Soal yang dikasih Sale Stock ------------------------------------------------------------------ -- SOAL 1 - Query data yang dikirimkan ke daerah Jabodetabek SELECT * FROM [shipping] WHERE ADDRESS_CITY = 'JABODETABEK' ---------------------------------------------------------------------------------------- -- Ini maksudnya ngambil dari table shipping, yang kolom address_city-nya Jabodetabek. ---------------------------------------------------------------------------------------- -- SOAL 2 -- Query data member dan jumlah order yang pernah dilakukan SELECT A.*, B.jumlah_order FROM [member] A LEFT JOIN (SELECT member_id, COUNT(1) as jumlah_order FROM [order] GROUP BY member_id) B ON A.member_id = b.member_id --------------------------------------------------------------------------------------------------- -- A.* maksudnya ngambil semua kolom table member, karena doi di-define sebagai A ([MEMBER] A) -- B.Jumlah_oder itu didapat dari COUNT(1) as jumlah_order dari table order, -- artinya ngambil jumlah order yang didapat dari jumlah order per-member_id (GROUP BY member_id) -- Join itu buat gabungin antara table member sama table order -- Kalo "ON A.member_id = b.member_id" buat identifier dari dua table tersebut disamain by apa. --------------------------------------------------------------------------------------------------- -- SOAL 3 -- Query data jumlah order dikelompokkan berdasarkan city dari member SELECT address_city, sum(jumlah_order) as jumlah_order FROM ( SELECT A.*, B.jumlah_order FROM [member] A LEFT JOIN (SELECT member_id, COUNT(1) as jumlah_order FROM [order] GROUP BY member_id) B ON A.member_id = b.member_id ) A GROUP BY address_city --------------------------------------------------------------------------------------------------- -- Ini hampir sama kaya nomor 2, cuma gue tambahin lagi di : -- Awal -> SELECT address_city, sum(jumlah_order) as jumlah_order FROM ( -- Akhir -> ) A GROUP BY address_city -- Artinya, abis dapet data sama kaya nomor 2, gue group lagi berdasarkankan address_city (area) --------------------------------------------------------------------------------------------------- -- SOAL 4 -- Query data 5 kota pengiriman yang paling tinggi di setiap bulannya SELECT address_city, sum(jumlah_order) as jumlah_order FROM ( SELECT A.*, B.jumlah_order FROM [member] A LEFT JOIN (SELECT member_id, COUNT(1) as jumlah_order FROM [order] GROUP BY member_id) B ON A.member_id = b.member_id ) A GROUP BY address_city ORDER BY jumlah_order DESC --------------------------------------------------------------------------------------------------- -- Ini hampir sama kaya nomor 3, cuma gue tambahin lagi di : -- Akhir -> ORDER BY jumlah_order DESC -- Artinya, di sort/order/urutin berdasarkan jumlah_order paling tinggi (DESC = descending - dari gede ke kecil) --------------------------------------------------------------------------------------------------- -- SOAL 5 -- Query data member yang address dan shipping addressnya berbeda SELECT A.*, C.address_city as Shipping_address_city FROM [member] A LEFT JOIN [order] B ON A.member_id = B.member_id LEFT JOIN [shipping] C ON B.order_id = C.order_id WHERE A.address_city <> C.address_city --------------------------------------------------------------------------------------------------- -- Jadi ini buat cari yang address_city di table member sama di table shipping yang beda -- Caranya di Akhir -> WHERE A.address_city <> C.address_city -- <> artinya tidak sama dengan ---------------------------------------------------------------------------------------------------- -- SOAL 6 SELECT A.*, B.jumlah_belanja, B.total_order, (B.total_order/B.jumlah_belanja) as rata2_belanja FROM [member] A LEFT JOIN (SELECT member_id, COUNT(1) as jumlah_belanja, SUM(total) as total_order FROM [order] GROUP BY member_id) B ON A.member_id = b.member_id WHERE jumlah_belanja >= 2 --------------------------------------------------------------------------------------------------- -- Jadi ini buat cari yang address_city di table member sama di table shipping yang beda -- Caranya di Akhir -> WHERE A.address_city <> C.address_city -- <> artinya tidak sama dengan ---------------------------------------------------------------------------------------------------- -- SOAL 7 -- Query orang yang sudah belanja di bulan Januari 2017 tidak belanja di February dan belanja lagi di Maret SELECT * FROM [member] WHERE member_id IN ( SELECT member_id FROM [order] WHERE MONTH(CREATED) IN ( 1 , 3 ) AND member_id NOT IN ( SELECT member_id FROM [order] WHERE MONTH(CREATED) = 2 ) GROUP BY member_id ) --------------------------------------------------------------------------------------------------- -- SELECT * FROM [member] WHERE member_id IN -- Artinya cari semua ditable member yang ... -- SELECT member_id FROM [order] WHERE MONTH(CREATED) IN ( 1 , 3 ) -- Ambil member_id dari table order yang bulannya di bulan 1 (January) dan bulan 3 (Maret) -- AND member_id NOT IN ( SELECT member_id FROM [order] WHERE MONTH(CREATED) = 2 ) -- Dan member_id dari table order tidak ada di bulan 2 (February) -- Jadi kalo kalimat panjangnya : -- Artinya cari semua ditable member yang diambil member_id dari table order yang bulannya di bulan 1 (January) dan bulan 3 (Maret) tapi member_id dari table order tidak ada di bulan 2 (February) ----------------------------------------------------------------------------------------------------
run
|
edit
|
history
|
help
0
practice sql_12AUG_Upddated
bc160401693
FIGURA5.2
Greatest_N_Per_Group
SQL for beginners( defined with errors while enforcing constraints)
Find gaps in timesheet data between certain hours
Libros
CTE reqursive query
Employee Department Interview Questions
tesing