Professional Documents
Culture Documents
SYNTAX:
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)
+--------------+-------------+------+-----+---------+-------+
| 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)
mysql> rollback;
Query OK, 0 rows affected (0.01 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.
5.Write a query to find all the salesmen who worked for only one customer.
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)
Table:Depart
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)
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)
+---------+---------+
| 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)
+------------+
| 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.