Skip to content

Instantly share code, notes, and snippets.

@hidayat365
Created October 2, 2013 02:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hidayat365/6788330 to your computer and use it in GitHub Desktop.
Save hidayat365/6788330 to your computer and use it in GitHub Desktop.
Menghitung nilai Minimum, Maksimum, Rata-rata, dan Total menggunakan SQL query
D:\xampp\mysql\bin>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.16 MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test
Database changed
mysql> --------------------------------------------------
mysql> -- bikin table dengan contoh datanya
mysql> --------------------------------------------------
mysql> create table contoh_data as
-> select 1 id, 20 quantity union all
-> select 2 id, 30 quantity union all
-> select 4 id, 40 quantity union all
-> select 5 id, 50 quantity union all
-> select 3 id, 60 quantity union all
-> select 6 id, 35 quantity union all
-> select 7 id, 45 quantity union all
-> select 8 id, 15 quantity union all
-> select 9 id, 75 quantity union all
-> select 10 id, 70 quantity ;
Query OK, 10 rows affected (0.16 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> --------------------------------------------------
mysql> -- coba lihat isi contoh datanya
mysql> --------------------------------------------------
mysql> select * from contoh_data ;
+----+----------+
| id | quantity |
+----+----------+
| 1 | 20 |
| 2 | 30 |
| 4 | 40 |
| 5 | 50 |
| 3 | 60 |
| 6 | 35 |
| 7 | 45 |
| 8 | 15 |
| 9 | 75 |
| 10 | 70 |
+----+----------+
10 rows in set (0.00 sec)
mysql> --------------------------------------------------
mysql> -- hitung min, max, sum, dan average
mysql> --------------------------------------------------
mysql> select sum(quantity) total
-> , count(*) jumlah_baris
-> , avg(quantity) rata2
-> , min(quantity) minimum
-> , max(quantity) maksimum
-> from contoh_data ;
+-------+--------------+---------+---------+----------+
| total | jumlah_baris | rata2 | minimum | maksimum |
+-------+--------------+---------+---------+----------+
| 440 | 10 | 44.0000 | 15 | 75 |
+-------+--------------+---------+---------+----------+
1 row in set (0.00 sec)
mysql> --------------------------------------------------
mysql> -- YAY!!!!! success
mysql> --------------------------------------------------
mysql>
@yehezkielermanto
Copy link

Great!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment