You are on page 1of 22

Ex.No.

1 Data definition commands Data Manipulation Commands for inserting,


deleting, updating and retrieving Tables and Transaction Control statements
AIM:

To design and implement a database in MySQL using Structured Query Language


commands

SYNTAX:

CREATE :(command used for creating tables)


Create table <table name> (column name1 data type (size) constraints, column2 data type (size),..,
column name N data type(size));
DESC :(command used to view the table structure)
Desc<table name>;
DDL COMMANDS:
 Create
 Alter
 Add
 Modify
 Drop
 Rename
 Drop
 Truncate
ALTER:(command used for modifying the table structure)
ADD:( To add a column in a table)
Alter table <table name> add(column name1 datatype1);
MODIFY:( To change the data type of a column in a table)
Alter table <table name> modify(column name1 datatype1);
DROP:( To delete a column in a table)
Alter table <table name> drop (column name);
RENAME: (command used to change the name of the table)
RENAME TABLE tbl_name TO new_tbl_name [, tbl_name2 TO new_tbl_name2] ...
DROP: (command used removing for an existing table)
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT |
CASCADE]
TRUNCATE :( command is used to remove all the records in the table and not the table itself)
Truncate table <table name>;
DML Commands:
 Insert – to insert one or more number of rows.
 Select – to display one or more rows.
 Update – used to alter the column values in a table.
 Delete – used to delete one or more rows.
INSERT:
Syntax 1: Insert into <table name> values (‘attributes1’, ’attributes2’……);
Syntax 2: Insert into<table name>(column names)values(list of data values);
Syntax 3: Insert into <table name>values(&columname1,&columnname2…);
SELECT:
Syntax 1:Select column name1,columnname2 from <table name>;
Syntax 2: select * from <tablename>;
Syntax 3: select * from <tablename> where <condition>;
UPDATE:
Update <table name> set <column name>=’values’ where <condition>;
DELETE:
Delete from <table name>;
TCL Commands
COMMIT
Syntax:
Commit:
Syntax:
ROLLBACK TO [SAVEPOINT] savepointname; Where,
SAVEPOINT:is optional and is used to rollback a partial transaction, as far the specified
savepoint.
Savepointname: is a savepoint created in current transaction.
SAVEPOINT
Syntax:
SAVEPOINT savepointname;
PROBLEM STATEMENT:
 Customer browse the catalogue of books
 Customers place the orders
 Customers call the book store and give the ISBN of a book and quantity.
 Store prepares a shipment that contains the books customers have ordered
TABLE FROM THE PROBLEM STATEMENT
1) Books
2) Customers
3) Orders
4) Order_list
[root@localhost ~]# service mysqld start
[root@localhost ~]# mysql
mysql> create database sheryl;
Query OK, 1 row affected (0.00 sec)
mysql> use sheryl;
Database changed
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| sheryl |
| test |
+--------------------+
4 rows in set (0.00 sec)
Table name: Books
mysql> Create table books(ISBN varchar(10) primary key, title varchar(30),
author char(10),Quan_instock int,price int ,pub_year date);
Query OK, 0 rows affected (0.01 sec)
Description of table
mysql> desc books;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ISBN | varchar(10) | NO | PRI | NULL | |
| title | varchar(30) | YES | | NULL | |
| author | char(10) | YES | | NULL | |
| Quan_instock | int(11) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pub_year | date | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

Table name: Customers


mysql> create table customer(cust_id varchar(10) primary key, cust_name
varchar(20), cust_addr varchar(15), card_no varchar(15));
Query OK, 0 rows affected (0.01 sec)

