mysql db setup

sg0212148@ubuntu:~$ mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.1.41-3ubuntu12.10 (Ubuntu)

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed
mysql> create table report (student text, subject text, score int);
Query OK, 0 rows affected (0.00 sec)

insert into report (student, subject, score) values (“st1″,”sb1”,3);
insert into report (student, subject, score) values (“st2″,”sb1”,4);
insert into report (student, subject, score) values (“st3″,”sb1”,5);
insert into report (student, subject, score) values (“st4″,”sb2”,4);
insert into report (student, subject, score) values (“st5″,”sb2”,5);
insert into report (student, subject, score) values (“st6″,”sb2”,6);
insert into report (student, subject, score) values (“st7″,”sb3”,5);
insert into report (student, subject, score) values (“st8″,”sb3”,6);
insert into report (student, subject, score) values (“st9″,”sb3”,7);
mysql> select * from report;
+———+———+——-+
| student | subject | score |
+———+———+——-+
| st1      | sb1      |     3 |
| st2      | sb1      |     4 |
| st3      | sb1      |     5 |
| st4      | sb2      |     4 |
| st5      | sb2      |     5 |
| st6      | sb2      |     6 |
| st7      | sb3      |     5 |
| st8      | sb3      |     6 |
| st9      | sb3      |     7 |
+———+———+——-+
9 rows in set (0.00 sec)

mysql>  select student,subject from report where score > 4;
+———+———+
| student | subject |
+———+———+
| st3      | sb1      |
| st5      | sb2      |
| st6      | sb2      |
| st7      | sb3      |
| st8      | sb3      |
| st9      | sb3      |
+———+———+
6 rows in set (0.00 sec)

mysql> select count(subject), subject from (select subject from report where score > 4) as SS group by subject;
+—————-+———+
| count(subject) | subject |
+—————-+———+
|              1 | sb1      |
|              2 | sb2      |
|              3 | sb3      |
+—————-+———+
3 rows in set (0.00 sec)

mysql> select count(subject), subject from (select subject from report where score > 4) as SS group by subject having count(subject)  > 1;
+—————-+———+
| count(subject) | subject |
+—————-+———+
|              2 | sb2      |
|              3 | sb3      |
+—————-+———+
2 rows in set (0.00 sec)

where‘ operates on individual rows to select row that later may be use for grouping (‘where’ cannot be used after grouping)

having‘ operate on agregate functions

you cannot have ‘having’ without group by for proper results:
mysql> select count(subject), subject from (select subject from report where score > 4) as SS having count(subject)  > 1;
+—————-+———+
| count(subject) | subject |
+—————-+———+
|              6 | sb1      |
+—————-+———+

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s