0% found this document useful (0 votes)
19 views

Assignment 2

Uploaded by

harshp.aidsioe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Assignment 2

Uploaded by

harshp.aidsioe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Group A

Assignment No: 2

Aim: Design at least 10 SQL queries for suitable database application using SQL DML
statements: all types of Join, Sub-Query and View.

Objective:
1. To learn and understand DML statements in MySQL
2. To learn SQL Joins, Subqueries & Views.

Hardware requirements:
Any CPU with Pentium Processor or similar, 256 MB
RAM or more, 1 GB Hard Disk or more.
Software requirements:
Ubuntu 14 Operating System, MySQL
Theory:

SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
Let's look at a selection from the "Orders" table:

OrderID CustomerID OrderDate

10308 2 1996-09-18

10309 37 1996-09-19

10310 77 1996-09-20

Then, look at a selection from the "Customers" table:

CustomerID CustomerName ContactName Country

1 Alfreds Futterkiste Maria Anders Germany

2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico

3 Antonio Moreno Taquería Antonio Moreno Mexico

Note that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers"
table. The relationship between the two tables above is the "CustomerID" column.Then, we can create the
following SQL statement (that contains an INNER JOIN), that selects records that have matching values in
both tables:
Example:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
and it will produce something like this:

OrderID CustomerName OrderDate

10308 Ana Trujillo Emparedados y helados 9/18/1996

10365 Antonio Moreno Taquería 11/27/1996

10383 Around the Horn 12/16/1996

10355 Around the Horn 11/15/1996

10278 Berglunds snabbköp 8/12/1996

Different Types of SQL JOINs:


Here are the different types of the JOINs in SQL:
 (INNER) JOIN: Returns records that have matching values in both tables
 LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the
right table
 RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from
the left table
 FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
SQL Views:
SQL CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains rows and
columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the data as if the data
were coming from one single table.
CREATE VIEW Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

SQL CREATE VIEW Examples:

The view "Product List" lists all active products (products that are not discontinued) from the "Products"
table. The view is created with the following SQL:
CREATE VIEW ProductList AS
SELECT ProductID, ProductName
FROM Products
WHERE Discontinued = No;
Then, we can query the view as follows:
SELECT * FROM ProductList;

SQL Updating a View:


You can update a view by using the following syntax:
SQL CREATE OR REPLACE VIEW Syntax
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Now we want to add the "Category" column to the "Product List" view. We will update the view with the
following SQL:

CREATE OR REPLACE VIEW Product List AS


SELECT ProductID, ProductName, Category
FROM Products
WHERE Discontinued = No;

Subqueries:
A subquery is a SQL query nested inside a larger query.
 A subquery may occur in :
 - A SELECT clause
 - A FROM clause
 - A WHERE clause
 In MySQL subquery can be nested inside a SELECT, INSERT, UPDATE, DELETE, SET, or DO
statement or inside another subquery.
 A subquery is usually added within the WHERE Clause of another SQL SELECT statement.
 You can use the comparison operators, such as >, <, or =. The comparison operator can also be a
multiple-row operator, such as IN, ANY, SOME, or ALL.
 A subquery can be treated as an inner query, which is a SQL query placed as a part of another
query called as outer query.
 The inner query executes first before its parent query so that the results of the inner query can be
passed to the outer query.
Subquery Syntax :

 The subquery (inner query) executes once before the main query (outer query) executes.
 The main query (outer query) use the subquery result.
Subquery syntax as specified by the SQL standard and supported in MySQL
DELETE FROM t1
WHERE s11 > ANY
(SELECT COUNT(*) /* no hint */ FROM t2
WHERE NOT EXISTS
(SELECT * FROM t3
WHERE ROW(5*t2.s1,77)=
(SELECT 50,11*s1 FROM t4 UNION SELECT 50,77 FROM
(SELECT * FROM t5) AS t5)));
A subquery can return a scalar (a single value), a single row, a single column, or a table (one or more rows
of one or more columns). These are called scalar, column, row, and table subqueries.
Subqueries: Guidelines
There are some guidelines to consider when using subqueries :
- A subquery must be enclosed in parentheses.
- Use single-row operators with single-row subqueries, and use multiple-row operators with multiple-row
subqueries.
- If a subquery (inner query) returns a null value to the outer query, the outer query will not return any
rows when using certain comparison operators in a WHERE clause.
Types of Subqueries
 The Subquery as Scalar Operand
 Comparisons using Subqueries
 Subqueries with ALL, ANY, IN, or SOME
 Row Subqueries
 Subqueries with EXISTS or NOT EXISTS
 Correlated Subqueries
 Subqueries in the FROM Clause