Description of table
mysql> desc customer;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| cust_id | varchar(10) | NO | PRI | NULL | |
| cust_name | varchar(20) | YES | | NULL | |
| cust_addr | varchar(15) | YES | | NULL | |
| card_no | varchar(15) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
===========================================================================
Table name: Orders
mysql> create table orders(order_no varchar(10) primary key, cust_id
varchar(10),order_date date);
Query OK, 0 rows affected (0.00 sec)
Description of table
mysql> desc orders;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| order_no | varchar(10) | NO | PRI | NULL | |
| cust_id | varchar(10) | YES | | NULL | |
| order_date | date | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
==========================================================================
Table name: Order_list
mysql> create table order_list(order_no varchar(20) primary key, ISBN
varchar(10), quantity int,ship_date date);
Query OK, 0 rows affected (0.00 sec)
Description of table
mysql> desc order_list;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| order_no | varchar(20) | NO | PRI | NULL | |
| ISBN | varchar(10) | YES | | NULL | |
| quantity | int(11) | YES | | NULL | |
| ship_date | date | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
1.Write a query for creating new table from existing table with all fields.
mysql> create table cust as select * from customer;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
Description of table
mysql> desc cust;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| cust_id | varchar(10) | NO | | NULL | |
| cust_name | varchar(20) | YES | | NULL | |
| cust_addr | varchar(15) | YES | | NULL | |
| card_no | varchar(15) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
2.Write a query for creating new table from existing table with selected fields.
mysql> create table cust1 as select cust_id,cust_name from cust;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
Description of table
mysql> desc cust1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| cust_id | varchar(10) | NO | | NULL | |
| cust_name | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
3.Write a query to create new table from existing table without any record.
mysql> create table cust2 as select * from cust where 1>2;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
Description of table
mysql> desc cust2;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| cust_id | varchar(10) | NO | | NULL | |
| cust_name | varchar(20) | YES | | NULL | |
| cust_addr | varchar(15) | YES | | NULL | |
| card_no | varchar(15) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

4. ALTER THE TABLE BOOKS BY INCREASING THE FIELD WIDTH OF ISBN TO 15.
mysql> desc books;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ISBN | varchar(10) | NO | PRI | NULL | |
| title | varchar(30) | YES | | NULL | |
| author | char(10) | YES | | NULL | |
| Quan_instock | int(11) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pub_year | date | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> Alter table books modify ISBN varchar(15);


Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc books;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ISBN | varchar(15) | NO | PRI | NULL | |
| title | varchar(30) | YES | | NULL | |
| author | char(10) | YES | | NULL | |
| quan_instock | int(11) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pub_year | date | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
5. DROP THE PRIMARY KEY FROM ORDERS TABLE.

mysql> desc orders;


+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| order_no | varchar(10) | NO | PRI | NULL | |
| cust_id | varchar(10) | YES | | NULL | |
| order_date | date | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> alter table orders drop primary key;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc orders;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| order_no | varchar(10) | NO | | NULL | |
| cust_id | varchar(10) | YES | | NULL | |
| order_date | date | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
6. ADD THE PRIMARY KEY TO ORDERS TABLE.
mysql> alter table orders add primary key (order_no);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc orders;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| order_no | varchar(10) | NO | PRI | NULL | |
| cust_id | varchar(10) | YES | | NULL | |
| order_date | date | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

7. ADD NEW COLUMN TO BOOK TABLE.


mysql> desc books;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ISBN | varchar(15) | NO | PRI | NULL | |
| title | varchar(30) | YES | | NULL | |
| author | char(10) | YES | | NULL | |
| quan_instock | int(11) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pub_year | date | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
mysql> alter table books add edition varchar(15);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc books;

+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ISBN | varchar(15) | NO | PRI | NULL | |
| title | varchar(30) | YES | | NULL | |
| author | char(10) | YES | | NULL | |
| quan_instock | int(11) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pub_year | date | YES | | NULL | |
| edition | varchar(15) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
8. DROP THE COLUMN FROM BOOK TABLE.
mysql> alter table books drop edition;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc books;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ISBN | varchar(15) | NO | PRI | NULL | |
| title | varchar(30) | YES | | NULL | |
| author | char(10) | YES | | NULL | |
| quan_instock | int(11) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pub_year | date | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
9. RENAME THE CUSTOMERS TABLE AS CUSTOMERS_1 TABLE.
ALTER
mysql> alter table cust rename to customers_1;
Query OK, 0 rows affected (0.00 sec)
mysql> desc cust;
ERROR 1146 (42S02): Table 'sheryl.cust' doesn't exist
mysql> desc customers_1;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| cust_id | varchar(10) | NO | | NULL | |
| cust_name | varchar(20) | YES | | NULL | |
| cust_addr | varchar(15) | YES | | NULL | |
| card_no | varchar(15) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec

============================================================================
10. Drop customer1 table.
mysql> drop table customer_1;
Query OK, 0 rows affected (0.00 sec)

11. INSERTING RECORDS IN ALL THE FOUR CREATED TABLES:


mysql> insert into books values('1-101','Bold','Raja',150,200,'1990-12-10');
mysql> select * from books;
+-------+-------------+----------+--------------+-------+------------+
| ISBN | title | author | quan_instock | price | pub_year |
+-------+-------------+----------+--------------+-------+------------+
| 1-101 | Bold |Raja | 200 | 120 | 0000-00-00 |
+-------+-------------+----------+--------------+-------+------------+
1 rows in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
mysql> insert into customer values('10','Geetha','Medavakkam','C10');
Query OK, 1 row affected (0.00 sec)
mysql> insert into customer values('11','Raja','Siruseri','C11');
Query OK, 1 row affected (0.00 sec)
mysql> select * from customer;
+---------+-----------+------------+---------+
| cust_id | cust_name | cust_addr | card_no |
+---------+-----------+------------+---------+
| 10 | Geetha | Medavakkam | C10 |
| 11 | Raja | Siruseri | C11 |
+---------+-----------+------------+---------+
2 rows in set (0.00 sec)

mysql> insert into orders values('N21','10','2017-01-12');


Query OK, 1 row affected (0.00 sec)
mysql> insert into orders values('N22','11','2017-02-16');
Query OK, 1 row affected (0.00 sec)
mysql> select * from orders;
+----------+---------+------------+
| order_no | cust_id | order_date |
+----------+---------+------------+
| N21 | 10 | 2017-01-12 |
| N22 | 11 | 2017-02-16 |
+----------+---------+------------+
2 rows in set (0.00 sec)
mysql> insert into order_list values('N21','1-101',100,'2017-01-12');
Query OK, 1 row affected (0.00 sec)
mysql> insert into order_list values('N22','1-102',105,'2017-02-16');
Query OK, 1 row affected (0.00 sec)
mysql> select * from order_list;
+----------+-------+----------+------------+
| order_no | ISBN | quantity | ship_date |
+----------+-------+----------+------------+
| N21 | 1-101 | 100 | 2017-01-12 |
| N22 | 1-102 | 105 | 2017-02-16 |
+----------+-------+----------+------------+
2 rows in set (0.00 sec)
=============================================================================
12. FIND THE NAMES OF ALL AUTHORS IN BOOKS RELATION.
mysql> select author from books;
+----------+
| author |
+----------+
| John |
| John |
| David |
| James |
| Benjamin |
+----------+
5 rows in set (0.00 sec)
13. UPDATE THE CUST_ADDR AS GUINDY FOR CUSTOMER NAME RAJA IN THE
CUSTOMER TABLE.
mysql> select * from customer;
+---------+-----------+------------+---------+
| cust_id | cust_name | cust_addr | card_no |
+---------+-----------+------------+---------+
| 10 | Geetha | Medavakkam | C10 |
| 11 | Raja | Siruseri | C11 |
+---------+-----------+------------+---------+
2 rows in set (0.00 sec)
mysql> update customer set cust_addr='Guindy' where cust_name='Raja';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from customer;
+---------+-----------+------------+---------+
| cust_id | cust_name | cust_addr | card_no |
+---------+-----------+------------+---------+
| 10 | Geetha | Medavakkam | C10 |
| 11 | Raja |Guindy | C11 |
+---------+-----------+------------+---------+
3 rows in set (0.00 sec)
==========================================================================
14. SHOW THE EFFECT OF ROLLBACK COMMAND WITH AN EXAMPLE.
mysql>create table product(id int primary key,category char(12),price
decimal(4,1);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>insert into product values(101,’Mixer’,1051);
mysql>insert into product values(102,’Toaster’,2000);
mysql> select * from product;
+-----+----------+-------+
| id | category | price |
+-----+----------+-------+
| 101 | MIXER | 1051 |
| 102 | Toaster | 2000 |
+-----+----------+-------+
2 rows in set (0.00 sec)

mysql> alter table product type=INNODB;


Query OK, 2 rows affected, 1 warning (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

mysql> start transaction;


mysql> insert into product values(103,'Slicer',1500);
Query OK, 1 row affected (0.00 sec)

mysql> rollback;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from product;


+-----+----------+-------+
| id | category | price |
+-----+----------+-------+
| 101 | MIXER | 1051 |
| 102 | Toaster | 2000 |
+-----+----------+-------+
2 rows in set (0.00 sec)
SAVE POINT
mysql>alter table research engine = INNODB;
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into research values(10,'Barath','Network');
Query OK, 1 row affected (0.00 sec)
mysql> savepoint st1;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into research values(11,'Abisha','DBMS');
Query OK, 1 row affected (0.00 sec)
mysql> savepoint st2;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into research values(12,'Durga','Datamining');
Query OK, 1 row affected (0.00 sec)
mysql> select * from research;
+-------+---------+------------+
| regno | name | res_title |
+-------+---------+------------+
| 10 | Barath | Network |
| 11 | Abisha | DBMS |
| 12 | Durga | Datamining |
+-------+---------+------------+
3 rows in set (0.00 sec)
mysql> rollback to st1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from research;


+-------+---------+------------+
| regno | name | res_title |
+-------+---------+------------+
| 10 | Bharath | Network |
+-------+---------+------------+
3 rows in set (0.00 sec)
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from research;


Empty set (0.00 sec)

RESULT: Thus the definition, manipulation & storing data items in a MySQL database using Structured
Query Language commands was created successfully.
Ex.No.2 Database Querying – Simple queries, Nested queries, Sub queries and Joins

AIM: To execute database querying with Simple queries, Nested queries, Sub queries and Joins

Syntax:
SELECT:
Syntax 1:Select column name1,columnname2 from <table name>;
Syntax 2: select * from <tablename>;
Syntax 3: select * from <tablename> where <condition>;
Select_clause from_clause [where_clause][groupby_clause][having_clause][orderby_clause]
The SELECT clause defines the types of the objects or values returned by the
query.The FROM clause defines the scope of the query by declaring one or more identification
variables, which can be referenced in the SELECTand WHERE clauses. An identification variable
represents one of the following elements:
 The abstract schema name of an entity
 An element of a collection relationship
 An element of a single-valued relationship
 A member of a collection that is the multiple side of a one-to-many relationship
 The WHERE clause is a conditional expression that restricts the objects or values retrieved
by the query. Although the clause is optional, most queries have a WHERE clause.
 The GROUP BY clause groups query results according to a set of properties.
 The HAVING clause is used with the GROUP BY clause to further restrict the query results
according to a conditional expression.
 The ORDER BY clause sorts the objects or values returned by the query into a specified
order.
Subqueries with the SELECT Statement
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
Subqueries with the INSERT Statement
INSERT INTO table_name [ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
Subqueries with the UPDATE Statement
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Subqueries with the DELETE Statement
DELETE FROM TABLE_NAME
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
JOIN COMMANDS
INNER JOIN command returns the matching rows from the tables that are being joined.
LEFT OUTER JOIN command returns matching rows from the tables being joined and also non-
matching row from the left table in the result and places null values in the attributes that come from
the right side table.
RIGHT OUTER JOIN command returns matching rows from the tables being joined and also non-
matching row from the right table in the result and places null values in the attributes that come
from the left side table.
1. FIND THE NAMES OF ALL NAMES IN BOOK RELATION ELIMINATE DUPLICATE.
mysql> select distinct author from books;
+----------+
| author |
+----------+
| David |
| James |
+----------+
3 rows in set (0.00 sec)
=============================================================================
2.DISPLAY IN THE BOOKS TABLE WITH ATTRIBUTE PRICE MULTIPLIED BY 10.
mysql> select price*10 from books;
+----------+
| price*10 |
+----------+
| 1200 |
| 1200 |
| 2000 |
+----------+
3 rows in set (0.00 sec)
============================================================================
3. FIND ALL TITLE FOR BOOKS WHOSE AUTHOR IS JAMES AND PRICE GREATER THAN
100.
mysql> select title from books where author='James'and price>100;
+-------------+
| title |
+-------------+
| Fairy Tales |
+-------------+
1 row in set (0.00 sec)
============================================================================
4. FIND ALL ISBN FOR BOOKS WITH BOOK PRICE BETWEEN 150 AND 200.
mysql> select isbn from books where price between 150 and 200;
+-------+
| isbn |
+-------+
| 1-103 |
| 2-203 |
+-------+
3 rows in set (0.00 sec)
=============================================================================
5. FIND THE NAMES OF CUSTOMER WHOSE NAME INCLUDES THE CHARACTER ‘E’ IN
THE THIRD POSITION.
mysql> select cust_name from customer where cust_name like '__%';
+-----------+
| cust_name |
+-----------+
| Geetha |
+-----------+
1 row in set (0.00 sec)
=============================================================================
6. FIND THE NAMES OF CUSTOMER WHOSE ADDRESS STARTS WITH SUBSTRING ‘E’.
mysql> select cust_name from customer where cust_addr like 'E%';
+-----------+
| cust_name |
+-----------+
| Edena |
+-----------+
1 row in set (0.00 sec)
=============================================================================
7.DISPLAY THE ENTIRE BOOK TABE IN DESCENDING ORDER OF PUB_YEAR.
mysql> select * from books order by pub_year desc;
+-------+-------------+----------+--------------+-------+------------+
| ISBN | title | author | quan_instock | price | pub_year |
+-------+-------------+----------+--------------+-------+------------+
| 1-103 | Beautiful | David | 150 | 200 | 1990-12-10 |
| 2-203 | Fairy Tales | James | 300 | 180 | 1996-04-10 |
+-------+-------------+----------+--------------+-------+------------+
5 rows in set (0.00 sec)
=============================================================================
8. FIND TOTAL NUMBER OF CUSTOMER.
mysql> select count(cust_id) from customer;
+----------------+
| count(cust_id) |
+----------------+
| 2 |
+----------------+
1 row in set (0.00 sec)
=============================================================================
9. FIND ALL THE CUSTOMER NUMBER THAT APPEARS IN THE CUSTOMER RELATION
WITH NULL VALUES FOR CARD_NO.
mysql> insert into customer values('50','Mclen','Tambaram',null);
Query OK, 1 row affected (0.00 sec)
mysql> select cust_name from customer where card_no is null;
+-----------+
| cust_name |
+-----------+
| Mclen |
+-----------+
1 row in set (0.00 sec)
=============================================================================

SUB QUERIES:

SALESMAN TABLE
salesman_id name city commission
----------- ---------- ---------- ----------
5001 James Hoog New York 0.15
5002 Nail Knite Paris 0.13
5005 Pit Alex London 0.11
5006 Mc Lyon Paris 0.14
5003 Lauson Hen San Jose 0.12
5007 Paul Adam Rome 0.13
ORDERS TABLE
ord_no purch_amt ord_date customer_id salesman_id
---------- ---------- ---------- ----------- -----------
70001 150.5 2012-10-05 3005 5002
70009 270.65 2012-09-10 3001 5005
70002 65.26 2012-10-05 3002 5001
70004 110.5 2012-08-17 3009 5003
70007 948.5 2012-09-10 3005 5002
70005 2400.6 2012-07-27 3007 5001
70008 5760 2012-09-10 3002 5001
70010 1983.43 2012-10-10 3004 5006
70003 2480.4 2012-10-10 3009 5003
70012 250.45 2012-06-27 3008 5002
70011 75.29 2012-08-17 3003 5007
70013 3045.6 2012-04-25 3002 5001
CUSTOMER TABLE
customer_id cust_name city grade salesman_id
----------- ------------ ---------- ---------- -----------
3002 Nick Rimando New York 100 5001
3005 Graham Zusi California 200 5002
3001 Brad Guzan London 5005
3004 Fabian Johns Paris 300 5006
3007 Brad Davis New York 200 5001
3009 Geoff Camero Berlin 100 5003
3008 Julian Green London 300 5002
3003 Jozy Altidor Moscow 200 5007

1.Write a query to display all the orders from the orders table issued by the salesman 'paul adam'.

mysql>SELECT * FROM orders WHERE salesman_id = (select salesman_id FROM salesman WHERE
name='Paul Adam');
2.Write a query to display all the orders which values are greater than the average order value for 10th
October 2012.

mysql>SELECT * FROM orders WHERE purch_amt > (select AVG(purch_amt) FROM orders where
ord_date ='10/10/2012');

3. Write a query to find the sums of the amounts from the orders table, grouped by date, eliminating all
those dates where the sum was not at least 1000.00 above the maximum amount for that date.

mysql>SELECT ord_date, SUM (purch_amt) FROM orders a GROUP BY ord_date HAVING SUM
(purch_amt) > (SELECT 1000.00 + MAX(purch_amt) FROM orders b WHERE a.ord_date = b.ord_date);

4. Write a query to extract the data from the customer table if and only if one or more of the customers
in the customer table are located in London.

mysql>SELECT customer_id,cust_name, city FROM customer WHERE EXISTS (SELECT * FROM


customer WHERE city='London');

5.Write a query to find all the salesmen who worked for only one customer.

mysql>ELECT * FROM salesman WHERE salesman_id IN (SELECT DISTINCT salesman_id FROM


customer a WHERE NOT EXISTS (SELECT * FROM customer b WHERE a.salesman_id=b.salesman_id
AND a.cust_name<>b.cust_name));

6.Write a query to find all those customers who hold a different grade than any customer of the city
Dallas.

mysql>SELECT *FROM customer WHERE NOT grade = ANY (SELECT grade FROM customerWHERE
city='Dallas');

Table: EMP
mysql> create table emp(empno int primary key,ename char(10),job
char(5),deptno int,sal int);
Query OK, 0 rows affected (0.00 sec)

mysql> desc emp;


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| empno | int(11) | NO | PRI | NULL | |
| ename | char(10) | YES | | NULL | |
| job | char(5) | YES | | NULL | |
| deptno | int(11) | YES | | NULL | |
| sal | int(11) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)

Table:Depart

mysql> create table depart(deptno int,dname char(12),loc char(12));


Query OK, 0 rows affected (0.01 sec)

mysql> alter table depart add foreign key(deptno) references emp(deptno);


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

mysql> desc depart;


+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| deptno | int(11) | YES | MUL | NULL | |
| dname | char(12) | YES | | NULL | |
| loc | char(12) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

Table:Staff
mysql> create table staff(staff_id int primary key,staff_name char(10),expr
int,age int);
Query OK, 0 rows affected (0.00 sec)
mysql> desc staff;
+------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| staff_id | int(11) | NO | PRI | NULL | |
| staff_name | char(10) | YES | | NULL | |
| expr | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+------------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)
Table:Book
mysql> create table book(book_id int,book_name char(12),edition int,primary
key(book_id),check(edition in(1,2,3)));
Query OK, 0 rows affected (0.00 sec)
mysql> desc book;
+-----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+-------+
| book_id | int(11) | NO | PRI | 0 | |
| book_name | char(12) | YES | | NULL | |
| edition | int(11) | YES | | NULL | |
+-----------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
Table:Issue
mysql> create table issue(staff_id int,book_id int,day date,primary
key(staff_id,book_id));
Query OK, 0 rows affected (0.01 sec)
mysql> alter table issue add foreign key(book_id) references book(book_id);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table issue add foreign key(staff_id) references
staff(staff_id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc issue;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| staff_id | int(11) | NO | PRI | 0 | |
| book_id | int(11) | NO | PRI | 0 | |
| day | date | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)

INSERTING RECORDS IN ALL THE CREATED TABLES:


mysql> insert into emp values(1,'Arjun','AP',1,10000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values(2,'Barath','ASP',2,20000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values(3,'Aparna','ASP',2,20000);
Query OK, 1 row affected (0.00 sec)
mysql> insert into emp values(4,'Karthik','AP',1,10000);
Query OK, 1 row affected (0.00 sec)
mysql> select * from emp;
+-------+---------+------+--------+-------+
| empno | ename | job | deptno | sal |
+-------+---------+------+--------+-------+
| 1 | Arjun | AP | 1 | 10000 |
| 2 | Barath | ASP | 2 | 20000 |
| 3 | Aparna | ASP | 2 | 20000 |
| 4 | Karthik | AP | 1 | 10000 |
+-------+---------+------+--------+-------+
4 rows in set (0.00 sec)
----------------------------------------------------------------------------
mysql> create table depart(deptno int,dname char(12),loc char(12));
Query OK, 0 rows affected (0.01 sec)
mysql> alter table depart add foreign key(deptno) references emp(deptno);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into depart values(1,'Accounting','Newyork');
Query OK, 1 row affected (0.00 sec)
mysql> insert into depart values(2,'Research','Dallas');
Query OK, 1 row affected (0.00 sec)
mysql> select * from depart;
+--------+------------+---------+
| deptno | dname | loc |
+--------+------------+---------+
| 1 | Accounting | Newyork |
| 2 | Research | Dallas |
| 30 | Sales | Chicago |
| 40 | Operation | Boston |
+--------+------------+---------+
4 rows in set (0.00 sec)
mysql> insert into staff values(11,'John',8,27);
Query OK, 1 row affected (0.00 sec)
mysql> insert into staff values(12,'Priya',9,29);
Query OK, 1 row affected (0.00 sec)
mysql> select * from staff;
+----------+------------+------+------+
| staff_id | staff_name | expr | age |
+----------+------------+------+------+
| 11 | John | 8 | 27 |
| 12 | Priya | 9 | 29 |
| 13 | Beulah | 10 | 36 |
+----------+------------+------+------+
3 rows in set (0.00 sec)

mysql> insert into book values(1023,'OOPS',3);


Query OK, 1 row affected (0.00 sec)
mysql> insert into book values(1056,'DBMS',2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from book;
+---------+-----------+---------+
| book_id | book_name | edition |
+---------+-----------+---------+
| 1023 | OOPS | 3 |
| 1056 | DBMS | 2 |
| 1036 | CN | 1 |
+---------+-----------+---------+
3 rows in set (0.00 sec)
----------------------------------------------------------------------------
mysql> insert into issue values(11,1023,'11-aug-14');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> insert into issue values(12,1056,'15-dec-14');
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> select * from issue;
+----------+---------+------------+
| staff_id | book_id | day |
+----------+---------+------------+
| 11 | 1023 | 0000-00-00 |
| 12 | 1056 | 0000-00-00 |
+----------+---------+------------+
2 rows in set (0.00 sec)

JOIN COMMANDS
-------------------------

EQUI-JOIN
Display the employee details, departments that the departments are same in both the emp and dept.
mysql> select * from emp,depart where emp.deptno=depart.deptno;
+-------+---------+------+--------+-------+--------+------------+---------+
| empno | ename | job | deptno | sal | deptno | dname | loc |
+-------+---------+------+--------+-------+--------+------------+---------+
| 1 | Arjun | AP | 1 | 10000 | 1 | Accounting | Newyork |
| 4 | Karthik | AP | 1 | 10000 | 1 | Accounting | Newyork |
| 2 | Barath | ASP | 2 | 20000 | 2 | Research | Dallas |
| 3 | Aparna | ASP | 2 | 20000 | 2 | Research | Dallas |
+-------+---------+------+--------+-------+--------+------------+---------+
4 rows in set (0.00 sec)

NON EQUI-JOIN
Display the employee details, departments that the departments are not same in both the emp and
dept.
mysql> select * from emp,depart where emp.deptno!=depart.deptno;
+-------+---------+------+--------+-------+--------+------------+---------+
| empno | ename | job | deptno | sal | deptno | dname | loc |
+-------+---------+------+--------+-------+--------+------------+---------+
| 2 | Barath | ASP | 2 | 20000 | 1 | Accounting | Newyork |
| 1 | Arjun | AP | 1 | 10000 | 2 | Research | Dallas |
| 4 | Karthik | AP | 1 | 10000 | 2 | Research | Dallas |
+-------+---------+------+--------+-------+--------+------------+---------+
12 rows in set (0.00 sec)

LEFT OUTER JOIN


Display Ename and location using LEFT OUTER JOIN
mysql> select emp.ename,loc from emp left outer join depart on
emp.deptno=depart.deptno;

+---------+---------+
| ename | loc |
+---------+---------+
| Arjun | Newyork |
| Barath | Dallas |
| Aparna | Dallas |
| Karthik | Newyork |
+---------+---------+
4 rows in set (0.00 sec)
RIGHT OUTER JOIN
Write a Query to display Ename,Job and Sal using Right outer Join
mysql> select emp.ename,job,sal from emp right outer join depart on
depart.deptno=emp.deptno;
+---------+------+-------+
| ename | job | sal |
+---------+------+-------+
| Arjun | AP | 10000 |
| Barath | ASP | 20000 |
| Aparna | ASP | 20000 |
| NULL | NULL | NULL |
| NULL | NULL | NULL |
+---------+------+-------+
6 rows in set (0.00 sec)
SELF JOIN
Write a Query to display employee names using Self Join
mysql> select distinct ename from emp x,depart y where x.deptno=y.deptno;
+---------+
| ename |
+---------+
| Arjun |
| Barath |
| Aparna |
+---------+
4 rows in set (0.00 sec)
SUB-QUERY (query within another query)
Update deptno by adding empno and keep that as deptno for employee 4.
mysql> update emp set deptno=( select sum(empno)from depart) where empno=4;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from emp;
+-------+---------+------+--------+-------+
| empno | ename | job | deptno | sal |
+-------+---------+------+--------+-------+
| 1 | Arjun | AP | 1 | 10000 |
| 2 | Barath | ASP | 2 | 20000 |
| 3 | Aparna | ASP | 2 | 20000 |
+-------+---------+------+--------+-------+
4 rows in set (0.00 sec)

Display staff name who has the bookid 1023.


mysql> select staff_name from staff where staff_id in(select staff_id from
issue where book_id=1023);

+------------+
| staff_name |
+------------+
| John |
+------------+
1 row in set (0.00 sec)
=============================================================================
Display staff_id whose edition is 2.
mysql> select staff_id from issue where book_id in(select book_id from book
where edition=2);
+----------+
| staff_id |
+----------+
| 12 |
+----------+
1 row in set (0.00 sec)
=============================================================================
Display edition of the book used by staff Chris.
mysql> select edition from book where book_id in(select book_id from issue
where staff_id in(select staff_id from staff where staff_name='chris'));
+---------+
| edition |
+---------+
| 3 |
+---------+
1 row in set (0.00 sec)
=============================================================================
RESULT:Thus Simple queries, Nested queries, Sub queries and Joins in a MySQL database using Structured
Query Language commands was created successfully.

You might also like