You are on page 1of 24

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> select ename from employee where (ename like'%r%r%' ||ename like'%a%a%') &&
(dno=30 ||eno=778);
+--------+
| ename |
+--------+
| Allan |
| Turner |
+--------+
2 rows in set (0.00 sec)
mysql> select ename from employee where (ename like'%r%r%' ||ename like'%a%a%') &&
(dno=30 ||eno=778);
+--------+
| ename |
+--------+
| Allan |
| Turner |
+--------+
2 rows in set (0.00 sec)

mysql> select ename,job_type,salary from employee where (job_type='clerk'||


job_type='analyst') && (salary!=1000 && salary!=3000 && salary!=5000);
+--------+----------+---------+
| ename | job_type | salary |
+--------+----------+---------+
| Adams | Clerk | 1150.00 |
| Scott | Analyst | 2850.00 |
| James | Clerk | 950.00 |
| Ford | analyst | 2600.00 |
| Miller | clerk | 1300.00 |
+--------+----------+---------+
5 rows in set (0.00 sec)

mysql> select ename,salary,commission from employee where


commission>(salary+0.05*salary);
+--------+---------+------------+
| ename | salary | commission |
+--------+---------+------------+
| Martin | 1250.00 | 1400.00 |
+--------+---------+------------+
1 row in set (0.08 sec)

mysql> select curdate();


+------------+
| curdate() |
+------------+
| 2017-09-28 |
+------------+
1 row in set (0.13 sec)

mysql> select eno,ename,salary,round(salary+salary*0.15) as 'new salary' from


employee;
+-----+--------+---------+------------+
| eno | ename | salary | new salary |
+-----+--------+---------+------------+
| 736 | Smith | 1000.00 | 1150 |
| 749 | Allan | 2000.00 | 2300 |
| 752 | Ward | 1300.00 | 1495 |
| 756 | Jones | 2300.00 | 2645 |
| 765 | Martin | 1250.00 | 1438 |
| 769 | Blake | 2870.00 | 3301 |
| 778 | Clark | 2900.00 | 3335 |
| 783 | King | 2950.00 | 3393 |
| 784 | Turner | 1450.00 | 1668 |
| 787 | Adams | 1150.00 | 1323 |
| 788 | Scott | 2850.00 | 3278 |
| 790 | James | 950.00 | 1093 |
| 792 | Ford | 2600.00 | 2990 |
| 793 | Miller | 1300.00 | 1495 |
+-----+--------+---------+------------+
14 rows in set (0.02 sec)

mysql> select ename,hire_date,date_add(hire_date, interval 6 month + dayofweek())as


salary_review_date 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 '+
dayofweek())as salary_review_date from employee' at line 1
mysql> select ename,hire_date,date_add(hire_date, interval 6 month + dayofweek())as
salary_review_date 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 '+
dayofweek())as salary_review_date from employee' at line 1
mysql> select ename,hire_date,date_add(hire_date, interval 6 month + dayofweek())as
salary_review_date 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 '+
dayofweek())as salary_review_date from employee' at line 1
mysql> select ename,hire_date,date_add(hire_date, interval 6 month)as
salary_review_date from employee;
+--------+------------+--------------------+
| ename | hire_date | salary_review_date |
+--------+------------+--------------------+
| Smith | 1980-12-17 | 1981-06-17 |
| Allan | 1981-02-20 | 1981-08-20 |
| Ward | 1981-02-22 | 1981-08-22 |
| Jones | 1981-04-02 | 1981-10-02 |
| Martin | 1981-04-22 | 1981-10-22 |
| Blake | 1981-05-01 | 1981-11-01 |
| Clark | 1981-06-09 | 1981-12-09 |
| King | 1981-11-17 | 1982-05-17 |
| Turner | 1981-09-08 | 1982-03-08 |
| Adams | 1983-01-12 | 1983-07-12 |
| Scott | 1982-12-09 | 1983-06-09 |
| James | 1981-12-03 | 1982-06-03 |
| Ford | 1981-12-03 | 1982-06-03 |
| Miller | 1982-01-23 | 1982-07-23 |
+--------+------------+--------------------+
14 rows in set (0.03 sec)