Conclusion:
Thus,we have successfully studied Joins, Subqueries and views and implemented SQL Queries.
Group A: Lab Assignment No. 2

Title : Design at least 10 SQL queries for suitable database application


using SQL DML statements: all types of Join, Sub-Query and View

mysql> show databases;


+ +
| Database
|
+ +
| information_schema |
|A|
| Abhi |
| COMPUTER |
|H|
| PVG |
| RENUKA |
| mysql |
| nishant |
| nishantl |
| performance_schema |
| renuka |
| sys |
| time |
+ +
14 rows in set (0.21 sec)

mysql> use Abhi;

Reading table information for completion of table and column names


You can turn off this feature to get a quicker startup with -ADatabase changed

mysql> show tables;


+ +
| Tables_in_Abhi |
+ +
| Employee |
| TE |
| auto |
| c_master |
| product_master |
+ +
5 rows in set (0.00 sec)

mysql> create table _master(product_no int,description varchar(20),profit_per


float,unit_measure varchar(10),quantity int,reorder int,sell_price
float,cost_price float,primary key(product_no));

Query OK, 0 rows affected (0.55 sec)

mysql> create table customer(cust_no int,cust_name varchar(20),cust_add


varchar(20),phone_no int,primary key(cust_no));

Query OK, 0 rows affected (0.28 sec)

mysql> create table capital(cap_no int,cap_name varchar(20),state_no


int,primary key(cap_no));

Query OK, 0 rows affected (0.27 sec)

mysql> create table state(state_no int,state_name varchar(20),state_code


int,capital varchar(20),primary key(state_no));

Query OK, 0 rows affected (0.28 sec)

mysql> insert into capital values('01','MH','01');

Query OK, 1 row affected (0.12 sec)

mysql> insert into capital values('02','RAJ','02');

Query OK, 1 row affected (0.04 sec)

mysql> insert into capital values('03','GOA','03');

Query OK, 1 row affected (0.05 sec)

mysql> insert into capital values('04','GUJ','04');

Query OK, 1 row affected (0.05 sec)


mysql> insert into capital values('05','KAR','05');

Query OK, 1 row affected (0.04 sec)

mysql> insert into state values('01','MH','01','MUM');

Query OK, 1 row affected (0.03 sec)

mysql> insert into state values('02','RAJ','02','JAI');

Query OK, 1 row affected (0.03 sec)

mysql> insert into state values('03','GOA','03','PAN');

Query OK, 1 row affected (0.04 sec)

mysql> insert into state values('04','GUJ','04','SUR');

Query OK, 1 row affected (0.04 sec)

mysql> insert into state values('05','KAR','05','BAN');

Query OK, 1 row affected (0.03 sec)

mysql> select * from capital;


+ + + +
| cap_no | cap_name | state_no |
+ + + +| 1 | MH | 1 |
| 2 | RAJ | 2 |
| 3 | GOA | 3 |
| 4 | GUJ | 4 |
| 5 | KAR | 5 |
+ + + +
5 rows in set (0.01 sec)

mysql> select * from state;

+ + + + +
| state_no | state_name | state_code | capital |
+ + + + +
| 1 | MH | 1 | MUM |
| 2 | RAJ | 2 | JAI |
| 3 | GOA | 3 | PAN |
| 4 | GUJ | 4 | SUR |
| 5 | KAR | 5 | BAN |
+ + + + +
5 rows in set (0.00 sec)

mysql> select capital.cap_no, state.state_no from capital inner join state on


capital.cap_no=state.state_no;
+ + +
| cap_no | state_no |
+ + +
|1|1|
|2|2|
|3|3|
|4|4|
|5|5|
+ + +
5 rows in set (0.06 sec)

mysql> UPDATE state SET state_no="78" where state_no='1';

Query OK, 1 row affected (0.04 sec)


Rows matched: 1
Changed: 1
Warnings: 0

mysql> UPDATE state SET state_no="58" where state_no='2';

Query OK, 1 row affected (0.04 sec)


Rows matched: 1
Changed: 1
Warnings: 0

mysql> UPDATE state SET state_no="46" where state_no='3';

Query OK, 1 row affected (0.03 sec)


Rows matched: 1
Changed: 1
Warnings: 0

mysql> UPDATE state SET state_no="489" where state_no='4';

Query OK, 1 row affected (0.05 sec)


Rows matched: 1
Changed: 1
Warnings: 0

mysql> UPDATE state SET state_no="458" where state_no='5';


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

mysql> insert into state values('05','MP','05','BHO');

Query OK, 1 row affected (0.03 sec)

mysql> select capital.cap_no, state.state_no from capital inner join state on


capital.cap_no=state.state_no;
+ + +
| cap_no | state_no |
+ + +
|
5|
5|
+ + +
1 row in set (0.00 sec)
mysql> select capital.cap_no, state.state_no from capital left join state on
capital.cap_no=state.state_no;
+ + +
| cap_no | state_no |
+ + +
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
|5|5|
+ + +
5 rows in set (0.00 sec)
mysql> select capital.cap_no, state.state_no from capital left join state on
capital.cap_no=state.state_name;
+ + +
| cap_no | state_no |
+ + +
| 1 | NULL |
| 2 | NULL |
| 3 | NULL |
| 4 | NULL |
| 5 | NULL |
+ + +
5 rows in set, 20 warnings (0.00 sec)
mysql> select capital.cap_no, state.state_no from capital right join state on
capital.cap_no=state.state_no;

+ + +
| cap_no | state_no |
+ + +
|
5|
5 || NULL | 46 |
| NULL | 58 |
| NULL | 78 |
| NULL | 458 |
| NULL | 489 |
+ + +
6 rows in set (0.00 sec)
mysql> select * from capital;
+ + + +
| cap_no | cap_name | state_no |
+ + + +
| 1 | MH | 1 |
| 2 | RAJ | 2 |
| 3 | GOA | 3 |
| 4 | GUJ | 4 |
| 5 | KAR | 5 |
+ + + +
5 rows in set (0.00 sec)
mysql> select * from state;
+ + + + +
| state_no | state_name | state_code | capital |
+ + + + +
| 5 | MP | 5 | BHO |
| 46 | GOA | 3 | PAN |
| 58 | RAJ | 2 | JAI |
| 78 | MH | 1 | MUM |
| 458 | KAR | 5 | BAN |
| 489 | GUJ | 4 | SUR |
+ + + + +
6 rows in set (0.00 sec)
mysql> select * from capital;
+ + + +
| cap_no | cap_name | state_no |
+ + + +
| 1 | MH | 1 |
| 2 | RAJ | 2 |
| 3 | GOA | 3 |
| 4 | GUJ | 4 |
| 5 | KAR | 5 |
+ + + +
5 rows in set (0.00 sec)
mysql> select capital.cap_no, state.state_no from capital inner join state on
capital.cap_no=state.state_no;
+ + +
| cap_no | state_no |
+ + +
|
5|
5|
+ + +
1 row in set (0.00 sec)

mysql> select capital.cap_no,capital.cap_name,state.capital,state.state_no from


capital inner join state on capital.cap_no=state.state_no;
+ + + + +
| cap_no | cap_name | capital | state_no |
+ + + + +
|
5 | KAR
| BHO
|
5|
+ + + + +
1 row in set (0.00 sec)
mysql> select capital.cap_no,capital.cap_name,state.capital,state.state_no
from capital left join state on capital.cap_no=state.state_no;
+ + + + +
| cap_no | cap_name | capital | state_no |
+ + + + +
| 1 | MH | NULL | NULL |
| 2 | RAJ | NULL | NULL |
| 3 | GOA | NULL | NULL |
| 4 | GUJ | NULL | NULL |
| 5 | KAR | BHO | 5 |
+ + + + +
5 rows in set (0.00 sec)
mysql> select capital.cap_no,capital.cap_name,state.capital,state.state_no
from capital right join state on capital.cap_no=state.state_no;
+ + + + +
| cap_no | cap_name | capital | state_no |
+ + + + +
|
5 | KAR
| BHO | 5 |
| NULL | NULL | PAN | 46 |
| NULL | NULL | JAI | 58 |
| NULL | NULL | MUM | 78 |
| NULL | NULL | BAN | 458 |
| NULL | NULL | SUR | 489 |
+ + + + +
6 rows in set (0.00 sec)

mysql> select capital.cap_no,capital.cap_name,state.capital,state.state_no from


