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
IBM developed the original version of SQL at
its San Jose Research Laboratory
Evolved as The Sequel language, its name
has changed to SQL (Structured Query
Language)
SQL has clearly established itself as the
standard relational-database language
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
Select: A1, A2,….An
Ai represents an attribute.
From: r1, r2,….rm
ri is a relation
Where: P
P represents a predicate.
6
The Select Clause
Example of a Simple Query:
“Find the names of all branches in the
loan relation”
select branch-name
from loan
7
More examples continued
Inserting keyword distinct after select we
can eliminate duplication
For instance:
select distinct branch-name
from loan
Inserting keyword all after select helps
restoring duplication.
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
Defines a Cartesian product of the
relations in the clause.
Example:
“For all customers who have a loan from
the bank, find their names, loan numbers
and loan amount”
11
The from Clause (Con’d)
select customer-name, borrower.loan-
number, amount
from borrower, loan
where borrower.loan-number =
loan.loan-number
12
The Rename Operation
Uses as clause to rename both, relations
and attributes
The as clause takes the form in SQL:
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 :
select customer-name, borrower.loan-number as loan-
id, amount
from borrower, loan
where borrower.loan-number = loan.loan-number
14
String Operations
SQL specifies by enclosing in single quotes,
for example, ‘Perryridge’
“%” character is use to match any substring.
“_” character is use to match any character
It expresses patterns by using the like
comparison operator
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
Operations such as union, intersect, ad
except operate on relations.
Corresponds to relational-algebra
operations , and .
Relations participating in the operations
must be compatible; i.e. must have same
set of attributes.
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
These functions take a collection of values as
input and return a single value.
SQL offers five built-in aggregate functions:
Average: avg
Minimum: min
Maximum: max
Total: sum
Count: count
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
Used to indicate absence of information
about the value of an attribute.
Can use special keyword null in a
predicate to test for a null value.
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
We define a view in SQL by using the
create view command.
To define a view, we must give the view
a name and must state the query that
computes the view.
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
Silbershcatz, A., Korth, H. and
Sudarshan, S. (2002). Database System
Concepts, 4th Edition
29
The End
Good Luck for the Quiz !!
30