You are on page 1of 15

MySQL Practice MIS-410, EWU

Microsoft Windows [Version 6.1.7601]


Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Users\DELL>cd c:\xampp\mysql\bin

c:\xampp\mysql\bin>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.27 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> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| cdcol |
| mamun |
| mysql |
| performance_schema |
| phpmyadmin |
| test |
| test_db |
| webauth |
+--------------------+
9 rows in set (0.00 sec)

mysql> drop database mamun;


Query OK, 3 rows affected (0.08 sec)

Page | 1
MySQL Practice MIS-410, EWU

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| cdcol |
| mysql |
| performance_schema |
| phpmyadmin |
| test |
| test_db |
| webauth |
+--------------------+
8 rows in set (0.01 sec)

mysql> CREATE DATABASE Practice;


Query OK, 1 row affected (0.01 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| cdcol |
| mysql |
| performance_schema |
| phpmyadmin |
| practice |
| test |
| test_db |
| webauth |
+--------------------+
9 rows in set (0.00 sec)

Page | 2
MySQL Practice MIS-410, EWU

mysql> use practice;


Database changed
mysql> CREATE TABLE Persons
-> (
-> P_Id int,
-> LastName varchar(255),
-> FirstName varchar(255),
-> Address varchar(255),
-> City varchar(255)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> describe persons;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| P_Id | int(11) | YES | | NULL | |
| LastName | varchar(255) | YES | | NULL | |
| FirstName | varchar(255) | YES | | NULL | |
| Address | varchar(255) | YES | | NULL | |
| City | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.09 sec)

mysql> INSERT INTO persons (P_Id, LastName, FirstName, Address,


City) VALUES (988,'Gayle','Chris','33, ABC lane','Bogura');
Query OK, 1 row affected (0.03 sec)

mysql> select * from persons;


+------+----------+-----------+--------------+--------+
| P_Id | LastName | FirstName | Address | City |
+------+----------+-----------+--------------+--------+
| 988 | Gayle | Chris | 33, ABC lane | Bogura |
+------+----------+-----------+--------------+--------+
1 row in set (0.04 sec)

Page | 3
MySQL Practice MIS-410, EWU

mysql> INSERT INTO persons


-> VALUES (678,'Gul','Umar','39, XYZ lane','Dhaka'),
-> (908,'Afridi','Shahid','83, HHH Road','Barisal'),
-> (148,'Sharma','Ishant','45, BCD Road','Dhaka'),
-> (448,'Hasan','Shakib-Al','66, CTC Road','Shylhet'),
-> (783,'Lara','Brian','58, HPA lane','Chittagong');
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> select * from persons;


+------+----------+-----------+--------------+------------+
| P_Id | LastName | FirstName | Address | City |
+------+----------+-----------+--------------+------------+
| 988 | Gayle | Chris | 33, ABC lane | Bogura |
| 678 | Gul | Umar | 39, XYZ lane | Dhaka |
| 908 | Afridi | Shahid | 83, HHH Road | Barisal |
| 148 | Sharma | Ishant | 45, BCD Road | Dhaka |
| 448 | Hasan | Shakib-Al | 66, CTC Road | Shylhet |
| 783 | Lara | Brian | 58, HPA lane | Chittagong |
+------+----------+-----------+--------------+------------+
6 rows in set (0.01 sec)

mysql> SELECT firstname, lastname, city FROM persons;


+-----------+----------+------------+
| firstname | lastname | city |
+-----------+----------+------------+
| Chris | Gayle | Bogura |
| Umar | Gul | Dhaka |
| Shahid | Afridi | Barisal |
| Ishant | Sharma | Dhaka |
| Shakib-Al | Hasan | Shylhet |
| Brian | Lara | Chittagong |
+-----------+----------+------------+
6 rows in set (0.00 sec)

mysql> alter table persons rename players;


Query OK, 0 rows affected (0.06 sec)

Page | 4
MySQL Practice MIS-410, EWU

mysql> SELECT firstname, lastname, city FROM players;


+-----------+----------+------------+
| firstname | lastname | city |
+-----------+----------+------------+
| Chris | Gayle | Bogura |
| Umar | Gul | Dhaka |
| Shahid | Afridi | Barisal |
| Ishant | Sharma | Dhaka |
| Shakib-Al | Hasan | Shylhet |
| Brian | Lara | Chittagong |
+-----------+----------+------------+
6 rows in set (0.00 sec)

mysql> alter table players change city location varchar(20);


Query OK, 6 rows affected (0.05 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> select * from players;


+------+----------+-----------+--------------+------------+
| P_Id | LastName | FirstName | Address | location |
+------+----------+-----------+--------------+------------+
| 988 | Gayle | Chris | 33, ABC lane | Bogura |
| 678 | Gul | Umar | 39, XYZ lane | Dhaka |
| 908 | Afridi | Shahid | 83, HHH Road | Barisal |
| 148 | Sharma | Ishant | 45, BCD Road | Dhaka |
| 448 | Hasan | Shakib-Al | 66, CTC Road | Shylhet |
| 783 | Lara | Brian | 58, HPA lane | Chittagong |
+------+----------+-----------+--------------+------------+
6 rows in set (0.00 sec)

mysql> CREATE TABLE scores ( Id int, match1 int, match2 int, match3
int, match4 int);
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO scores


-> VALUES (678,55,67,98,120),
-> (908,70,80,30,49),
-> (148,12,33,41,17),
-> (448,58,90,122,87),
-> (783,75,143,100,82);
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

Page | 5
MySQL Practice MIS-410, EWU

mysql> select * from scores;


+------+--------+--------+--------+--------+
| Id | match1 | match2 | match3 | match4 |
+------+--------+--------+--------+--------+
| 678 | 55 | 67 | 98 | 120 |
| 908 | 70 | 80 | 30 | 49 |
| 148 | 12 | 33 | 41 | 17 |
| 448 | 58 | 90 | 122 | 87 |
| 783 | 75 | 143 | 100 | 82 |
+------+--------+--------+--------+--------+
5 rows in set (0.00 sec)

mysql> SELECT p.firstname, p.lastname, s.match1, s.match4


-> FROM players as p, scores as s
-> WHERE p.P_id = s.Id;
+-----------+----------+--------+--------+
| firstname | lastname | match1 | match4 |
+-----------+----------+--------+--------+
| Umar | Gul | 55 | 120 |
| Shahid | Afridi | 70 | 49 |
| Ishant | Sharma | 12 | 17 |
| Shakib-Al | Hasan | 58 | 87 |
| Brian | Lara | 75 | 82 |
+-----------+----------+--------+--------+
5 rows in set (0.04 sec)

mysql> SELECT p.lastname, s.match1


-> FROM players as p
-> LEFT JOIN scores as s
-> ON p.P_Id=s.Id
-> ORDER BY s.match1;
+----------+--------+
| lastname | match1 |
+----------+--------+
| Gayle | NULL |
| Sharma | 12 |
| Gul | 55 |
| Hasan | 58 |
| Afridi | 70 |
| Lara | 75 |
+----------+--------+
6 rows in set (0.00 sec)

Page | 6
MySQL Practice MIS-410, EWU

mysql> SELECT * FROM scores


-> WHERE match3 BETWEEN 60 AND 90;
Empty set (0.00 sec)

mysql> SELECT * FROM scores


-> WHERE match3 BETWEEN 75 AND 110;
+------+--------+--------+--------+--------+
| Id | match1 | match2 | match3 | match4 |
+------+--------+--------+--------+--------+
| 678 | 55 | 67 | 98 | 120 |
| 783 | 75 | 143 | 100 | 82 |
+------+--------+--------+--------+--------+
2 rows in set (0.00 sec)

mysql> select (match1+match2+match3+match4)/4 as AverageRun from


scores;
+------------+
| AverageRun |
+------------+
| 85.0000 |
| 57.2500 |
| 25.7500 |
| 89.2500 |
| 100.0000 |
+------------+
5 rows in set (0.01 sec)

mysql> select p.lastname, (s.match1+s.match2+s.match3+s.match4)/4


as Average
-> from scores as s, players as p
-> where p.P_Id=s.Id;
+----------+----------+
| lastname | Average |
+----------+----------+
| Gul | 85.0000 |
| Afridi | 57.2500 |
| Sharma | 25.7500 |
| Hasan | 89.2500 |
| Lara | 100.0000 |
+----------+----------+
5 rows in set (0.00 sec)

Page | 7
MySQL Practice MIS-410, EWU

mysql> SELECT p.lastname,


ROUND((s.match1+s.match2+s.match3+s.match4)/4,1)
-> AS Average FROM Players as p, scores as s
-> where p.P_Id=448 and p.P_Id=s.Id;
+----------+---------+
| lastname | Average |
+----------+---------+
| Hasan | 89.3 |
+----------+---------+
1 row in set (0.00 sec)

mysql> INSERT INTO scores(Id, match4, match3, match2)


-> VALUES (988,25,88,160);
Query OK, 1 row affected (0.01 sec)

mysql> select * from scores;


+------+--------+--------+--------+--------+
| Id | match1 | match2 | match3 | match4 |
+------+--------+--------+--------+--------+
| 678 | 55 | 67 | 98 | 120 |
| 908 | 70 | 80 | 30 | 49 |
| 148 | 12 | 33 | 41 | 17 |
| 448 | 58 | 90 | 122 | 87 |
| 783 | 75 | 143 | 100 | 82 |
| 988 | NULL | 160 | 88 | 25 |
+------+--------+--------+--------+--------+
6 rows in set (0.00 sec)

mysql> UPDATE scores SET match1=49 where id=988;


Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from scores;


+------+--------+--------+--------+--------+
| Id | match1 | match2 | match3 | match4 |
+------+--------+--------+--------+--------+
| 678 | 55 | 67 | 98 | 120 |
| 908 | 70 | 80 | 30 | 49 |
| 148 | 12 | 33 | 41 | 17 |
| 448 | 58 | 90 | 122 | 87 |
| 783 | 75 | 143 | 100 | 82 |
| 988 | 49 | 160 | 88 | 25 |
+------+--------+--------+--------+--------+
6 rows in set (0.00 sec)

Page | 8
MySQL Practice MIS-410, EWU

mysql> SELECT players.lastname, scores.match3 FROM players, scores


-> WHERE players.P_Id=scores.Id ORDER BY scores.match3 DESC;
+----------+--------+
| lastname | match3 |
+----------+--------+
| Hasan | 122 |
| Lara | 100 |
| Gul | 98 |
| Gayle | 88 |
| Sharma | 41 |
| Afridi | 30 |
+----------+--------+
6 rows in set (0.00 sec)

mysql> select lastname from players where lastname like 'g%';


+----------+
| lastname |
+----------+
| Gayle |
| Gul |
+----------+
2 rows in set (0.05 sec)

mysql> select lastname from players where lastname like '%r%';


+----------+
| lastname |
+----------+
| Afridi |
| Sharma |
| Lara |
+----------+
3 rows in set (0.00 sec)

mysql> select lastname from players where lastname like '_a%';


+----------+
| lastname |
+----------+
| Gayle |
| Hasan |
| Lara |
+----------+
3 rows in set (0.00 sec)

Page | 9
MySQL Practice MIS-410, EWU

mysql> SELECT * FROM players WHERE location='Dhaka';


+------+----------+-----------+--------------+----------+
| P_Id | LastName | FirstName | Address | location |
+------+----------+-----------+--------------+----------+
| 678 | Gul | Umar | 39, XYZ lane | Dhaka |
| 148 | Sharma | Ishant | 45, BCD Road | Dhaka |
+------+----------+-----------+--------------+----------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM Players LIMIT 3;


+------+----------+-----------+--------------+----------+
| P_Id | LastName | FirstName | Address | location |
+------+----------+-----------+--------------+----------+
| 988 | Gayle | Chris | 33, ABC lane | Bogura |
| 678 | Gul | Umar | 39, XYZ lane | Dhaka |
| 908 | Afridi | Shahid | 83, HHH Road | Barisal |
+------+----------+-----------+--------------+----------+
3 rows in set (0.00 sec)

mysql> SELECT p.firstname, p.location,


-> (s.match1+s.match2+s.match3+s.match4) AS total_runs
-> FROM players as p
-> INNER JOIN scores as s
-> ON p.P_Id = s.Id
-> ORDER BY total_runs DESC
-> LIMIT 3;
+-----------+------------+------------+
| firstname | location | total_runs |
+-----------+------------+------------+
| Brian | Chittagong | 400 |
| Shakib-Al | Shylhet | 357 |
| Umar | Dhaka | 340 |
+-----------+------------+------------+
3 rows in set (0.08 sec)

mysql> SELECT p.firstname, p.location,


-> (s.match1+s.match2+s.match3+s.match4) AS total_runs
-> FROM players as p, scores as s
-> WHERE p.P_Id = s.Id
-> ORDER BY total_runs DESC
-> LIMIT 5;

Page | 10
MySQL Practice MIS-410, EWU

+-----------+------------+------------+
| firstname | location | total_runs |
+-----------+------------+------------+
| Brian | Chittagong | 400 |
| Shakib-Al | Shylhet | 357 |
| Umar | Dhaka | 340 |
| Chris | Bogura | 322 |
| Shahid | Barisal | 229 |
+-----------+------------+------------+
5 rows in set (0.00 sec)

mysql> SELECT COUNT(*) FROM players;


+----------+
| COUNT(*) |
+----------+
| 6 |
+----------+
1 row in set (0.01 sec)

mysql> SELECT COUNT(*) as No_of_Players FROM players;


+---------------+
| No_of_Players |
+---------------+
| 6 |
+---------------+
1 row in set (0.01 sec)

mysql> SELECT COUNT(DISTINCT location) as NO_of_Locations FROM


players;
+-----------------+
| NO_of_Locations |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.01 sec)

Page | 11
MySQL Practice MIS-410, EWU

mysql> SELECT location, COUNT( location) as NumberOfLocation


-> FROM players GROUP BY location;
+------------+------------------+
| location | NumberOfLocation |
+------------+------------------+
| Barisal | 1 |
| Bogura | 1 |
| Chittagong | 1 |
| Dhaka | 2 |
| Shylhet | 1 |
+------------+------------------+
5 rows in set (0.01 sec)

mysql> select now();


+---------------------+
| now() |
+---------------------+
| 2013-06-07 21:16:42 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT NOW(),CURDATE(),CURTIME();


+---------------------+------------+-----------+
| NOW() | CURDATE() | CURTIME() |
+---------------------+------------+-----------+
| 2013-06-07 21:17:18 | 2013-06-07 | 21:17:18 |
+---------------------+------------+-----------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF('2013-01-29','2008-11-30') AS DiffDate;


+----------+
| DiffDate |
+----------+
| 1521 |
+----------+
1 row in set (0.00 sec)

Page | 12
MySQL Practice MIS-410, EWU

mysql> CREATE TABLE Private


-> (Id int NOT NULL,
-> Education varchar(50) NOT NULL,
-> DateOfBirth date,
-> Weight_KG int,
-> Height_Meter decimal(3,2),
-> PRIMARY KEY (Id));
Query OK, 0 rows affected (0.01 sec)

mysql> describe private;


+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| Id | int(11) | NO | PRI | NULL | |
| Education | varchar(50) | NO | | NULL | |
| DateOfBirth | date | YES | | NULL | |
| Weight_KG | int(11) | YES | | NULL | |
| Height_Meter | decimal(3,2) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql> INSERT INTO private


-> VALUES (783,'MSc','1975-06-08',81,1.73);
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO private


-> VALUES (988,'BSc','1982-06-23',98,1.93),
-> (908,'BA','1985-06-23',77,1.80),
-> (678,'MBA','1983-06-23',82,1.90),
-> (148,'HSC','1987-06-23',73,1.99),
-> (448,'BBA','1988-06-23',68,1.73);
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0

Page | 13
MySQL Practice MIS-410, EWU

mysql> select * from private;


+-----+-----------+-------------+-----------+--------------+
| Id | Education | DateOfBirth | Weight_KG | Height_Meter |
+-----+-----------+-------------+-----------+--------------+
| 148 | HSC | 1987-06-23 | 73 | 1.99 |
| 448 | BBA | 1988-06-23 | 68 | 1.73 |
| 678 | MBA | 1983-06-23 | 82 | 1.90 |
| 783 | MSc | 1975-06-08 | 81 | 1.73 |
| 908 | BA | 1985-06-23 | 77 | 1.80 |
| 988 | BSc | 1982-06-23 | 98 | 1.93 |
+-----+-----------+-------------+-----------+--------------+
6 rows in set (0.01 sec)

mysql> SELECT p.lastname, p.address, r.education, s.match4 as


Runs_Scored
-> FROM players as p, scores as s, private as r
-> WHERE p.p_id=s.id AND s.id=r.id;
+----------+--------------+-----------+-------------+
| lastname | address | education | Runs_Scored |
+----------+--------------+-----------+-------------+
| Gul | 39, XYZ lane | MBA | 120 |
| Afridi | 83, HHH Road | BA | 49 |
| Sharma | 45, BCD Road | HSC | 17 |
| Hasan | 66, CTC Road | BBA | 87 |
| Lara | 58, HPA lane | MSc | 82 |
| Gayle | 33, ABC lane | BSc | 25 |
+----------+--------------+-----------+-------------+
6 rows in set (0.00 sec)

mysql> SELECT concat( p.firstname, ' ', p.lastname ) AS FullName,


r.education
-> FROM players as p, scores as s, private as r
-> WHERE p.p_id=s.id AND s.id=r.id;
+-----------------+-----------+
| FullName | education |
+-----------------+-----------+
| Umar Gul | MBA |
| Shahid Afridi | BA |
| Ishant Sharma | HSC |
| Shakib-Al Hasan | BBA |
| Brian Lara | MSc |
| Chris Gayle | BSc |
+-----------------+-----------+
6 rows in set (0.00 sec)

Page | 14
MySQL Practice MIS-410, EWU

mysql> SELECT concat( p.firstname, ' ', p.lastname ) AS FullName,


-> DATEDIFF(now(), r.dateofbirth) AS AGE_in_Days
-> FROM players as p, private as r
-> WHERE p.p_id=r.id;
+-----------------+-------------+
| FullName | AGE_in_Days |
+-----------------+-------------+
| Chris Gayle | 11307 |
| Umar Gul | 10942 |
| Shahid Afridi | 10211 |
| Ishant Sharma | 9481 |
| Shakib-Al Hasan | 9115 |
| Brian Lara | 13879 |
+-----------------+-------------+
6 rows in set (0.00 sec)

mysql> SELECT concat( p.firstname, ' ', p.lastname ) AS FullName,


-> ROUND((DATEDIFF(now(), r.dateofbirth)/365.25),2) AS
AGE_in_Years
-> FROM players as p, private as r
-> WHERE p.p_id=r.id;
+-----------------+--------------+
| FullName | AGE_in_Years |
+-----------------+--------------+
| Chris Gayle | 30.96 |
| Umar Gul | 29.96 |
| Shahid Afridi | 27.96 |
| Ishant Sharma | 25.96 |
| Shakib-Al Hasan | 24.96 |
| Brian Lara | 38.00 |
+-----------------+--------------+
6 rows in set (0.01 sec)

Page | 15

You might also like