Advertisement
hadimaster65555

SQL - Introduction to DML Script by HadiMaster

Mar 28th, 2024 (edited)
1,545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PostgreSQL 3.62 KB | Source Code | 0 0
  1. -- Create the products table
  2. CREATE TABLE products (
  3.     product_id SERIAL PRIMARY KEY,
  4.     name VARCHAR(100),
  5.     description TEXT,
  6.     category VARCHAR(50),
  7.     price NUMERIC(10, 2),
  8.     inventory_quantity INT
  9. );
  10.  
  11. -- Insert data into the products table
  12. INSERT INTO products (product_id, name, description, category, price, inventory_quantity) VALUES
  13. (1, 'Laptop', '15.6" Intel Core i5, 8GB RAM, 512GB SSD', 'Electronics', 899.99, 50),
  14. (2, 'Smartphone', '6.5" OLED Display, 128GB Storage, 12MP Camera', 'Electronics', 699.99, 100),
  15. (3, 'Headphones', 'Wireless Over-Ear Headphones with ANC', 'Electronics', 199.99, 75),
  16. (4, 'Smartwatch', 'Fitness Tracker with Heart Rate Monitor', 'Electronics', 149.99, 30),
  17. (5, 'Backpack', 'Waterproof Laptop Backpack with USB Charging', 'Fashion', 49.99, 200),
  18. (6, 'Sneakers', 'Men''s Running Shoes with Breathable Mesh', 'Fashion', 79.99, 150),
  19. (7, 'Dress', 'Women''s Floral Print Summer Dress', 'Fashion', 39.99, 80),
  20. (8, 'Watch', 'Stainless Steel Analog Watch', 'Fashion', 129.99, 100),
  21. (9, 'Tablet', '10" Android Tablet with Quad-Core Processor', 'Electronics', 299.99, 50),
  22. (10, 'Camera', 'DSLR Camera Kit with 18-55mm Lens', 'Electronics', 799.99, 20),
  23. (11, 'Keyboard', 'Mechanical Gaming Keyboard with RGB Lighting', 'Electronics', 129.99, 50),
  24. (12, 'Mouse', 'Wireless Optical Mouse', 'Electronics', 29.99, 100),
  25. (13, 'Speaker', 'Bluetooth Portable Speaker', 'Electronics', 79.99, 150),
  26. (14, 'T-shirt', 'Men''s Cotton T-shirt', 'Fashion', 19.99, 200),
  27. (15, 'Jeans', 'Women''s Skinny Jeans', 'Fashion', 49.99, 100),
  28. (16, 'Sunglasses', 'Polarized UV Protection Sunglasses', 'Fashion', 59.99, 50),
  29. (17, 'Wallet', 'Genuine Leather Bifold Wallet', 'Fashion', 39.99, 120),
  30. (18, 'Printer', 'Wireless All-in-One Printer', 'Electronics', 149.99, 40),
  31. (19, 'Monitor', '27" LED Monitor with Full HD Resolution', 'Electronics', 249.99, 60),
  32. (20, 'External HDD', '2TB Portable External Hard Drive', 'Electronics', 89.99, 80),
  33. (21, 'Power Bank', '20000mAh Portable Power Bank', 'Electronics', 49.99, 100),
  34. (22, 'Fitness Tracker', 'Waterproof Activity Tracker', 'Electronics', 79.99, 150),
  35. (23, 'Yoga Mat', 'Non-Slip Exercise Mat', 'Fitness', 29.99, 200),
  36. (24, 'Dumbbells', 'Set of 10 lb Rubber-Coated Dumbbells', 'Fitness', 39.99, 100),
  37. (25, 'Protein Powder', 'Whey Protein Isolate Powder', 'Fitness', 49.99, 50);
  38.  
  39. -- ambil semua kolom dari tabel products
  40. SELECT * FROM products;
  41.  
  42. -- urutkan data berdasarkan price secara descending
  43. SELECT * FROM products
  44. ORDER BY price DESC;
  45.  
  46. -- ambil nilai unique dari data
  47. SELECT DISTINCT category FROM products
  48.  
  49. -- ambil 5 barang termahal
  50. SELECT * FROM products
  51. ORDER BY price DESC
  52. LIMIT 5;
  53.  
  54. -- ambil semua data yang harganya lebih kecil dari 100
  55. SELECT * FROM products
  56. WHERE price < 100;
  57.  
  58. -- memilih produk dengan kategori 'Elektronik' atau 'Fashion'
  59. SELECT *
  60. FROM products
  61. WHERE category IN ('Electronics', 'Fashion');
  62.  
  63. -- memilih produk dengan harga antara $50 dan $100
  64. SELECT *
  65. FROM products
  66. WHERE price BETWEEN 50 AND 100;
  67.  
  68. -- memilih produk dengan nama yang mengandung kata 'Speaker'
  69. SELECT *
  70. FROM products
  71. WHERE name LIKE '%Speaker%';
  72.  
  73. -- memilih produk dengan deskripsi yang tidak kosong
  74. SELECT *
  75. FROM products
  76. WHERE description IS NOT NULL;
  77.  
  78. -- masukkan nilai ke dalam suatu tabel
  79. INSERT INTO products (name, description, category, price, inventory_quantity)
  80. VALUES ('Wireless Earbuds', 'Bluetooth Earphones with Charging Case', 'Electronics', 59.99, 80);
  81.  
  82. -- update harga dari produk dengan product_id = 3
  83. UPDATE products
  84. SET price = 69.99
  85. WHERE product_id = 3;
  86.  
  87. -- hapus produk dengan product_id = 10
  88. DELETE FROM products
  89. WHERE product_id = 10;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement