Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Views
Aggregate functions
Joins
SQL - Aggregate
functions
Aggregate function
- MIN
Returns the smallest value that
occurs in the specified column
Column need not be numeric type
List the minimum unit price from the
item table
SELECT MIN (UnitPrice)
FROM Item;
Aggregate function
- MAX
Returns the largest value that occurs
in the specified column
Column need not be numeric type
List the Maximum unit price from the
item table
SELECT MAX (Total_Available_Balance_in_Dollars)
FROM Customer_Transaction;
Aggregate function
- AVG
Returns the average of all the values in
the
specified column
Column must be numeric data type
Aggregate function
- SUM
Adds up the values in the specified column
Column must be numeric data type
Value of the sum must be within the range
of that
data type
Count(*)
=
Count(ColumnName)
Count(*)
= No of rows, regardless of NULLs
Count(ColumnName)
= No. of rows that do not have
NULL Value
SQL Group By
ItemNam
e
UnitPrice
Pen
30
Bread
20
Poteto
10
Mobile
5000
iPod
600
Diary
200
Class
A
A
A
C
B
B
Class
AVG(UnitPrice)
A
B
C
20
400
5000
Supplier
Id
S1
S4
S3
S2
S2
S2
SQL Group By
SELECT Class, SupplierId, COUNT(ItemId)
FROM Item GROUP BY Class, SupplierId;
Item
ItemId
STN001
BAK003
GRO001
ELC001
ELC004
STN002
ItemNam
Supplier
e
UnitPrice
Class
Id
Pen
30
A
S1
Bread
20
A
S4
Poteto
10
A
S3
Mobile
5000
C
S2
iPod
600
B
S2
Diary
200
B
S2
Class
SupplierId Count(ItemId)
A
S1
1
A
S3
1
A
S4
1
B
S2
2
C
S2
1
AVG(UnitPri
ce)
20
400
5000
After
Having
Class
A
A
A
C
B
B
SupplierI
d
S1
S4
S3
S2
S2
S2
Class
B
C
AVG(UnitPr
ice)
400
5000
JOINS
Cartesian Product
Self join
Inner join
Equi join
Outer join
Left-outer join
Right-outer join
JOINS
JOIN
INNER
THET
EQUI
A
OUTER
SELF
NATUR
AL
LEFT
RIGH
T
FUL
L
FROM Table1,Table2;
Inner Joins
Get all combinations of Emp and Cust information such that the
Emp and Cust are co-located.
SELECT Table1.Emp_ID, Table1.City, Table2.Cust_ID,
Table2.City
FROM Table1, Table2
WHERE Table1.City = Table2.City;
SELECT s.SupplierId,s.SupplierName
FROM Supplier s, ItemOrder io
WHERE s.SupplierId = io.SupplierId AND
io.DeleviryStatus=Not Delivered;
Display the details of Supplier who has delivered more than one item
SELECT s.SupplierId,s.SupplierName
FROM Supplier s, ItemOrder io
WHERE s.SupplierId = io.SupplierId
Group By s.SupplierId,s.SupplierName
having count(*)>1;
Display the name of the item, name of the supplier, and quantity supplied
if the supplier has supplied that item.
SELECT i.ItemId,s.SupplierId,io.QtyOfOrder
FROM Item i, Supplier s, ItemOrder io
WHERE s.SupplierId = io.SupplierId
AND i.ItemId=io.ItemId;
Display the id and name of the customer and the total amount of
purchasing done so far from the retail shop.
SELECT c.CustomerId,c.CustomerName,Sum(cp.NetPrice)
FROM Customer c, CustomerPurchase cp
WHERE c.CustomerId=cp.CustomerId
Group by
c.CustomerId,c.CustomerName;
Display the id and name of the customer and the total amount of
purchasing done along with bill id.
Note: A customer may have paid several bills
SELECT
c.CustomerId,c.CustomerName,cp.BillId,Sum(cp.NetPri
ce)
FROM Customer c, CustomerPurchase cp
WHERE c.CustomerId=cp.CustomerId
Group by c.CustomerId, c.CustomerName, cp.BillId;
Outer join
Example of left-join
List all cities of Table1 if there is match in cities in Table2 & also
unmatched Cities from Table1
SELECT Table1.Emp_ID, Table1.City, Table2.Cust_ID, Table2.City
FROM Table1, Table2
WHERE Table1.City = Table2.City (+);
SELECT
Customer_details.Cust_id,Cust_Last_name,Loan_no,
Amount_in_dollars
FROM Customer_Account_details,Customer_loan
WHERE
Customer_Account_details.Cust_id =
Customer_loan.Cust_id (+);
Summary
The need of join operations between tables in
the database.
The different types of JOIN operations.
Applying Self Join, Inner join, and Outer Join.
What is a view?
A view is a kind of
virtual table
Create a view
Types of views
Horizontal views
Vertical views
Row/column subset views
Grouped views
Joined views
Horizontal views
Horizontal view restricts a users access
to only selected rows of a table.
Vertical views
A view which selects only few columns of a
table:
Vertical view restricts a users access to
only certain columns of a table.
Updating a VIEW
CREATE VIEW View_Cust
AS SELECT *
FROM Customer_Details
WHERE CUST_ID IN (101,102,103);
--Insert Statement
INSERT INTO view_cust
VALUES (103,'Langer','G.','Justin',3421,'Savings', Global Commerce
Bank', 'Langer_Justin@Yahoo.com');
--Delete Statement
DELETE view_cust WHERE cust_id = 103;
--Update Statament
UPDATE view_cust
SET Cust_last_name = 'Smyth'
WHERE cust_id = 101;
Updating View
A view can be updated if the query that defines the view
meets all of these restrictions:
Dropping Views
Views are dropped similar to the way in which the tables are
dropped. However, you must own a view in order to drop it.
DROP VIEW
<view name>;
Solution is :
Advantages of
views
Security
Query simplicity
Structural simplicity
Disadvantages of views
Performance
Restrictions