Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /* SQL Tuning by
  2. by alul-cholil.blogspot.com
  3. */
  4.  
  5. --Penggunaan nama kolom lebih cepat dari '*'
  6. SELECT id, nama_depan, nama_belakang, umur
  7. FROM mahasiswa;
  8.     --daripada
  9. SELECT*FROM mahasiswa;
  10.  
  11.  
  12. --Hati-hati dalam menggunakan WHERE
  13. SELECT id, first_name, age FROM student_details WHERE age > 10;
  14.     --daripada
  15. SELECT id, first_name, age FROM student_details WHERE age != 10;
  16. ------------------------------------------
  17. SELECT id, first_name, age
  18. FROM student_details
  19. WHERE first_name LIKE 'Chan%';
  20.     --daripada
  21. SELECT id, first_name, age
  22. FROM student_details
  23. WHERE SUBSTR(first_name,1,3) = 'Cha';
  24. ------------------------------------------
  25. SELECT id, first_name, age
  26. FROM student_details
  27. WHERE first_name LIKE NVL ( :name, '%');
  28.     --daripada
  29. SELECT id, first_name, age
  30. FROM student_details
  31. WHERE first_name = NVL ( :name, first_name);
  32. ------------------------------------------
  33. SELECT product_id, product_name
  34. FROM product
  35. WHERE unit_price BETWEEN MAX(unit_price) AND MIN(unit_price)
  36.     --daripada
  37. SELECT product_id, product_name
  38. FROM product
  39. WHERE unit_price >= MAX(unit_price)
  40. AND unit_price <= MIN(unit_price)
  41. ------------------------------------------
  42.  
  43. --Having digunakan untuk mensortir baris setelah semua baris di select
  44. --lebih baik menggunakan query
  45. SELECT mapel, COUNT(mapel)
  46. FROM detil_siswa
  47. WHERE mapel != 'Kimia'
  48. AND mapel != 'Matematika'
  49. GROUP BY mapel;
  50.     --daripada
  51. SELECT mapel, COUNT(mapel)
  52. FROM detil_siswa
  53. GROUP BY mapel
  54. HAVING mapel!= 'Kimia' AND mapel!= 'Matematika';  
  55.  
  56.  
  57. --Gunakan EXISTS untuk seleksi kriteria yg bukan sub-query
  58. SELECT * FROM product p
  59. WHERE EXISTS (SELECT * FROM order_items o
  60. WHERE o.product_id = p.product_id)
  61.     --daripada
  62. SELECT * FROM product p
  63. WHERE product_id IN
  64. (SELECT product_id FROM order_items
  65.  
  66.  
  67. --Gunakan UNION ALL daripada UNION
  68. SELECT id, first_name
  69. FROM student_details_class10
  70. UNION ALL
  71. SELECT id, first_name
  72. FROM sports_team;
  73.     --daripada
  74. SELECT id, first_name, subject
  75. FROM student_details_class10
  76. UNION
  77. SELECT id, first_name
  78. FROM sports_team;