mysql> commit;
Query OK, 0 rows affected (0.08 sec)

mysql> use hamu;


Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table tracks(


-> track_id int primary key,
-> artist_id int,
-> tname varchar(255),
-> length int,
-> foreign key(artist_id) references artists(artist_id)
-> );
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> create table artists(
-> artist_id int primary key,
-> sname varchar(255),
-> location varchar(255)
-> );
Query OK, 0 rows affected (0.44 sec)

mysql> create table tracks(


-> track_id int primary key,
-> artist_id int,
-> tname varchar(255),
-> length int,
-> foreign key(artist_id) references artists(artist_id)
-> );
Query OK, 0 rows affected (0.52 sec)

mysql>
mysql> insert into artists(artist_id,sname,location)
-> values(1001,'Alla Rakha Rehman','Chennai'),
-> (1002,'Kailas Kher','Delhi'),
-> (1003,'Rahul Dev Barman','Kolkata'),
-> (1004,'Lucky Ali','Mumbai');
Query OK, 4 rows affected (0.14 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> insert into tracks(track_id,artist_id,tname,length)


-> values(10,1001,'Bande Matram',6),
-> (11,1002,'Alla Ke Bande',10),
-> (12,1003,'Raste Apni Jagah',12),
-> (13,1003,'Kahi Door Jab',5),
-> (14,1001,'Maa Tujhe Salaam',8),
-> (15,1002,'Teri Dewani',6),
-> (16,1003,'Ak Rasta Hai',7);
Query OK, 7 rows affected (0.11 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> select * from artists;


+-----------+-------------------+----------+
| artist_id | sname | location |
+-----------+-------------------+----------+
| 1001 | Alla Rakha Rehman | Chennai |
| 1002 | Kailas Kher | Delhi |
| 1003 | Rahul Dev Barman | Kolkata |
| 1004 | Lucky Ali | Mumbai |
+-----------+-------------------+----------+
4 rows in set (0.00 sec)

mysql> select * from tracks;


+----------+-----------+------------------+--------+
| track_id | artist_id | tname | length |
+----------+-----------+------------------+--------+
| 10 | 1001 | Bande Matram | 6 |
| 11 | 1002 | Alla Ke Bande | 10 |
| 12 | 1003 | Raste Apni Jagah | 12 |
| 13 | 1003 | Kahi Door Jab | 5 |
| 14 | 1001 | Maa Tujhe Salaam | 8 |
| 15 | 1002 | Teri Dewani | 6 |
| 16 | 1003 | Ak Rasta Hai | 7 |
+----------+-----------+------------------+--------+
7 rows in set (0.00 sec)

mysql> create table emp(


-> eid int primary key,
-> ename varchar(255),
-> age int,
-> salary int
-> );
Query OK, 0 rows affected (0.47 sec)

mysql>
mysql>
mysql> create table dept(
-> did int primary key,
-> dname varchar(255),
-> budget int,
-> managerid int
-> );
Query OK, 0 rows affected (0.52 sec)

mysql> create table works(


-> eid int,
-> did int,
-> pct_time int,
-> foreign key(eid) references emp(eid),
-> foreign key(did) references dept(did)
-> );
Query OK, 0 rows affected (0.44 sec)

mysql>
mysql> insert into emp(eid,ename,age,salary)
-> values(1001,'Sabeer Bhatia',60,50000),
-> (1002,'Pranav Mistry',65,55000),
-> (1003,'Sam Pitroda',50,60000),
-> (1004,'Mark Zuckerberg',50,35000),
-> (1005,'Larry Page',40,45000),
-> (1006,'Steve Ballmer',45,46000),
-> (1007,'Tim Cook',80,65000);
Query OK, 7 rows affected (0.08 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> insert into dept(did,dname,budget,managerid)


-> values(10,'Hardware',10000,1001),
-> (20,'Software',20000,1002),
-> (30,'Quality Assurance',30000,1003),
-> (40,'Development',15000,1004),
-> (50,'Human Resources',40000,1005),
-> (60,'Support',12000,1006);
Query OK, 6 rows affected (0.11 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> insert into works(eid,did,pct_time)


-> values(1001,10,20),
-> (1002,20,30),
-> (1004,40,15),
-> (1005,40,50),
-> (1003,30,80),
-> (1006,60,35),
-> (1005,50,40),
-> (1001,20,15);
Query OK, 8 rows affected (0.30 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> select
-> * from emp;
+------+-----------------+------+--------+
| eid | ename | age | salary |
+------+-----------------+------+--------+
| 1001 | Sabeer Bhatia | 60 | 50000 |
| 1002 | Pranav Mistry | 65 | 55000 |
| 1003 | Sam Pitroda | 50 | 60000 |
| 1004 | Mark Zuckerberg | 50 | 35000 |
| 1005 | Larry Page | 40 | 45000 |
| 1006 | Steve Ballmer | 45 | 46000 |
| 1007 | Tim Cook | 80 | 65000 |
+------+-----------------+------+--------+
7 rows in set (0.00 sec)

mysql> select * from dept;


+-----+-------------------+--------+-----------+
| did | dname | budget | managerid |
+-----+-------------------+--------+-----------+
| 10 | Hardware | 10000 | 1001 |
| 20 | Software | 20000 | 1002 |
| 30 | Quality Assurance | 30000 | 1003 |
| 40 | Development | 15000 | 1004 |
| 50 | Human Resources | 40000 | 1005 |
| 60 | Support | 12000 | 1006 |
+-----+-------------------+--------+-----------+
6 rows in set (0.00 sec)

mysql> select *from works;


+------+------+----------+
| eid | did | pct_time |
+------+------+----------+
| 1001 | 10 | 20 |
| 1002 | 20 | 30 |
| 1004 | 40 | 15 |
| 1005 | 40 | 50 |
| 1003 | 30 | 80 |
| 1006 | 60 | 35 |
| 1005 | 50 | 40 |
| 1001 | 20 | 15 |
+------+------+----------+
8 rows in set (0.00 sec)

mysql> create table suppliers(


-> sid int primary key,
-> sname varchar(255),
-> scity varchar(255)
-> );
Query OK, 0 rows affected (0.67 sec)

mysql> create table parts(


-> pid int primary key,
-> pname varchar(255),
-> color varchar(255)
-> );
Query OK, 0 rows affected (0.30 sec)

mysql>
mysql> create table catalog(
-> sid int,
-> pid int,
-> price int,
-> foreign key(sid) references suppliers(sid),
-> foreign key(pid) references parts(pid)
-> );
Query OK, 0 rows affected (0.56 sec)

mysql> insert into suppliers(sid,sname,scity)


-> values(1001,'Tata','Delhi'),
-> (1002,'Toyota','Tokyo'),
-> (1003,'Maruti','Fukishima'),
-> (1004,'Ford','Detroit'),
-> (1005,'Chevrolet','New York'),
-> (1006,'Nissan','Hiroshima'),
-> (1007,'Renault','Paris'),
-> (1008,'Mahindra','Pune');
Query OK, 8 rows affected (0.09 sec)
Records: 8 Duplicates: 0 Warnings: 0

mysql> insert into parts(pid,pname,color)


-> values(11,'Wind Shield Wiper','Red'),
-> (12,'Alloy Wheel','Green'),
-> (13,'Steering','Red'),
-> (14,'Tyre','Black'),
-> (15,'Handle','Green');
Query OK, 5 rows affected (0.13 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> insert into catalog(sid,pid,price)


-> values(1001,11,20000),
-> (1002,11,30000),
-> (1003,11,25000),
-> (1004,12,22000),
-> (1004,13,21000),
-> (1004,14,40000),
-> (1005,12,33000),
-> (1008,13,44000),
-> (1007,14,11000);
Query OK, 9 rows affected (0.17 sec)
Records: 9 Duplicates: 0 Warnings: 0

mysql> select * from supliers;


ERROR 1146 (42S02): Table 'hamu.supliers' doesn't exist
mysql> select * from suppliers;
+------+-----------+-----------+
| sid | sname | scity |
+------+-----------+-----------+
| 1001 | Tata | Delhi |
| 1002 | Toyota | Tokyo |
| 1003 | Maruti | Fukishima |
| 1004 | Ford | Detroit |
| 1005 | Chevrolet | New York |
| 1006 | Nissan | Hiroshima |
| 1007 | Renault | Paris |
| 1008 | Mahindra | Pune |
+------+-----------+-----------+
8 rows in set (0.00 sec)
mysql> select * from parts;
+-----+-------------------+-------+
| pid | pname | color |
+-----+-------------------+-------+
| 11 | Wind Shield Wiper | Red |
| 12 | Alloy Wheel | Green |
| 13 | Steering | Red |
| 14 | Tyre | Black |
| 15 | Handle | Green |
+-----+-------------------+-------+
5 rows in set (0.00 sec)

mysql> select * from catalog;


+------+------+-------+
| sid | pid | price |
+------+------+-------+
| 1001 | 11 | 20000 |
| 1002 | 11 | 30000 |
| 1003 | 11 | 25000 |
| 1004 | 12 | 22000 |
| 1004 | 13 | 21000 |
| 1004 | 14 | 40000 |
| 1005 | 12 | 33000 |
| 1008 | 13 | 44000 |
| 1007 | 14 | 11000 |
+------+------+-------+
9 rows in set (0.00 sec)

mysql> create table sailors(


-> sid int primary key,
-> sname varchar(255),
-> rating int,
-> age int
-> );
Query OK, 0 rows affected (0.38 sec)

mysql> create table boats(


-> bid int primary key,
-> bname varchar(255),
-> color varchar(255)
-> );
Query OK, 0 rows affected (0.43 sec)

mysql> create table reserves(


-> sid int,
-> bid int,
-> day int,
-> foreign key(sid) references sailors(sid),
-> foreign key(bid) references boats(bid)
-> );
Query OK, 0 rows affected (0.56 sec)

mysql> insert into sailors(sid,sname,rating,age)


-> values(1001,'Jack Sparrow',8,34),
-> (1002,'William Nick',6,55),
-> (1003,'Columbus',3,45),
-> (1004,'Vasco D Gama',5,29),
-> (1005,'Magellan',9,51),
-> (1006,'Witwikcy',1,45);
Query OK, 6 rows affected (0.11 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> insert into boats(bid,bname,color)


-> values(12,'INS Virat','Red'),
-> (13,'INS Arjun','Green'),
-> (14,'INS Kavery','Blue'),
-> (15,'INS Ajay','Green');
Query OK, 4 rows affected (0.13 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql> insert into reserves(sid,bid,day)


-> values(1001,12,2),
-> (1001,13,2),
-> (1002,14,3),
-> (1002,15,4),
-> (1003,12,5),
-> (1004,12,6),
-> (1005,15,1);
Query OK, 7 rows affected (0.25 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql>
mysql> select * from reserves;
+------+------+------+
| sid | bid | day |
+------+------+------+
| 1001 | 12 | 2 |
| 1001 | 13 | 2 |
| 1002 | 14 | 3 |
| 1002 | 15 | 4 |
| 1003 | 12 | 5 |
| 1004 | 12 | 6 |
| 1005 | 15 | 1 |
+------+------+------+
7 rows in set (0.00 sec)

mysql> select* from sailers;


ERROR 1146 (42S02): Table 'hamu.sailers' doesn't exist
mysql> select* from sailors;
+------+--------------+--------+------+
| sid | sname | rating | age |
+------+--------------+--------+------+
| 1001 | Jack Sparrow | 8 | 34 |
| 1002 | William Nick | 6 | 55 |
| 1003 | Columbus | 3 | 45 |
| 1004 | Vasco D Gama | 5 | 29 |
| 1005 | Magellan | 9 | 51 |
| 1006 | Witwikcy | 1 | 45 |
+------+--------------+--------+------+
6 rows in set (0.00 sec)

mysql> select* from boats;


+-----+------------+-------+
| bid | bname | color |
+-----+------------+-------+
| 12 | INS Virat | Red |
| 13 | INS Arjun | Green |
| 14 | INS Kavery | Blue |
| 15 | INS Ajay | Green |
+-----+------------+-------+
4 rows in set (0.00 sec)

mysql> create table department(


-> deptid int primary key,
-> deptname varchar(255),
-> building varchar(255)
-> );
Query OK, 0 rows affected (0.48 sec)

mysql> create table professor(


-> profid int primary key,
-> deptid int,
-> profname varchar(255),
-> profage int,
-> salary int,
-> foreign key(deptid) references department(deptid)
-> );
Query OK, 0 rows affected (0.56 sec)

mysql> mysql> Ctrl-C -- exit!


create table committee(
-> commname varchar(255),
-> profid int,
-> foreign key(profid) references professor(profid)
-> create table committee(
-> commname varchar(255),
-> profid int,
-> foreign key(profid) references professor(profid)
-> );
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 'create
table committee(
commname varchar(255),
profid int,
foreign key(profid) r' at line 5
mysql> create table committee(
-> commname varchar(255),
-> profid int,
-> foreign key(profid) references professor(profid)
-> );
Query OK, 0 rows affected (0.35 sec)

mysql> insert into department(deptid,deptname,building)


-> values(10,'Mathematics','White'),
-> (11,'Physics','Red'),
-> (12,'Computer Science','Green'),
-> (13,'Zoology','Black');
Query OK, 4 rows affected (0.12 sec)
Records: 4 Duplicates: 0 Warnings: 0

mysql>
mysql> insert into professor(profid,deptid,profname,profage,salary)
-> values(2222,10,'Santosh Kumar',45,45000),
-> (2223,11,'Sukumar',44,55000),
-> (2224,11,'Naveen Garg',34,65000),
-> (2225,12,'Bimal Roy',49,47000),
-> (2226,13,'Sia Bal Pal',39,48000),
-> (2227,12,'Pramod Kumar Kapoor',40,79000),
-> (2228,13,'Sandeep Sen',42,59000);
Query OK, 7 rows affected (0.11 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> insert into committee(commname,profid)


-> values('Course Structure',2222),
-> ('Examination',2223),
-> ('Examination',2224),
-> ('Mark Evaluation',2225),
-> ('Scholarship',2226),
-> ('Discipline',2227);
Query OK, 6 rows affected (0.13 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> select * from committee;


+------------------+--------+
| commname | profid |
+------------------+--------+
| Course Structure | 2222 |
| Examination | 2223 |
| Examination | 2224 |
| Mark Evaluation | 2225 |
| Scholarship | 2226 |
| Discipline | 2227 |
+------------------+--------+
6 rows in set (0.00 sec)

mysql> select * from professor;


+--------+--------+---------------------+---------+--------+
| profid | deptid | profname | profage | salary |
+--------+--------+---------------------+---------+--------+
| 2222 | 10 | Santosh Kumar | 45 | 45000 |
| 2223 | 11 | Sukumar | 44 | 55000 |
| 2224 | 11 | Naveen Garg | 34 | 65000 |
| 2225 | 12 | Bimal Roy | 49 | 47000 |
| 2226 | 13 | Sia Bal Pal | 39 | 48000 |
| 2227 | 12 | Pramod Kumar Kapoor | 40 | 79000 |
| 2228 | 13 | Sandeep Sen | 42 | 59000 |
+--------+--------+---------------------+---------+--------+
7 rows in set (0.00 sec)

mysql> select * from department;


+--------+------------------+----------+
| deptid | deptname | building |
+--------+------------------+----------+
| 10 | Mathematics | White |
| 11 | Physics | Red |
| 12 | Computer Science | Green |
| 13 | Zoology | Black |
+--------+------------------+----------+
4 rows in set (0.00 sec)

mysql>
mysql>
mysql> create table pizza(
-> pid int primary key,
-> pname varchar(255),
-> size int
-> );
Query OK, 0 rows affected (0.61 sec)

mysql> create table store(


-> sname varchar(255) primary key,
-> location varchar(255),
-> Quality char(1)
-> );
Query OK, 0 rows affected (0.45 sec)

mysql> mysql> Ctrl-C -- exit!


create table soldby(
-> pid int,
-> sname varchar(255),
-> price int,
-> foreign key(pid) references pizza(pid),
-> foreign key(sname) references store(sname)
-> );
Query OK, 0 rows affected (0.41 sec)

mysql> insert into pizza(pid,pname,size)


-> values(101,'Domino',10),
-> (102,'Pizza Hut',20),
-> (103,'PSD',30),
-> (104,'Subway',25),
-> (105,'Mc Donald',30),
-> (106,'Nirullas',28);
Query OK, 6 rows affected (0.09 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> insert into store(sname,location,quality)


-> values('Cannaught Place','Central Delhi','A'),
-> ('India Gate','Central Delhi','B'),
-> ('Saket','South Delhi','B'),
-> ('North Store','Civil Lines','C'),
-> ('South Store','Malviya Nagar','D');
Query OK, 5 rows affected (0.11 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> insert into soldby(pid,sname,price)


-> values(101,'Cannaught Place',100),
-> (102,'India Gate',200),
-> (101,'North Store',300),
-> (103,'South Store',450),
-> (104,'Saket',110),
-> (105,'North Store',150),
-> (101,'North Store',250);
Query OK, 7 rows affected (0.33 sec)
Records: 7 Duplicates: 0 Warnings: 0

mysql> select * from pizza;


+-----+-----------+------+
| pid | pname | size |
+-----+-----------+------+
| 101 | Domino | 10 |
| 102 | Pizza Hut | 20 |
| 103 | PSD | 30 |
| 104 | Subway | 25 |
| 105 | Mc Donald | 30 |
| 106 | Nirullas | 28 |
+-----+-----------+------+
6 rows in set (0.00 sec)

mysql> select
-> * from store;
+-----------------+---------------+---------+
| sname | location | Quality |
+-----------------+---------------+---------+
| Cannaught Place | Central Delhi | A |
| India Gate | Central Delhi | B |
| North Store | Civil Lines | C |
| Saket | South Delhi | B |
| South Store | Malviya Nagar | D |
+-----------------+---------------+---------+
5 rows in set (0.00 sec)

mysql> select * from soldby;


+------+-----------------+-------+
| pid | sname | price |
+------+-----------------+-------+
| 101 | Cannaught Place | 100 |
| 102 | India Gate | 200 |
| 101 | North Store | 300 |
| 103 | South Store | 450 |
| 104 | Saket | 110 |
| 105 | North Store | 150 |
| 101 | North Store | 250 |
+------+-----------------+-------+
7 rows in set (0.00 sec)

mysql> select tname from tracks where length>10;


+------------------+
| tname |
+------------------+
| Raste Apni Jagah |
+------------------+
1 row in set (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> use hamu2;


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

mysql> select * department;


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
'department' at line 1
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.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.00 sec)

mysql> use hamu;


Database changed
mysql> select pname from pizza where pname like'__i%';
Empty set (0.00 sec)

mysql>
mysql> select pname from pizza where pname like'__i%';
Empty set (0.00 sec)

mysql>
mysql> select * from pizza;
+-----+-----------+------+
| pid | pname | size |
+-----+-----------+------+
| 101 | Domino | 10 |
| 102 | Pizza Hut | 20 |
| 103 | PSD | 30 |
| 104 | Subway | 25 |
| 105 | Mc Donald | 30 |
| 106 | Nirullas | 28 |
+-----+-----------+------+
6 rows in set (0.00 sec)

mysql> select pname from pizza where pname like'_i%';


+-----------+
| pname |
+-----------+
| Pizza Hut |
| Nirullas |
+-----------+
2 rows in set (0.00 sec)

mysql>
mysql> select pname from pizza where pid=(select pid from soldby group by pid order
by count(pid) desc limit 1);
+--------+
| pname |
+--------+
| Domino |
+--------+
1 row in set (0.06 sec)

mysql> select store.quality,avg(soldby.price) as 'average price' from store left


join soldby on store.sname=soldby.sname group by store.quality;
+---------+---------------+
| quality | average price |
+---------+---------------+
| A | 100.0000 |
| B | 155.0000 |
| C | 233.3333 |
| D | 450.0000 |
+---------+---------------+
4 rows in set (0.06 sec)

mysql>
mysql> select pname from pizza where pid not in (select pid from soldby);
+----------+
| pname |
+----------+
| Nirullas |
+----------+
1 row in set (0.02 sec)

mysql> select pname from pizza where pid in(select pid from soldby where
sname='north store');
+-----------+
| pname |
+-----------+
| Domino |
| Mc Donald |
+-----------+
2 rows in set (0.06 sec)

mysql> select pname from pizza where pname='__i%';


Empty set (0.00 sec)

mysql> select pname from where pid=( select max(soldby) from soldby);
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 'where
pid=( select max(soldby) from soldby)' at line 1
mysql> select pname from where pid=( select max(soldby) from soldby);
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 'where
pid=( select max(soldby) from soldby)' at line 1
mysql> select pname from where pid=( select max(price) from soldby);
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 'where
pid=( select max(price) from soldby)' at line 1
mysql> select pname from pizza where pid=( select max(price) from soldby);
Empty set (0.00 sec)

mysql> select pname from pizza where pid=(select pid from soldby group by pid order
by count(pid) desc limit 1);
+--------+
| pname |
+--------+
| Domino |
+--------+
1 row in set (0.00 sec)

mysql> select pname from pizza where pid is not in ( select pid from soldby);
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 'in
( select pid from soldby)' at line 1
mysql> select pname from pizza where pid not in ( select pid from soldby);
+----------+
| pname |
+----------+
| Nirullas |
+----------+
1 row in set (0.00 sec)

mysql> select pname from pizza where pid in ( select pid from soldby where
sname='north store');
+-----------+
| pname |
+-----------+
| Domino |
| Mc Donald |
+-----------+
2 rows in set (0.00 sec)

mysql> select avg(price) as avg_price from soldby;


+-----------+
| avg_price |
+-----------+
| 222.8571 |
+-----------+
1 row in set (0.00 sec)

mysql> select ename from employee where salary >(select ;


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>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql>
mysql> select ename from employee where salary >(select max(salary) from employee
where ename='clerk');
ERROR 1146 (42S02): Table 'hamu.employee' doesn't exist
mysql> use hamu2;
Database changed
mysql> select ename from employee where salary >(select max(salary) from employee
where ename='clerk');
Empty set (0.00 sec)

mysql> select ename from employee where salary >(select max(salary) from employee
where ename='clerk');
Empty set (0.00 sec)

mysql> show tables;


+-----------------+
| Tables_in_hamu2 |
+-----------------+
| department |
| employee |
+-----------------+
2 rows in set (0.00 sec)

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>

You might also like