Advertisement
hadimaster65555

SQL - Introduction to Aggregation and Subquery Script by HadiMaster

Mar 29th, 2024
1,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PostgreSQL 3.04 KB | Source Code | 0 0
  1. -- Create the marketing dataset table
  2. CREATE TABLE marketing_data (
  3.     customer_id SERIAL PRIMARY KEY,
  4.     age INT,
  5.     gender VARCHAR(10),
  6.     income INT,
  7.     education_level VARCHAR(50),
  8.     purchase_amount INT
  9. );
  10.  
  11. -- Insert data into the marketing dataset table
  12. INSERT INTO marketing_data (age, gender, income, education_level, purchase_amount) VALUES
  13. (35, 'Male', 50000, 'Bachelor''s Degree', 100),
  14. (28, 'Female', 60000, 'Master''s Degree', 150),
  15. (42, 'Male', 75000, 'High School', 200),
  16. (38, 'Female', 55000, 'Bachelor''s Degree', 120),
  17. (45, 'Male', 80000, 'PhD', 180),
  18. (31, 'Female', 48000, 'High School', 90),
  19. (26, 'Male', 40000, 'High School', 80),
  20. (29, 'Female', 65000, 'Bachelor''s Degree', 130),
  21. (33, 'Male', 70000, 'Master''s Degree', 160),
  22. (36, 'Female', 58000, 'Bachelor''s Degree', 140),
  23. (40, 'Male', 72000, 'Bachelor''s Degree', 170),
  24. (27, 'Female', 52000, 'High School', 110),
  25. (34, 'Male', 68000, 'Master''s Degree', 150),
  26. (39, 'Female', 73000, 'Bachelor''s Degree', 200),
  27. (32, 'Male', 50000, 'High School', 100),
  28. (37, 'Female', 60000, 'Master''s Degree', 170),
  29. (43, 'Male', 78000, 'Bachelor''s Degree', 190),
  30. (30, 'Female', 54000, 'High School', 120),
  31. (44, 'Male', 82000, 'PhD', 210),
  32. (25, 'Female', 45000, 'Bachelor''s Degree', 80),
  33. (29, 'Male', 67000, 'Master''s Degree', 160),
  34. (35, 'Female', 57000, 'Bachelor''s Degree', 130),
  35. (41, 'Male', 74000, 'Bachelor''s Degree', 180),
  36. (28, 'Female', 51000, 'High School', 100),
  37. (33, 'Male', 69000, 'Master''s Degree', 150);
  38.  
  39. -- contoh agregasi data menggunakan group by
  40.  
  41. SELECT education_level, COUNT(*) AS num_customers
  42. FROM marketing_data
  43. GROUP BY education_level;
  44.  
  45. SELECT SUM(purchase_amount) as total_purchase
  46. FROM marketing_data;
  47.  
  48. SELECT gender, AVG(purchase_amount) AS avg_purchase_amount
  49. FROM marketing_data
  50. GROUP BY gender;
  51.  
  52. SELECT gender, MAX(income) AS max_income, MIN(income) as min_income
  53. FROM marketing_data
  54. GROUP BY gender;
  55.  
  56. -- penggunaan subquery
  57.  
  58. -- hitung jumlah total purchase untuk customer yang memiliki income lebih dari 60000
  59. SELECT total_purchase_amount
  60. FROM (
  61.     SELECT SUM(purchase_amount) AS total_purchase_amount
  62.     FROM marketing_data
  63.     WHERE income > 60000
  64. ) AS subquery;
  65.  
  66. -- mencari pelanggan yang memiliki purchase amount di atas rata-rata
  67. SELECT *
  68. FROM marketing_data
  69. WHERE purchase_amount > (
  70.     SELECT AVG(purchase_amount)
  71.     FROM marketing_data
  72. );
  73.  
  74. -- mencari customer yang memiliki income tertinggi
  75. SELECT *
  76. FROM marketing_data
  77. WHERE income = (
  78.     SELECT MAX(income)
  79.     FROM marketing_data
  80. );
  81.  
  82. -- with syntax
  83.  
  84. -- hitung jumlah total purchase untuk customer yang memiliki income lebih dari 60000
  85. WITH subquery AS (
  86.     SELECT SUM(purchase_amount) AS total_purchase_amount
  87.     FROM marketing_data
  88.     WHERE income > 60000
  89. )
  90. SELECT total_purchase_amount
  91. FROM subquery;
  92.  
  93. -- mencari pelanggan yang memiliki purchase amount di atas rata-rata
  94. WITH avg_income AS (
  95.     SELECT AVG(income) AS average_income
  96.     FROM marketing_data
  97. )
  98. SELECT *
  99. FROM marketing_data
  100. WHERE income > (SELECT average_income FROM avg_income);
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement