Вы находитесь на странице: 1из 11

drop database if exists lab7db;

create database lab7db;

use lab7db;

drop table if exists employees;

create table employees(employeeid int primary key, firstname varchar(255), lastname


varchar(255), email varchar(255), dob date, phone char(11), country varchar(255),
children int);

insert into employees values(1, 'John', 'Osupile', 'John.Osupile@yahoo.com', '1988-


04-02', '017-6745481', 'Brunei',5);
insert into employees values(2, 'Steven', 'Frecky', 'goldfish@fishhere.net', '1974-
04-04', '012-6745441', 'Nigeria',2);
insert into employees values(3, 'Agang', 'Maroba', 'am@heroindomain.org', '1988-04-
02', '013-2079370', 'Brunei',3);
insert into employees values(4, 'Elsie', 'Frecky', 'elsie@superdiva.co.uk', '1980-
10-20', '019-3305184', 'Brunei',4);

dml - insert, update, delete, select

select to view records


-select
-distinct
-where
-and & or
-order by
-limit
-like & wildcard operator(%)
-in
-between
-alias
-aggregate function
-avg()
-count()
-max()
-min()
-sum()
-group by
-having

// all columns, all rows


mysql> select * from employees;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
4 rows in set (0.00 sec)

//one column, all rows


mysql> select firstname from employees;
+-----------+
| firstname |
+-----------+
| John |
| Steven |
| Agang |
| Elsie |
+-----------+
4 rows in set (0.00 sec)

//two or more columns, just add the comma, all rows


mysql> select firstname,lastname from employees;
+-----------+----------+
| firstname | lastname |
+-----------+----------+
| John | Osupile |
| Steven | Frecky |
| Agang | Maroba |
| Elsie | Frecky |
+-----------+----------+
4 rows in set (0.00 sec)

//different layout to view the records


mysql> select * from employees \G
*************************** 1. row ***************************
employeeid: 1
firstname: John
lastname: Osupile
email: John.Osupile@yahoo.com
dob: 1988-04-02
phone: 017-6745481
country: Brunei
children: 5
*************************** 2. row ***************************
employeeid: 2
firstname: Steven
lastname: Frecky
email: goldfish@fishhere.net
dob: 1974-04-04
phone: 012-6745441
country: Nigeria
children: 2
*************************** 3. row ***************************
employeeid: 3
firstname: Agang
lastname: Maroba
email: am@heroindomain.org
dob: 1988-04-02
phone: 013-2079370
country: Brunei
children: 3
*************************** 4. row ***************************
employeeid: 4
firstname: Elsie
lastname: Frecky
email: elsie@superdiva.co.uk
dob: 1980-10-20
phone: 019-3305184
country: Brunei
children: 4
4 rows in set (0.00 sec)

mysql>

//before distinct
mysql> select lastname from employees;
+----------+
| lastname |
+----------+
| Osupile |
| Frecky |
| Maroba |
| Frecky |
+----------+
4 rows in set (0.01 sec)

//after distinct, the redundant will not be displayed more than one time
mysql> select distinct lastname from employees;
+----------+
| lastname |
+----------+
| Osupile |
| Frecky |
| Maroba |
+----------+
3 rows in set (0.02 sec)

//where equal
mysql> select * from employees where country ='Brunei';
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.00 sec)

//where not equal


mysql> select * from employees where country != 'Brunei';
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| employeeid | firstname | lastname | email | dob | phone
| country | children |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-6
745441 | Nigeria | 2 |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
1 row in set (0.00 sec)

//where not equal, another symbol


mysql> select * from employees where country <> 'Brunei';
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| employeeid | firstname | lastname | email | dob | phone
| country | children |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-6
745441 | Nigeria | 2 |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
1 row in set (0.00 sec)

//where less than


mysql> select * from employees where children < 3;
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| employeeid | firstname | lastname | email | dob | phone
| country | children |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-6
745441 | Nigeria | 2 |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
1 row in set (0.01 sec)

//where less and equal


mysql> select * from employees where children <= 3;
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| employeeid | firstname | lastname | email | dob | phone
| country | children |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-6
745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-2
079370 | Brunei | 3 |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
2 rows in set (0.00 sec)

//where greater and equal


mysql> select * from employees where children >= 3;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.00 sec)

//where greater than


mysql> select * from employees where children > 3;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
2 rows in set (0.00 sec)

// and operator
mysql> select dob from employees where firstname ='elsie' and lastname ='frecky'
;
+------------+
| dob |
+------------+
| 1980-10-20 |
+------------+
1 row in set (0.00 sec)

//or operator
mysql> select dob from employees where firstname ='elsie' or lastname ='frecky';

+------------+
| dob |
+------------+
| 1974-04-04 |
| 1980-10-20 |
+------------+
2 rows in set (0.00 sec)

//and or operator
mysql> select dob from employees where (firstname ='elsie' or firstname = 'steve
n') and lastname ='frecky';
+------------+
| dob |
+------------+
| 1974-04-04 |
| 1980-10-20 |
+------------+
2 rows in set (0.00 sec)
//before sorting
mysql> select firstname from employees;
+-----------+
| firstname |
+-----------+
| John |
| Steven |
| Agang |
| Elsie |
+-----------+
4 rows in set (0.00 sec)

// after sorting, by default it is ascending


mysql> select firstname from employees order by firstname;
+-----------+
| firstname |
+-----------+
| Agang |
| Elsie |
| John |
| Steven |
+-----------+
4 rows in set (0.00 sec)

//after sorting ascending using keyword


mysql> select firstname from employees order by firstname asc;
+-----------+
| firstname |
+-----------+
| Agang |
| Elsie |
| John |
| Steven |
+-----------+
4 rows in set (0.00 sec)

//after sorting descending


mysql> select firstname from employees order by firstname desc;
+-----------+
| firstname |
+-----------+
| Steven |
| John |
| Elsie |
| Agang |
+-----------+
4 rows in set (0.01 sec)

//view the top 3 records


mysql> select * from employees limit 3;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.00 sec)

//same as above
mysql> select * from employees limit 0,3;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.00 sec)

// view index 2 and 3 records


mysql> select * from employees limit 2,3;
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| employeeid | firstname | lastname | email | dob | phone
| country | children |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-2
079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-3
305184 | Brunei | 4 |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
2 rows in set (0.00 sec)

// view index 3 and 3 records


mysql> select * from employees limit 3,3;
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| employeeid | firstname | lastname | email | dob | phone
| country | children |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-3
305184 | Brunei | 4 |
+------------+-----------+----------+-----------------------+------------+------
-------+---------+----------+
1 row in set (0.00 sec)

//if use wildcard operator, use like


//starts with a and ends with anything
mysql> select * from employees where firstname like 'A%';
+------------+-----------+----------+---------------------+------------+--------
-----+---------+----------+
| employeeid | firstname | lastname | email | dob | phone
| country | children |
+------------+-----------+----------+---------------------+------------+--------
-----+---------+----------+
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-207
9370 | Brunei | 3 |
+------------+-----------+----------+---------------------+------------+--------
-----+---------+----------+
1 row in set (0.02 sec)

//starts with anything and ends with n


mysql> select * from employees where firstname like '%n';
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
2 rows in set (0.02 sec)

//starts with anything and ends with anything and the middle will has n
mysql> select * from employees where firstname like '%n%';
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.01 sec)

// use or
mysql> select * from employees where country = 'brunei' or country = 'nigeria';
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
4 rows in set (0.00 sec)

// another way is use in. shorter code


mysql> select * from employees where country in ('brunei', 'nigeria');
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 2 | Steven | Frecky | goldfish@fishhere.net | 1974-04-04 | 012-
6745441 | Nigeria | 2 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
4 rows in set (0.00 sec)

//use and
mysql> select * from employees where children >=3 and children <=5;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.02 sec)

//another way use between, shorter code. it includes the 3 and 5 as well
mysql> select * from employees where children between 3 and 5;
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| employeeid | firstname | lastname | email | dob | phon
e | country | children |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
| 1 | John | Osupile | John.Osupile@yahoo.com | 1988-04-02 | 017-
6745481 | Brunei | 5 |
| 3 | Agang | Maroba | am@heroindomain.org | 1988-04-02 | 013-
2079370 | Brunei | 3 |
| 4 | Elsie | Frecky | elsie@superdiva.co.uk | 1980-10-20 | 019-
3305184 | Brunei | 4 |
+------------+-----------+----------+------------------------+------------+-----
--------+---------+----------+
3 rows in set (0.00 sec)

//before alias
mysql> select firstname, children from employees;
+-----------+----------+
| firstname | children |
+-----------+----------+
| John | 5 |
| Steven | 2 |
| Agang | 3 |
| Elsie | 4 |
+-----------+----------+
4 rows in set (0.00 sec)

//after alias. for good naming purposes only


mysql> select firstname, children as no_of_children from employees;
+-----------+----------------+
| firstname | no_of_children |
+-----------+----------------+
| John | 5 |
| Steven | 2 |
| Agang | 3 |
| Elsie | 4 |
+-----------+----------------+
4 rows in set (0.00 sec)

// average number of children for employees


mysql> select avg(children) from employees;
+---------------+
| avg(children) |
+---------------+
| 3.5000 |
+---------------+
1 row in set (0.03 sec)

//the highest number of children


mysql> select max(children) from employees;
+---------------+
| max(children) |
+---------------+
| 5 |
+---------------+
1 row in set (0.01 sec)

//the lowest number of children


mysql> select min(children) from employees;
+---------------+
| min(children) |
+---------------+
| 2 |
+---------------+
1 row in set (0.00 sec)

//the total number of children from brunei


mysql> select sum(children) from employees where country ='Brunei';
+---------------+
| sum(children) |
+---------------+
| 12 |
+---------------+
1 row in set (0.03 sec)

// to know how many country for the employees


mysql> select count(distinct country) from employees;
+-------------------------+
| count(distinct country) |
+-------------------------+
| 2 |
+-------------------------+
1 row in set (0.02 sec)

//number of employee for each country.


mysql> select country, count(employeeid) from employees group by country;
+---------+-------------------+
| country | count(employeeid) |
+---------+-------------------+
| Brunei | 3 |
| Nigeria | 1 |
+---------+-------------------+
2 rows in set (0.00 sec)

//country that has more than 1 employee


mysql> select country, count(employeeid) from employees group by country having
count(country) >1;
+---------+-------------------+
| country | count(employeeid) |
+---------+-------------------+
| Brunei | 3 |
+---------+-------------------+
1 row in set (0.00 sec)

Вам также может понравиться