Basic SQL: Section 4.1-4.7
Basic SQL: Section 4.1-4.7
By Rahul Mehta
1
Overview
Background
Basic Structure
Set Operations
Aggregate Functions
Null Values
Nested Subqueries
Views
2
Background
3
Different parts of SQL
Data-definition language
Interactive data-manipulation language
View definition
Transaction Control
Embedded SQL and dynamic SQL
Integrity
Authorization
4
Basic Structure of SQL
Consists of three clauses:
(i) Select
- Used to list the attributes desired in the result of a query.
(ii) From
- Lists the relations to be scanned in the evaluation of the expression.
(iii) Where
- Consists of a predicate involving attributes of the relations that appear in the from
clause.
5
A typical SQL query form
6
The Select Clause
7
More examples continued
8
The where clause
Example:
“Find all loan numbers for loans made at the
Perryridge branch with loan amounts greater than
$ 1200.”
select loan-number
from loan
where branch-name = ‘Perryridge’ and amount > 1200
9
More examples of Where
clause
Logical connectives like and, or, and not are
used in the where clause
Example:
Loan number of those loans with loan amounts
between $90,000 & $ 100,000
select loan number
from loan
where amount between 90000 and 100000
10
The from Clause
11
The from Clause (Con’d)
12
The Rename Operation
old-name as new-name
13
The Rename Operation
(Con’d)
Example:
To change attribute name loan-number to be replaced
with name loan-id :
14
String Operations
15
String Operations (Con’d)
Example:
Find the names of all customers whose
street address includes the substring
‘Main’
select customer-name
from customer
where customer-street like ‘%Main%’
16
Set Operations
17
Union Operation
Example:
To find all customers having a loan, an
account, or both at bank:
(select customer-name
from depositor)
union
(select customer-name
from borrower)
18
Intersect Operation
Example:
To find all customers who have both a loan
and an account at the bank:
(select distinct customer-name
from depositor)
intersect
(select distinct customer-name
from borrower)
19
Except Operation
Example:
To find all customers who have an account but
no loan at the bank:
(select distinct customer-name)
from depositor)
except
(select customer-name
from borrower)
20
Aggregate Functions
21
Aggregate Functions (Con’d)
Example:
Find the average account balance at the
Perryridge branch.”
select avg (balance)
from account
where branch-name =‘Perryridge’
22
Null Values
23
Null Values (Con’d)
Example:
select loan-number
from loan
where amount is null
24
Nested Subqueries
A subquery is a select-from-where
expression that is nested within another
query.
Common use includes:
Perform tests for set membership
Make set comparisons
Determine set cardinality
25
Nested Subqueries (Con’d)
Example:
Find those customers who are borrowers from
the bank and who appear in the list of account
holders obtained in the subquery
select distinct customer-name
from borrower
where customer-name in (select customer-
name from depositor)
26
Views
27
Views (Con’d)
Example:
Using view all-customer, we can find all
customers of the Perryridge branch:
select customer-name
from all-customer
where branch-name = ‘Perryridge’
28
Bibliography
29
The End
30