capital left join state on capital.cap_no=state.state_no union
selectcapital.cap_no,capital.cap_name,state.capital,state.state_no from capital
right join state on capital.cap_no=state.state_no;
+ + + + +
| cap_no | cap_name | capital | state_no |
+ + + + +
| 1 | MH | NULL | NULL |
| 2 | RAJ | NULL | NULL |
| 3 | GOA | NULL | NULL |
| 4 | GUJ | NULL | NULL |
| 5 | KAR | BHO | 5 |
| NULL | NULL | PAN | 46 |
| NULL | NULL | JAI | 58 |
| NULL | NULL | MUM | 78 |
| NULL | NULL | BAN | 458 |
| NULL | NULL | SUR | 489 |
+ + + + +
10 rows in set (0.00 sec)

mysql> select * from capital c1, state s1 where c1.cap_no=s1.state_no;

+ + + + +
+ + +
| cap_no | cap_name | state_no | state_no | state_name |
state_code | capital |
+ + + + +
+ + +
|
5 | KAR
5 | BHO
|
|
5|
5 | MP
|
+ + + + +
+ + +
1 row in set (0.00 sec)

mysql> select * from capital c1, state s1 where c1.cap_no! =s1.state_no;


+ + + + +
+ + +
| cap_no | cap_name | state_no | state_no | state_name |
state_code | capital |
+ + + + +
+ + +
|
1 | MH
5 | BHO
| | 1 | 5 | MP |
|
2 | RAJ
5 | BHO
| | 2 | 5 | MP |
|
3 | GOA
5 | BHO
| | 3 | 5 | MP |
|
4 | GUJ
5 | BHO
| | 4 | 5 | MP |
|
1 | MH
3 | PAN
| | 1 | 46 | GOA |
|
2 | RAJ
3 | PAN
| | 2 | 46 | GOA |
|
3 | GOA
3 | PAN
| | 3 | 46 | GOA |
|
4 | GUJ
3 | PAN
| | 4 | 46 | GOA |
|
5 | KAR
3 | PAN
| | 5 | 46 | GOA |
|
1 | MH
2 | JAI
| | 1 | 58 | RAJ |
|
2 | RAJ
2 | JAI
| | 2 | 58 | RAJ |
|
3 | GOA
2 | JAI
| | 3 | 58 | RAJ |
|
4 | GUJ
2 | JAI
| | 4 | 58 | RAJ |
|
5 | KAR
2 | JAI
| | 5 | 58 | RAJ |
|
1 | MH
1 | MUM
| | 1 | 78 | MH |
|
2 | RAJ
1 | MUM
| | 2 | 78 | MH ||
3 | GOA
1 | MUM
| | 3 | 78 | MH |
|
4 | GUJ
1 | MUM
| | 4 | 78 | MH |
|
5 | KAR
1 | MUM
| | 5 | 78 | MH |
|
1 | MH
5 | BAN
| | 1 | 458 | KAR |
|
2 | RAJ
5 | BAN
| | 2 | 458 | KAR |
|
3 | GOA
5 | BAN
| | 3 | 458 | KAR |
|
4 | GUJ
5 | BAN
| | 4 | 458 | KAR |
|
5 | KAR
5 | BAN
| | 5 | 458 | KAR |
|
1 | MH
4 | SUR
| | 1 | 489 | GUJ |
|
2 | RAJ
4 | SUR
| | 2 | 489 | GUJ |
|
3 | GOA
4 | SUR
| | 3 | 489 | GUJ |
|
4 | GUJ
4 | SUR
| | 4 | 489 | GUJ |
|
5 | KAR
4 | SUR
| | 5 | 489 | GUJ |
+ + + + +
+ + +
29 rows in set (0.00 sec)

mysql> select * from state where state_no=(select state_no from state where
state_name='MH');
+ + + + +
| state_no | state_name | state_code | capital |
+ + + + +
|
78 | MH
|
1 | MUM
|
+ + + + +
1 row in set (0.06 sec)

mysql> select * from state where state_no=(select state_no from state where
state_name='GUJ');
+ + + + +
| state_no | state_name | state_code | capital |
+ + + + +
|
489 | GUJ
|
4 | SUR
|
+ + + + +
1 row in set (0.00 sec)

mysql> select * from state where state_no=(select capital.state_no from capital


where cap_name='MH');

Empty set (0.00 sec)

mysql> select * from state where state_no=(select capital.state_no from capital


where cap_name='GUJ');

Empty set (0.00 sec)

mysql> select * from state where state_no=(select capital.state_no from capital


where cap_name='RAJ');

Empty set (0.00 sec)

mysql> select * from state where state_no=(select capital.state_no from capital


where cap_name='KAR');
+ + + + +
| state_no | state_name | state_code | capital |
+ + + + +
|
5 | MP
|
5 | BHO
|
+ + + + +
1 row in set (0.00 sec)

You might also like