Assignment 2
Assignment 2
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:
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
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:
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;
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
+ + + + +
| 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)
+ + +
| 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)
+ + + + +
+ + +
| 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 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)