You are on page 1of 16

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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 |
| hamu |
| mysql |
| performance_schema |
| phpmyadmin |
+--------------------+
5 rows in set (0.00 sec)

mysql> use hamu;


Database changed
mysql> show tables;
+----------------+
| Tables_in_hamu |
+----------------+
| artists |
| boats |
| catalog |
| committee |
| department |
| department_e |
| dept |
| emp |
| employee |
| parts |
| pizza |
| professor |
| reserves |
| sailors |
| soldby |
| store |
| suppliers |
| tracks |
| works |
+----------------+
19 rows in set (0.00 sec)

mysql> create database hamu2;


Query OK, 1 row affected (0.00 sec)

mysql> use databases hamu2;


ERROR 1049 (42000): Unknown database 'databases'
mysql> use hamu2;
Database changed
mysql> create table department(
-> dno int not null primary key,
-> dname varchar(50),
-> location varchar(50) default 'new delhi'
-> );
Query OK, 0 rows affected (0.58 sec)

mysql> create table employee(


-> eno char(3) not null primary key,
-> ename varchar(50) not null,
-> job_type varchar(50) not null,
-> manager char(3),
-> hire_date date not null,
-> dno int,
-> commission decimal(10,2),
-> salary decimal(7,2),
-> foreign key (dno) references department(dno)
-> );
Query OK, 0 rows affected (0.53 sec)

