Chapter 8 SQL Complex Queries
Chapter 8 SQL Complex Queries
• Normalization
– 1NF
– 2NF
– 3NF
• Merits and Demerits of Normalization
• Introduction to SQL
• DDL Statements
• Relational Operators
• DML Statements
• Group By and Having clause
• Joins
2
Session Plan
• Independent Subqueries
• Correlated Query
• Exists and Not Exists
• Views
• DCL Statements
• Grant
• Revoke
3
Independent subqueries
5
Retrieval using SUB QUERIES
To list the Cust_ID and Loan_No for all Customers who have taken a loan of
amount greater than the loan amount of Customer (Cust_ID = 104).
Customer Data
6
Sub Query (Contd…)
7
Retrieval using SUB QUERIES
List customer names of all customers who have taken a loan > $3000.00.
Customer Data
8
Retrieval using SUB QUERIES
List customer names of all customers who have the same Account_type as
Customer ‘Jones Simon’ .
Customer Data
9
Retrieval using SUB QUERIES
List customer names of all customers who do not have a Fixed Deposit.
Customer Data
10
Retrieval using SUB QUERIES
List customer names of all customers who have either a Fixed Deposit or a
loan but not both at any of Bank Branches. The list includes customers
who have no fixed deposit and loan at any of the bank branches.
SELECT Cust_Last_Name, Cust_Mid_Name, Cust_First_Name
FROM Customer_Details
WHERE Cust_ID
NOT IN
( SELECT Cust_ID
FROM Customer_Loan
WHERE Cust_ID
IN
(SELECT Cust_ID
FROM Customer_Fixed_Deposit ));
11
Correlated subqueries
• You can refer to the table in the FROM clause of the outer query in
the inner query using Correlated sub-queries.
• The inner query is executed separately for each row of the outer
query.
(i.e. In Co-Related Sub-queries, SQL performs a sub-query over
and over again – once for each row of the main query. )
13
Correlated Sub Queries
To list all Customers IDs who have a fixed deposit of amount less than the
sum of all their loans.
SELECT Cust_Id
FROM Customer_fixed_Deposit
WHERE amount_in_dollars
<
(SELECT sum(amount_in_dollars)
FROM Customer_Loan
WHERE Customer_Loan.Cust_Id =
Customer_Fixed_Deposit.Cust_ID);
14
Correlated Sub Queries
List customer details of all customers who have both a Fixed Deposit and a
loan at any of Bank Branches
SELECT Cust_ID,Cust_Last_Name
FROM Customer_Account_Details
WHERE Cust_ID
IN
(SELECT Cust_ID
FROM Customer_Loan
WHERE Customer_Loan.Cust_ID =
Customer_Account_Details.Cust_ID)
AND Cust_ID IN
(SELECT Cust_ID
FROM Customer_Fixed_Deposit
WHERE Customer_Fixed_Deposit.Cust_ID=Customer_
15
Account_Details.Cust_ID);
EXISTS Vs NOT EXISTS
List all Customers who have at least one Fixed Deposit more than $3000.00.
17
Retrieval using EXISTS
List all Cust_ID who have both a Fixed Deposit and a Loan at the Bank
SELECT Cust_ID
FROM Customer_Fixed_Deposit
WHERE EXISTS
(SELECT *
FROM Customer_Loan
WHERE Customer_Loan.Cust_ID =
Customer_Fixed_Deposit.Cust_ID);
18
Retrieval using NOT EXISTS
List all Customers who don’t have a single Fixed Deposit over $3000.00.
19
Views
21
Create a view
22
Assigning names to columns
23
A view can be modified by the DML command.
Updating a VIEW
--Delete Statement
DELETE view_cust WHERE cust_id = 103;
--Update Statament
UPDATE view_cust
SET Cust_last_name = 'Smyth'
WHERE cust_id = 101;
Demos\Views.doc 24
Updating View
A view can be updated if the query that defines the view meets all of
these restrictions:
• DISTINCT must not be specified; that is, duplicate rows must not be
eliminated from the query results
• The FROM clause must specify only one updateable table; the view
must have a single underlying source table
• The WHERE clause must not include a sub query; only simple row-by-
row search conditions may appear
25
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.
26
Checking View Updates :– Check Option
FROM Customer_Details
27
Checking View Updates :– Check Option
FROM view_customer;
Solution is :
FROM Customer_Details
28
Advantages of views
• Security
• Query simplicity
• Structural simplicity
29
Disadvantages of views
• Performance
• Restrictions
30
Data Control Language (DCL)
GRANT {
[ALTER[, ]]
[DELETE[, ]]
[INDEX[, ]]
[INSERT[, ]]
[SELECT[, ]]
[UPDATE [(column-name[,...])][, ]]
| ALL [PRIVILEGES]] }
ON [TABLE] {table-name[,...] | view-
name[,...]}
TO [AuthID][,...]
[WITH GRANT OPTION]
32
GRANT
GRANT ALL
ON Customer_Loan
TO PUBLIC ;
33
Grant
GRANT SELECT
EDWIN 1. WITH GRANT OPTION
ON Customer_Loan
TO EDWIN
WITH GRANT OPTION;
2. GRANT
JACK
BORIS
34
Taking PRIVILIGES away
REVOKE{
[ALTER[, ]]
[DELETE[, ]]
[INDEX[, ]]
[INSERT[, ]]
[SELECT[, ]]
[UPDATE [(column-name[,...])][, ]]
| ALL [PRIVILEGES] }
ON [TABLE] {table-name[,...] | view-name [,...]}
FROM AuthID[,...]
35
Revoke
REVOKE ALL
ON Customer_Loan
FROM PUBLIC ;
36
Revoke
EDWIN
3. REVOKE
2. GRANT
JACK
BORIS
37
Summary
38
Appendix - SQL Functions
Example:
SELECT SYSDATE FROM DUAL;
40
SQL Functions - Decode
Decode
– A DECODE function compares expr to each search value one by one. If expr
is equal to a search, Oracle returns the corresponding result. If no match is
found, Oracle returns default, or, if default is omitted, returns null.
41
SQL Functions - Trim
Trim
Syntax:
TRIM enables you to trim leading or trailing characters (or both) from a
character string. If trim_character or trim_source is a character literal, you
must enclose it in single quotes.
42
SQL Functions - Substr
Substr
Syntax:
43
SQL Functions - TRANSLATE
TRANSLATE
– The following statement translates a license number. All letters 'ABC...Z' are
translated to 'X' and all digits '012 . . . 9' are translated to '9':
SELECT TRANSLATE('2KRW229',
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'9999999999XXXXXXXXXXXXXXXXXXXXXXXXXX') "License"
FROM DUAL;
44
SQL Functions – TO_CHAR
TO_CHAR
45
SQL Functions – TO_DATE
TO_DATE
46
SQL Functions - NVL
NVL
– If expr1 is null, NVL returns expr2. If expr1 is not null, NVL returns expr1.
SELECT last_name,
NVL(TO_CHAR(commission_pct), 'Not Applicable')
"COMMISSION"
FROM employees
WHERE last_name LIKE 'B%';
47
Pseudocolumns
48
Pseudocolumns
• ROWID
• ROWNUM
49
Examples
50
Thank You