mysql>
mysql> insert into department(dno,dname,location)
-> values(10,'Accounting','New York'),
-> (20,'Research','Dallas'),
-> (30,'Sales','Chicago'),
-> (40,'Operation','Boston'),
-> (50,'Marketing','New Delhi');
Query OK, 5 rows affected (0.14 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> insert into


employee(eno,ename,job_type,manager,hire_date,dno,commission,salary)
-> values(765,'Martin','sales_man',null,'1981-04-22',30,1400.00,1250.00),
-> (756,'Jones','manager',783,'1981-04-02',20,0.00,2300.00),
-> (752,'Ward','sales_man',769,'1981-02-22',30,500.00,1300.00),
-> (749,'Allan','sales_man',769,'1981-02-20',30,300.00,2000.00),
-> (736,'Smith','clerk',790,'1980-12-17',20,0.00,1000.00),
-> (793,'Miller','clerk',788,'1982-01-23',40,0.00,1300.00),
-> (792,'Ford','analyst',756,'1981-12-03',20,0.00,2600.00),
-> (790,'James','Clerk',769,'1981-12-03',30,0.00,950.00),
-> (787,'Adams','Clerk',778,'1983-01-12',20,0.00,1150.00),
-> (784,'Turner','sales_man',769,'1981-09-08',30,0.00,1450.00),
-> (783,'King','President',null,'1981-11-17',10,0.00,2950.00),
-> (788,'Scott','Analyst',756,'1982-12-09',20,0.00,2850.00),
-> (778,'Clark','Manager',783,'1981-06-09',10,0.00,2900.00),
-> (769,'Blake','Manager',783,'1981-05-01',30,0.00,2870.00);
Query OK, 14 rows affected (0.60 sec)
Records: 14 Duplicates: 0 Warnings: 0

mysql> select * from employee;


+-----+--------+-----------+---------+------------+------+------------+---------+
| eno | ename | job_type | manager | hire_date | dno | commission | salary |
+-----+--------+-----------+---------+------------+------+------------+---------+
| 736 | Smith | clerk | 790 | 1980-12-17 | 20 | 0.00 | 1000.00 |
| 749 | Allan | sales_man | 769 | 1981-02-20 | 30 | 300.00 | 2000.00 |
| 752 | Ward | sales_man | 769 | 1981-02-22 | 30 | 500.00 | 1300.00 |
| 756 | Jones | manager | 783 | 1981-04-02 | 20 | 0.00 | 2300.00 |
| 765 | Martin | sales_man | NULL | 1981-04-22 | 30 | 1400.00 | 1250.00 |
| 769 | Blake | Manager | 783 | 1981-05-01 | 30 | 0.00 | 2870.00 |
| 778 | Clark | Manager | 783 | 1981-06-09 | 10 | 0.00 | 2900.00 |
| 783 | King | President | NULL | 1981-11-17 | 10 | 0.00 | 2950.00 |
| 784 | Turner | sales_man | 769 | 1981-09-08 | 30 | 0.00 | 1450.00 |
| 787 | Adams | Clerk | 778 | 1983-01-12 | 20 | 0.00 | 1150.00 |
| 788 | Scott | Analyst | 756 | 1982-12-09 | 20 | 0.00 | 2850.00 |
| 790 | James | Clerk | 769 | 1981-12-03 | 30 | 0.00 | 950.00 |
| 792 | Ford | analyst | 756 | 1981-12-03 | 20 | 0.00 | 2600.00 |
| 793 | Miller | clerk | 788 | 1982-01-23 | 40 | 0.00 | 1300.00 |
+-----+--------+-----------+---------+------------+------+------------+---------+
14 rows in set (0.00 sec)

mysql> select eno,ename,job_type,hire_date from employee;


+-----+--------+-----------+------------+
| eno | ename | job_type | hire_date |
+-----+--------+-----------+------------+
| 736 | Smith | clerk | 1980-12-17 |
| 749 | Allan | sales_man | 1981-02-20 |
| 752 | Ward | sales_man | 1981-02-22 |
| 756 | Jones | manager | 1981-04-02 |
| 765 | Martin | sales_man | 1981-04-22 |
| 769 | Blake | Manager | 1981-05-01 |
| 778 | Clark | Manager | 1981-06-09 |
| 783 | King | President | 1981-11-17 |
| 784 | Turner | sales_man | 1981-09-08 |
| 787 | Adams | Clerk | 1983-01-12 |
| 788 | Scott | Analyst | 1982-12-09 |
| 790 | James | Clerk | 1981-12-03 |
| 792 | Ford | analyst | 1981-12-03 |
| 793 | Miller | clerk | 1982-01-23 |
+-----+--------+-----------+------------+
14 rows in set (0.00 sec)

mysql>
mysql> select distinct(job_type) from employee;
+-----------+
| job_type |
+-----------+
| clerk |
| sales_man |
| manager |
| President |
| Analyst |
+-----------+
5 rows in set (0.07 sec)

mysql>
mysql> select concat(ename,',',job_type) as 'name and job' from employee;
+------------------+
| name and job |
+------------------+
| Smith,clerk |
| Allan,sales_man |
| Ward,sales_man |
| Jones,manager |
| Martin,sales_man |
| Blake,Manager |
| Clark,Manager |
| King,President |
| Turner,sales_man |
| Adams,Clerk |
| Scott,Analyst |
| James,Clerk |
| Ford,analyst |
| Miller,clerk |
+------------------+
14 rows in set (0.08 sec)

mysql> select
concat(eno,',',ename,',',job_type,',',manager,',',hire_date,',',dno,',',commission,
',',salary) as THE_OUTPUT from employee;
+------------------------------------------------------+
| THE_OUTPUT |
+------------------------------------------------------+
| 736,Smith,clerk,790,1980-12-17,20,0.00,1000.00 |
| 749,Allan,sales_man,769,1981-02-20,30,300.00,2000.00 |
| 752,Ward,sales_man,769,1981-02-22,30,500.00,1300.00 |
| 756,Jones,manager,783,1981-04-02,20,0.00,2300.00 |
| NULL |
| 769,Blake,Manager,783,1981-05-01,30,0.00,2870.00 |
| 778,Clark,Manager,783,1981-06-09,10,0.00,2900.00 |
| NULL |
| 784,Turner,sales_man,769,1981-09-08,30,0.00,1450.00 |
| 787,Adams,Clerk,778,1983-01-12,20,0.00,1150.00 |
| 788,Scott,Analyst,756,1982-12-09,20,0.00,2850.00 |
| 790,James,Clerk,769,1981-12-03,30,0.00,950.00 |
| 792,Ford,analyst,756,1981-12-03,20,0.00,2600.00 |
| 793,Miller,clerk,788,1982-01-23,40,0.00,1300.00 |
+------------------------------------------------------+
14 rows in set (0.02 sec)

mysql> select ename,salary from employee where salary>2850;,


+-------+---------+
| ename | salary |
+-------+---------+
| Blake | 2870.00 |
| Clark | 2900.00 |
| King | 2950.00 |
+-------+---------+
3 rows in set (0.11 sec)

-> select ename,salary from employee where salary>2850;select ename,salary from


employee where salary>2850;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '
select ename,salary from employee where salary>2850' at line 1
+-------+---------+
| ename | salary |
+-------+---------+
| Blake | 2870.00 |
| Clark | 2900.00 |
| King | 2950.00 |
+-------+---------+
3 rows in set (0.01 sec)

mysql> select ename,dno from employee where eno=790;


+-------+------+
| ename | dno |
+-------+------+
| James | 30 |
+-------+------+
1 row in set (0.06 sec)

mysql> select ename,salary from employee where salary< 1500 or salary>2850;


+--------+---------+
| ename | salary |
+--------+---------+
| Smith | 1000.00 |
| Ward | 1300.00 |
| Martin | 1250.00 |
| Blake | 2870.00 |
| Clark | 2900.00 |
| King | 2950.00 |
| Turner | 1450.00 |
| Adams | 1150.00 |
| James | 950.00 |
| Miller | 1300.00 |
+--------+---------+
10 rows in set (0.06 sec)

mysql> select ename,job_type,hire_date from employee where hire_date between '1981-


02-20' and '1981-05-01' order by hire_date;
+--------+-----------+------------+
| ename | job_type | hire_date |
+--------+-----------+------------+
| Allan | sales_man | 1981-02-20 |
| Ward | sales_man | 1981-02-22 |
| Jones | manager | 1981-04-02 |
| Martin | sales_man | 1981-04-22 |
| Blake | Manager | 1981-05-01 |
+--------+-----------+------------+
5 rows in set (0.06 sec)

mysql> select ename,dno from employee where dno=10 ||dno=30 order by ename;
+--------+------+
| ename | dno |
+--------+------+
| Allan | 30 |
| Blake | 30 |
| Clark | 10 |
| James | 30 |
| King | 10 |
| Martin | 30 |
| Turner | 30 |
| Ward | 30 |
+--------+------+
8 rows in set (0.00 sec)

mysql> select ename,salary from employee where dno=10 ||dno=30 && salary >1500;
+-------+---------+
| ename | salary |
+-------+---------+
| Allan | 2000.00 |
| Blake | 2870.00 |
| Clark | 2900.00 |
| King | 2950.00 |
+-------+---------+
4 rows in set (0.00 sec)

mysql> select ename,hire_date from employee where hire_date like'1981-%%-%%';


+--------+------------+
| ename | hire_date |
+--------+------------+
| Allan | 1981-02-20 |
| Ward | 1981-02-22 |
| Jones | 1981-04-02 |
| Martin | 1981-04-22 |
| Blake | 1981-05-01 |
| Clark | 1981-06-09 |
| King | 1981-11-17 |
| Turner | 1981-09-08 |
| James | 1981-12-03 |
| Ford | 1981-12-03 |
+--------+------------+
10 rows in set, 1 warning (0.05 sec)

mysql> select ename,job_type from employee where manager is null;


+--------+-----------+
| ename | job_type |
+--------+-----------+
| Martin | sales_man |
| King | President |
+--------+-----------+
2 rows in set (0.00 sec)

mysql> select ename,salary,commission from employee where commission>0 order by


salary,commission desc;
+--------+---------+------------+
| ename | salary | commission |
+--------+---------+------------+
| Martin | 1250.00 | 1400.00 |
| Ward | 1300.00 | 500.00 |
| Allan | 2000.00 | 300.00 |
+--------+---------+------------+
3 rows in set (0.00 sec)

mysql> select ename from employee where ename like'__a%';


+-------+
| ename |
+-------+
| Blake |
| Clark |
| Adams |
+-------+
3 rows in set (0.00 sec)

mysql>

mysql> select ename from employee where salary >(select max(salary) from employee
where job-type='clerk');
ERROR 1054 (42S22): Unknown column 'job' in 'where clause'
mysql> select ename from employee where salary >(select max(salary) from employee
where job_type='clerk');
+--------+
| ename |
+--------+
| Allan |
| Jones |
| Blake |
| Clark |
| King |
| Turner |
| Scott |
| Ford |
+--------+
8 rows in set (0.00 sec)

mysql> select datediff(curdate(),hire_date) from employee;


+-------------------------------+
| datediff(curdate(),hire_date) |
+-------------------------------+
| 13434 |
| 13369 |
| 13367 |
| 13328 |
| 13308 |
| 13299 |
| 13260 |
| 13099 |
| 13169 |
| 12678 |
| 12712 |
| 13083 |
| 13083 |
| 13032 |
+-------------------------------+
14 rows in set (0.03 sec)

mysql> select datediff(curdate(),hire_date)/30.5 from employee;


+------------------------------------+
| datediff(curdate(),hire_date)/30.5 |
+------------------------------------+
| 440.4590 |
| 438.3279 |
| 438.2623 |
| 436.9836 |
| 436.3279 |
| 436.0328 |
| 434.7541 |
| 429.4754 |
| 431.7705 |
| 415.6721 |
| 416.7869 |
| 428.9508 |
| 428.9508 |
| 427.2787 |
+------------------------------------+
14 rows in set (0.00 sec)

mysql> select concat('ename' 'earns' 'salary' 'monthly but wants' '3*current


salary') as dream salary;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'salary'
at line 1
mysql> select concat('ename' 'earns' 'salary' 'monthly but wants' '3*current
salary') as 'dream_salary' from employee;
+---------------------------------------------------+
| dream_salary |
+---------------------------------------------------+
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
| enameearnssalarymonthly but wants3*current salary |
+---------------------------------------------------+
14 rows in set (0.00 sec)

mysql> select concat(ename,' ', 'earns', salary,' ', 'monthly but wants',
(3*salary))as 'dream salary' from employee;
+----------------------------------------------+
| dream salary |
+----------------------------------------------+
| Smith earns1000.00 monthly but wants3000.00 |
| Allan earns2000.00 monthly but wants6000.00 |
| Ward earns1300.00 monthly but wants3900.00 |
| Jones earns2300.00 monthly but wants6900.00 |
| Martin earns1250.00 monthly but wants3750.00 |
| Blake earns2870.00 monthly but wants8610.00 |
| Clark earns2900.00 monthly but wants8700.00 |
| King earns2950.00 monthly but wants8850.00 |
| Turner earns1450.00 monthly but wants4350.00 |
| Adams earns1150.00 monthly but wants3450.00 |
| Scott earns2850.00 monthly but wants8550.00 |
| James earns950.00 monthly but wants2850.00 |
| Ford earns2600.00 monthly but wants7800.00 |
| Miller earns1300.00 monthly but wants3900.00 |
+----------------------------------------------+
14 rows in set (0.00 sec)

mysql>
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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 |
| hamu |
| hamu2 |
| mysql |
| performance_schema |
| phpmyadmin |
+--------------------+
6 rows in set (0.03 sec)

mysql> use hamu2;


Database changed
mysql> show tables;
+-----------------+
| Tables_in_hamu2 |
+-----------------+
| department |
| employee |
+-----------------+
2 rows in set (0.00 sec)

mysql> select * from employee;


+-----+--------+-----------+---------+------------+------+------------+---------+
| eno | ename | job_type | manager | hire_date | dno | commission | salary |
+-----+--------+-----------+---------+------------+------+------------+---------+
| 736 | Smith | clerk | 790 | 1980-12-17 | 20 | 0.00 | 1000.00 |
| 749 | Allan | sales_man | 769 | 1981-02-20 | 30 | 300.00 | 2000.00 |
| 752 | Ward | sales_man | 769 | 1981-02-22 | 30 | 500.00 | 1300.00 |
| 756 | Jones | manager | 783 | 1981-04-02 | 20 | 0.00 | 2300.00 |
| 765 | Martin | sales_man | NULL | 1981-04-22 | 30 | 1400.00 | 1250.00 |
| 769 | Blake | Manager | 783 | 1981-05-01 | 30 | 0.00 | 2870.00 |
| 778 | Clark | Manager | 783 | 1981-06-09 | 10 | 0.00 | 2900.00 |
| 783 | King | President | NULL | 1981-11-17 | 10 | 0.00 | 2950.00 |
| 784 | Turner | sales_man | 769 | 1981-09-08 | 30 | 0.00 | 1450.00 |
| 787 | Adams | Clerk | 778 | 1983-01-12 | 20 | 0.00 | 1150.00 |
| 788 | Scott | Analyst | 756 | 1982-12-09 | 20 | 0.00 | 2850.00 |
| 790 | James | Clerk | 769 | 1981-12-03 | 30 | 0.00 | 950.00 |
| 792 | Ford | analyst | 756 | 1981-12-03 | 20 | 0.00 | 2600.00 |
| 793 | Miller | clerk | 788 | 1982-01-23 | 40 | 0.00 | 1300.00 |
+-----+--------+-----------+---------+------------+------+------------+---------+
14 rows in set (0.09 sec)

mysql> Select * from department;


+-----+------------+-----------+
| dno | dname | location |
+-----+------------+-----------+
| 10 | Accounting | New York |
| 20 | Research | Dallas |
| 30 | Sales | Chicago |
| 40 | Operation | Boston |
| 50 | Marketing | New Delhi |
+-----+------------+-----------+
5 rows in set (0.08 sec)

mysql> 26
-> select ename,hire_date,dayname(hire_date) 'Day' from employee;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '26
select ename,hire_date,dayname(hire_date) 'Day' from employee' at line 1
mysql> select ename,hire_date,dayname(hire_date) 'Day' from employee;
+--------+------------+-----------+
| ename | hire_date | Day |
+--------+------------+-----------+
| Smith | 1980-12-17 | Wednesday |
| Allan | 1981-02-20 | Friday |
| Ward | 1981-02-22 | Sunday |
| Jones | 1981-04-02 | Thursday |
| Martin | 1981-04-22 | Wednesday |
| Blake | 1981-05-01 | Friday |
| Clark | 1981-06-09 | Tuesday |
| King | 1981-11-17 | Tuesday |
| Turner | 1981-09-08 | Tuesday |
| Adams | 1983-01-12 | Wednesday |
| Scott | 1982-12-09 | Thursday |
| James | 1981-12-03 | Thursday |
| Ford | 1981-12-03 | Thursday |
| Miller | 1982-01-23 | Saturday |
+--------+------------+-----------+
14 rows in set (0.00 sec)

mysql> select ename,'No commission' as commission from employee where eno Not IN
(select eno from employee where commission >;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at
line 1
mysql> select ename,'No commission' as commission from employee where eno Not IN
(select eno from employee where commission >0);
+--------+---------------+
| ename | commission |
+--------+---------------+
| Smith | No commission |
| Jones | No commission |
| Blake | No commission |
| Clark | No commission |
| King | No commission |
| Turner | No commission |
| Adams | No commission |
| Scott | No commission |
| James | No commission |
| Ford | No commission |
| Miller | No commission |
+--------+---------------+
11 rows in set (0.00 sec)

mysql> select ename,dname,department.dno from employee, department where


employee.dno=department.dno;
+--------+------------+-----+
| ename | dname | dno |
+--------+------------+-----+
| Clark | Accounting | 10 |
| King | Accounting | 10 |
| Smith | Research | 20 |
| Jones | Research | 20 |
| Adams | Research | 20 |
| Scott | Research | 20 |
| Ford | Research | 20 |
| Allan | Sales | 30 |
| Ward | Sales | 30 |
| Martin | Sales | 30 |
| Blake | Sales | 30 |
| Turner | Sales | 30 |
| James | Sales | 30 |
| Miller | Operation | 40 |
+--------+------------+-----+
14 rows in set (0.05 sec)

mysql> select distinct(job_type) from employee where dno=30;


+-----------+
| job_type |
+-----------+
| sales_man |
| Manager |
| Clerk |
+-----------+
3 rows in set (0.00 sec)

mysql> select ename,dname,employee.dno from employee,department where


employee.dno=department.dno and commission > 0;
+--------+-------+------+
| ename | dname | dno |
+--------+-------+------+
| Allan | Sales | 30 |
| Ward | Sales | 30 |
| Martin | Sales | 30 |
+--------+-------+------+
3 rows in set (0.00 sec)

mysql> select ename,dname from employee,department where


employee.dno=department.dno and ename like '%a%';
+--------+------------+
| ename | dname |
+--------+------------+
| Clark | Accounting |
| Adams | Research |
| Allan | Sales |
| Ward | Sales |
| Martin | Sales |
| Blake | Sales |
| James | Sales |
+--------+------------+
7 rows in set (0.00 sec)

mysql> select ename,job_type,department.dno,dname from employee,department where


location = 'Dallas' and department.dno=employee.dno;
+-------+----------+-----+----------+
| ename | job_type | dno | dname |
+-------+----------+-----+----------+
| Smith | clerk | 20 | Research |
| Jones | manager | 20 | Research |
| Adams | Clerk | 20 | Research |
| Scott | Analyst | 20 | Research |
| Ford | analyst | 20 | Research |
+-------+----------+-----+----------+
5 rows in set (0.00 sec)

mysql> select eno,ename,salary from employee where salary >(select avg(salary) from
employee) and
-> -> dno IN (select dno from employee where ename like '%t%');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '> dno IN
(select dno from employee where ename like '%t%')' at line 2
mysql> select eno,ename,salary from employee where salary >(select avg(salary) from
employee) and
-> -> dno IN (select dno from employee where ename like '%t%');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '> dno IN
(select dno from employee where ename like '%t%')' at line 2
mysql> select eno,ename,salary from employee where salary >(select avg(salary) from
employee) and
-> dno IN (select dno from employee where ename like '%t%');
+-----+-------+---------+
| eno | ename | salary |
+-----+-------+---------+
| 749 | Allan | 2000.00 |
| 756 | Jones | 2300.00 |
| 769 | Blake | 2870.00 |
| 788 | Scott | 2850.00 |
| 792 | Ford | 2600.00 |
+-----+-------+---------+
5 rows in set (0.00 sec)

mysql> select ename,dno,salary from employee where dno in (select dno from employee
where commission > 0) and salary in (select salary from employee where
commission>0);
+--------+------+---------+
| ename | dno | salary |
+--------+------+---------+
| Allan | 30 | 2000.00 |
| Ward | 30 | 1300.00 |
| Martin | 30 | 1250.00 |
+--------+------+---------+
3 rows in set (0.02 sec)

mysql> select ename,hire_date from employee where hire_date > (select hire_date
from employee where ename='Blake');
+--------+------------+
| ename | hire_date |
+--------+------------+
| Clark | 1981-06-09 |
| King | 1981-11-17 |
| Turner | 1981-09-08 |
| Adams | 1983-01-12 |
| Scott | 1982-12-09 |
| James | 1981-12-03 |
| Ford | 1981-12-03 |
| Miller | 1982-01-23 |
+--------+------------+
8 rows in set (0.00 sec)

mysql> select max(salary),min(salary),sum(salary),avg(salary) from employee;


+-------------+-------------+-------------+-------------+
| max(salary) | min(salary) | sum(salary) | avg(salary) |
+-------------+-------------+-------------+-------------+
| 2950.00 | 950.00 | 26870.00 | 1919.285714 |
+-------------+-------------+-------------+-------------+
1 row in set (0.00 sec)

mysql> select max(salary),min(salary),sum(salary),avg(salary) from employee group


by job_type;
+-------------+-------------+-------------+-------------+
| max(salary) | min(salary) | sum(salary) | avg(salary) |
+-------------+-------------+-------------+-------------+
| 2850.00 | 2600.00 | 5450.00 | 2725.000000 |
| 1300.00 | 950.00 | 4400.00 | 1100.000000 |
| 2900.00 | 2300.00 | 8070.00 | 2690.000000 |
| 2950.00 | 2950.00 | 2950.00 | 2950.000000 |
| 2000.00 | 1250.00 | 6000.00 | 1500.000000 |
+-------------+-------------+-------------+-------------+
5 rows in set (0.00 sec)

mysql> select count(*) from employee group by job_type;


+----------+
| count(*) |
+----------+
| 2 |
| 4 |
| 3 |
| 1 |
| 4 |
+----------+
5 rows in set (0.00 sec)

mysql> select count(distinct(manager)) from employee;


+--------------------------+
| count(distinct(manager)) |
+--------------------------+
| 6 |
+--------------------------+
1 row in set (0.00 sec)

mysql> select max(salary)-min(salary) from employee;


+-------------------------+
| max(salary)-min(salary) |
+-------------------------+
| 2000.00 |
+-------------------------+
1 row in set (0.00 sec)

mysql> select manager,min(salary) minSalary from employee group by manager having


minSalary>=1000 and manager is not NULL;
+---------+-----------+
| manager | minSalary |
+---------+-----------+
| 756 | 2600.00 |
| 778 | 1150.00 |
| 783 | 2300.00 |
| 788 | 1300.00 |
| 790 | 1000.00 |
+---------+-----------+
5 rows in set (0.01 sec)

mysql> select dname,location,count(eno),avg(salary) from employee,department where


employee.dno=department.dno group by dname;
+------------+----------+------------+-------------+
| dname | location | count(eno) | avg(salary) |
+------------+----------+------------+-------------+
| Accounting | New York | 2 | 2925.000000 |
| Operation | Boston | 1 | 1300.000000 |
| Research | Dallas | 5 | 1980.000000 |
| Sales | Chicago | 6 | 1636.666667 |
+------------+----------+------------+-------------+
4 rows in set (0.00 sec)

mysql> elect ename,hire_date from employee where dno =(select dno from employee
where ename like 'Blake');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'elect
ename,hire_date from employee where dno =(select dno from employee where e' at line
1
mysql> elect ename,hire_date from employee where dno =(select dno from employee
where ename like 'Blake');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'elect
ename,hire_date from employee where dno =(select dno from employee where e' at line
1
mysql> select ename,hire_date from employee where dno =(select dno from employee
where ename like 'Blake');
+--------+------------+
| ename | hire_date |
+--------+------------+
| Allan | 1981-02-20 |
| Ward | 1981-02-22 |
| Martin | 1981-04-22 |
| Blake | 1981-05-01 |
| Turner | 1981-09-08 |
| James | 1981-12-03 |
+--------+------------+
6 rows in set (0.00 sec)

mysql> select eno,ename from employee where salary >(select avg(salary) from
employee)
-> -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '>' at
line 2
mysql> select eno,ename from employee where salary >(select avg(salary) from
employee)
-> ;
+-----+-------+
| eno | ename |
+-----+-------+
| 749 | Allan |
| 756 | Jones |
| 769 | Blake |
| 778 | Clark |
| 783 | King |
| 788 | Scott |
| 792 | Ford |
+-----+-------+
7 rows in set (0.00 sec)

mysql> select eno,ename from employee where dno IN (select dno from employee where
ename like '%t%');
+-----+--------+
| eno | ename |
+-----+--------+
| 736 | Smith |
| 749 | Allan |
| 752 | Ward |
| 756 | Jones |
| 765 | Martin |
| 769 | Blake |
| 784 | Turner |
| 787 | Adams |
| 788 | Scott |
| 790 | James |
| 792 | Ford |
+-----+--------+
11 rows in set (0.00 sec)

mysql> select ename,salary from employee where manager = (select eno from employee
where ename like 'KING');
+-------+---------+
| ename | salary |
+-------+---------+
| Jones | 2300.00 |
| Blake | 2870.00 |
| Clark | 2900.00 |
+-------+---------+
3 rows in set (0.00 sec)

mysql> select dno,ename,job_type from employee where dno = (select dno from
department where dname = 'Sales');
+------+--------+-----------+
| dno | ename | job_type |
+------+--------+-----------+
| 30 | Allan | sales_man |
| 30 | Ward | sales_man |
| 30 | Martin | sales_man |
| 30 | Blake | Manager |
| 30 | Turner | sales_man |
| 30 | James | Clerk |
+------+--------+-----------+
6 rows in set (0.00 sec)
mysql>

You might also like