0% found this document useful (0 votes)
4 views

SQL - DML

The document provides an overview of SQL, focusing on Data Definition Language (DDL) and Data Manipulation Language (DML), including the basic structure of SQL queries. It covers various SQL clauses such as SELECT, FROM, WHERE, and discusses operations like joins, set operations, and aggregate functions. Additionally, it explains the use of SQL in database design and manipulation, with examples related to a banking database.

Uploaded by

Marie Sarkis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL - DML

The document provides an overview of SQL, focusing on Data Definition Language (DDL) and Data Manipulation Language (DML), including the basic structure of SQL queries. It covers various SQL clauses such as SELECT, FROM, WHERE, and discusses operations like joins, set operations, and aggregate functions. Additionally, it explains the use of SQL in database design and manipulation, with examples related to a banking database.

Uploaded by

Marie Sarkis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

SQL

Data Definition Language(DDL)


Data Manipulation Language(DML)

Data modification
§ Extraction
§ Insertion, Updating, Deletion
→ SQL: Basic Structure
Data Manipulation Language (DML)
Database Manipulation

Front-end; Database
end-user applications
Schema Database design

Implementation
SQL prompt MSSQLServer, MySql, Oracle, etc.
Submitting
SQL statements

S Q L (DDL) Databases
App-2
SQL Physical
(DML) (RDBMS) Database Schemas
+ DATA
...

App-n

2
Extraction
SQL: Basic Structure
® SQL is based on set and relational operations with certain modifications and
enhancements
A typical SQL query has the form:
select A1, A2, …, An
from r1, r2, …, rm
where P
§ Ai represent attributes
§ ri represent relations
§ P is a predicate.
® This query is equivalent to the relational algebra expression:

ÕA1, A2, …, An(sP (r1 ´ r2 ´ … ´ rm) )


® The result of an SQL query is a relation. I.e., SQL statements can be nested.

3
Consider the following
Banking database
Depositor Account
cname accid
accid bname
balance

Branch
Customer Borrower bname
cname
loanid city
cstreet cname assets
ccity

Loan
loanid
bname
amount branch (bname, bcity, assets)
customer (cname, cstreet, ccity)
depositor (#cname, #accid)
borrower (#cname, #loanid)
account (accid, #bname, balance)
loan (loanid, #bname, amount)

4
SQL: select … from … where

The select Clause


® The select clause corresponds to the projection operation of the relational algebra.
It is used to list the attributes desired in the result of a query.
loan (loanid, #bname, amount)
Find the names of all branches in the loan relation
select bname Equivalent to: Õbname(loan)
from loan
® An asterisk in the select clause denotes “all attributes”
select *
from loan
® SQL allows duplicates in relations as well as in query results. Use select distinct to
force the elimination of duplicates.
Find the names of all branches in the loan relation, and remove duplicates
select distinct bname
from loan
® The keyword all specifies that duplicates not be removed.
select all bname
from loan

5
SQL: select … from … where

The select Clause (Cont.)


The select clause can contain arithmetic expressions involving
the operators,+,-,÷ and ´, and operating on constants or
attributes of tuples. loan (loanid, #bname, amount)

Example of query:
select bname, loanid, amount *100
from loan

would return a relation which is the same as the loan relations,


except that the attribute amount is multiplied by 100

6
SQL: select … from … where

The where Clause loan (loanid, #bname, amount)


The where clause specifies conditions that tuples in the relations in the from
clause must satisfy.
Find all loan numbers for loans made at the Perryridge branch with loan amounts greater than
$1200.

select loanid
from loan
where bname = “Perryridge” and amount >1200

® SQL allows logical connectives and, or, and not. Arithmetic expressions can be used in the
comparison operators.

Note: attributes used in a query (both select and where parts) must be defined in the relations in
the from clause.
® SQL includes the between operator for convenience.
Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is,
³ $90,000 and £ $100,000)
select loanid
from loan
where amount between 90000 and 100000

7
SQL: select … from … where

The from Clause


® The from clause corresponds to the Cartesian product operation of the relational algebra.

Find the Cartesian product borrower ´ loan borrower (#cname, #loanid)


loan (loanid, #bname, amount)
select *
from borrower, loan
It is rarely used without a where clause.

Find the name and loan number of all customers having a loan at the Perryridge branch.

select distinct cname, b.loanid


from borrower as b, loan as l
where b.loanid = l.loanid
and bname = “Perryridge”

8
SQL: select … from … where

The Rename Operation

® Renaming relations and attributes using the as clause:


old-name as new-name

Find the name and loan number of all customers having a loan at the Perryridge
branch; replace the column name loanid with the name loan-id.
borrower (#cname, #loanid)
loan (loanid, #bname, amount)
select distinct cname, b.loanid as loan-id
from borrower as b, loan as l
where b.loanid = l.loanid
and bname = “Perryridge”

9
SQL: select … from … where

Tuple Variables/Alias
® Tuple variables are defined in the from clause via the use of the as clause.
Find the customer names and their loan numbers for all customers having a loan at some branch.

select distinct cname, loanid from borrower

® Tuple variable/Alias are used as short hand.


branch (bname, bcity, assets)
borrower (#cname, #loanid)
loan (loanid, #bname, amount)
Find the names of all branches that have greater assets than some branch located in Brooklyn.

select distinct B1.bname


from branch as B1, branch as B2
where B1.assets > B2.assets and B2.bcity=“Brooklyn”

10
SQL: select … from … where

String Operations
® Character attributes can be compared to a pattern:
§ % matches any substring.
§ _ matches any single character.

Find the name of all customers whose street includes the substring
‘Main’.

select cname customer (cname, cstreet, ccity)


from customer
where cstreet like “%Main%”
Match the name “Main%” : like “Main\%”

11
SQL: select … from … where

Ordering the Display of Tuples


List in alphabetic order the names of all customers having a loan at Perryridge
branch
borrower (#cname, #loanid)
loan (loanid, #bname, amount)
select distinct cname
from borrower b, loan l
where b.loanid = l.loanid and bname = “Perryridge”
order by cname

12
SQL: select … from … where

Set Operations
® The set operation union, intersect, and except operate on
relations and correspond to the relational algebra
operations È, Ç and -.
§ Each of the above operations automatically eliminates
duplicate;
§ To retain all duplicates use the corresponding multiset versions union all,
intersect all and except all.

§ Suppose a tuple occurs m times in r and n times in s, then, it occurs:


• m + n times in r union all s
• min(m,n) times in r intersect all s
• max(0,m-n) times in r except all s

13
SQL: select … from … where …union / intersect / except branch (branch-name, branch-city, assets)
customer (customer-name, customer-street, customer-city)
account (account-number, #branch-name, balance)
Set operations loan (loan-number, #branch-name, amount)
depositor (#customer-name, #account-number)
borrower (#customer-name, #loan-number)
Find all customers who have a loan, an account,
or both:

(select cname from depositor)


union
(select cname from borrower)

Find all customers who have both a loan and an account.

(select cname from depositor)


intersect
(select cname from borrower)

Find all customers who have an account but no loan.

(select cname from depositor)


except
(select cname from borrower)
14
Joined Relations
® Join operations take two relations and return as a result another relation.

® These additional operations are typically used as subquery expressions in


the from clause

® Join condition – defines which tuples in the two relations match, and what
attributes are present in the result of the join.
natural
Join Conditions on <predicate>
using (A1, A2, ..., An)

® Join type – defines how tuples in each relation that do not match any tuple in
the other relation (based on the join condition) are treated.

inner join
left outer join
Join Types right outer join
full outer join

15
® An extension of the join operation that avoids loss of information.
® Computes the join and then adds tuples form one relation that does not
Outer Join match tuples in the other relation to the result of the join
Difference between LEFT and RIGHT OUTER JOIN in SQL: In short, the following are
some notable difference between LEFT and RIGHT outer join in SQL :
1. LEFT OUTER join includes unmatched rows from the
left table while RIGHT OUTER join includes
unmatched rows from the right side of the table.

2. The result of LEFT OUTER join can be seen as


INNER JOIN + unmatched rows of left able while the
result of the RIGHT OUTER join is equal to INNER
JOIN + unmatched rows from the right side table.

§ In ANSI SQL, left outer join is written as LEFT JOIN


while right outer join is written as RIGHT JOIN in
select SQL statements.

The SQL RIGHT JOIN syntax


SELECT columns
FROM table1 LEFT/RIGHT/FULL [OUTER] JOIN table2
ON column1 = column2
WHERE condition

§ In Transact-SQL syntax left outer join is written as *= and right outer join is written as =*, Sybase
database supports both syntaxes and you can write join queries in both ANSI and T-SQL syntax.

16
SELECT columns Relation loan Relation borrower
FROM table1 LEFT/RIGHT/FULL JOIN table2
loan-number branch-name amount customer-name loan-number
L-170 Downtown 3000 Jones L-170
ON column-name1 = column-name2 L-230 Redwood 4000 Smith L-230
WHERE condition L-260 Perryridge 1700 Hayes L-155

Join – Example Inner Join loan borrower


loan-number branch-name amount customer-name
SELECT *
FROM loan l INNER JOIN L-170 Downtown 3000 Jones
borrower b L-230 Redwood 4000 Smith
ON l.loan-number = b.loan-
number
Left Outer Join loan borrower
loan-number branch-name amount customer-name
SELECT *
FROM loan l LEFT JOIN L-170 Downtown 3000 Jones
borrower b L-230 Redwood 4000 Smith
ON l.loan-number = b.loan- L-260 Perryridge 1700 null
number
Right Outer Join loan borrower
loan-number branch-name amount customer-name
SELECT * L-170 Downtown 3000 Jones
FROM loan l RIGHT JOIN L-230 Redwood 4000 Smith
borrower b
L-155 null null Hayes
ON l.loan-number = b.loan-
number
Full Outer Join loan borrower
loan-number branch-name amount customer-name
SELECT *
FROM loan l FULL JOIN L-170 Downtown 3000 Jones
borrower b L-230 Redwood 4000 Smith
ON l.loan-number = b.loan- L-260 Perryridge 1700 null
number L-155 null null Hayes

17
SQL JOINS; Summary…
There are three major types of joins.
Type 1: INNER JOIN - only where both tables match; INNER JOIN or JOIN
Type 2: OUTER JOINS where either one or both tables match
1. LEFT OUTER JOIN aka LEFT JOIN
2. RIGHT OUTER JOIN aka RIGHT JOIN
3. FULL OUTER JOIN aka FULL JOIN
Type 3: CROSS JOIN - Cartesian product(all possible combos of each table)

18
Aggregate functions - Group by
Relational Algebra:

Definition: : G1,G2, … GnÁ F1 A1, F2 A2,…, Fm Am(E)


Example: (
bname Á count (distinct accid ) account )
Operates on a column of a relation, and return a value
account (accid, #bname, balance)

Aggregate function types:


avg average value
min minimum value
max maximum value
sum sum of values
count number of values

19
account (accid, #bname, balance)
Aggregate functions - Group by Aggregate function types:
avg average value
groupByK min minimum value
select a1,a2,…, an, agg-function(….) max maximum value
from account sum sum of values
Where …. groupByK count number of values
group by a1,a2,…, an
Where

Attributes in select clause outside of aggregate functions


must appear in group by list, why? bname accid balance
Perryridge A102 400
Perryridge A201 900
select bname, balance, count(distinct accid) Brighton A217 750
from account Brighton A215 750
group by bname, balance Redwood a222 700

20
SQL: select …avg/min… from … where

Aggregate Functions-Example bname accid balance


Find the numbers of tuples in the customer relation. Perryridge A102 400
select count(*)
from customer Perryridge A201 900
§ remember * stands for all attributes Brighton A217 750
§ compare to: Brighton A215 750
select count(ccity) Redwood a222 700
from customer

Find the average account balance at the Perryridge branch.


select avg(balance) select bname, sum(balance) as totbal
from account from account
where bname=“Perryridge” group by bname bname totbal
Perryridge 1300
Find the number of depositor in the bank Brighton 1500
select count (distinct cname) Redwood 700
from depositor
ü distinct is redundant if you know cname is a key
depositor (#cname, #accid)
21
SQL: select… from … where… group by/having select a1,a2,…, an, agg-function(….)
from account
Aggregate functions group by a1,a2,…, an

Aggregate functions-Group by Attributes in select clause


Find the number of depositors for each branch. outside of aggregate
functions must appear in
select bname, count(distinct cname) group by list
from depositor, account
branch (bname, bcity, assets)
where depositor.accid = account.accid customer (cname, cstreet, ccity)
group by bname depositor (#cname, #accid)
borrower (#cname, #loanid)
account (accid, #bname, balance)
loan (loanid, #bname, amount)
Aggregate Functions-Having Clause
Find the names of all branches where the average account balance is more
than $1200
depositor (#cname, #accid)
select bname, avg(balance) account (accid, #bname, balance)
from account predicates in the having
group by bname clause are applied after the
having avg (balance) >1200 formation of groups

22
Aggregate functions - Group by
List the branches where there is 100 or more accounts.
select bname, count(distinct accid) as Nbacc
from account
group by bname
having Nbacc >=100
For each group of tuples with the same branch name (bname), apply
aggregate function count and distinct to accid
grouping
bname accid balance
Perryridge A102 400
Brighton A217 750 bname accid balance counting
Perryridge A102 400
Perryridge A201 900
Perryridge A201 900 bname count-accid
Brighton A215 750
Brighton A217 750 Perryridge 2
Redwood a222 700
Brighton A215 750 Brighton 2
Redwood a222 700 Redwood 1

23
Aggregate/Group by with Join
Find the number of depositors for each branch.

select bname, count(distinct cname)


from depositor, account
where depositor.accid= account.accid
group by bname
depositor (#cname, #accid)
account (accid, #bname, balance)

Perform Join then group by then count ( distinct () )


ØJoin Þ (cname, accid, bname, balance)
Group by and aggregate functions apply to the Join
result

24
Aggregate Functions-Having Clause
Find the names of all branches where the average account
balance is more than $1200
select bname, avg(balance)
from account
group by bname
having avg (balance) >1200
predicates in the having clause are applied to each group after the formation
of groups
bname accid balance
Perryridge A102 400
Perryridge A201 900
Brighton A217 750
Brighton A215 750
Redwood a222 700
25
Null Values
It is possible for tuples to have a null value, denoted by null,for some
of their attributes; null signifies an unknown value or that a value
does not exist.
The result of any arithmetic expression involving null is null.
® Find all loan numbers which appear in the loan relation with null values
for amount.
select loanid
from loan
where amount is null
® Total all loan amounts
select sum(amount)
from loan
Above statement ignores null amounts; result is null if there is no non-null amount.

® All aggregate operations except count(*) ignore tuples with null


values on the aggregated attributes.
26
NULL Values

27
SQL - Nested Subqueries
® Every SQL statement returns a relation/set in the result;
remember a relation could be null or merely contain a single
atomic value
® You can replace a value or set of values with a SQL statement
(ie., a subquery)

select * select *
from loan from loan
where amount > 1200 where amount > (select avg(amount)
from loan )
MIllegal if the sub-query returns the wrong type for the
comparison

28
Set Membership
® F in r Û $ t Î r ( t = F )
F is typically an attribute name and the set of values is typically
returned from a subquery

0
4
(5 in 5 ) returns true
0
Example Query
4 Find all customers who have both an
(5 in 6 ) returns false account and a loan in the bank.
select distinct cname
0 from borrower
4
where cname in (select cname
(5 not in 6 ) returns true
from depositor)

29
Example

Example Query
Find all customers who have a loan at the bank but do not have an
account at the bank. depositor (#cname, #accid)
select distinct cname borrower (#cname, #loanid)
from borrower account (accid, #bname, balance)
where cname not in (select cname loan (loanid, #bname, amount)
from depositor )
Find all customers who have both an account and a loan at the
Perryridge branch.
select distinct cname
from borrower, loan
where borrower.loanid = loan.loanid and
bname =“Perryridge” and
(bname , cname ) in
(select bname , cname
from depositor, account
where depositor.accid = account.accid)
30
Set Comparison
Find all branches that have greater assets than
some branches located in Brooklyn.
branch (bname, bcity, assets)

select distinct B1.bname


from branch as B1, branch as B2
where B1.assets > B2.assets
and B2.bcity= “Brooklyn”

31
Views
® Provide a mechanism to hide certain data from the view of certain
users. To create a view we use the command:
create view v as <query expression> create view result as
where: select bname ,
• <query expression> is any legal SQL query avg(balance) as avg-balance
• the view name is represented by v from account
group by bname
Example Queries
• A view consisting of branches and their customers
create view all-customer as
(select bname , cname
from depositor, account
where depositor.accid = account.name-number)
union (select bname , cname
from borrower, loan
where borrower.loanid = loan.loan-number)
Find all customers of the Perryridge branch
select cname
from all-customer
where bname = “Perryridge”

32
Data Manipulation Language(DML)
Data modification
§ Extraction
§ Insertion, Updating, Deletion
→ SQL: Basic Structure
SQL: Modification of the database – delete, update, insert
Modification of the Database - Deletion
® Delete all account records at the Perryridge branch
account (accid, #bname, balance)
delete from account
where bname = “Perryridge”

Conceptually, delete is done in two steps:


§ Find the tuples you want to delete:
select *
from account
where bname = “Perryridge”
§ delete the tuples you found.

34
SQL: Modification of the database – delete, update, insert
Modification of the Database - Deletion
® Delete all accounts at every branch located in Needham.

delete from depositor depositor (#cname, #accid)


branch (bname, bcity, assets)
where accid in ( select accid account (accid, #bname, balance)
from branch, account
where bcity = “Needham”
and branch.bname = account.bname )

delete from account


where bname in (select bname from branch
where bcity = “Needham”)
® The first delete is needed to enforce “every depositor must have at least
one account”. Note that the two deletes can’t be reordered.
35
SQL: Modification of the database – delete, update, insert
Example Query
Delete the records of all accounts with balances below the
average at the bank
delete from account
where balance < (select avg (balance)
from account)
§ Problem: as we delete tuples from deposit, the average
balance changes
§ Solution used in SQL:
• First, compute and save the average balance of the
bank in a variable
• Next, delete all accounts with balance less than the
average

36
SQL: Modification of the database – delete, update, insert

Modification of the database -Insertion


® Add a new tuple to account account (accid, #bname, balance)
insert into account values (“Perryridge”, A-9732, 1200)
insert into account (bname , accid, balance,)
values
(“Perryridge”, A-97321, 1200),
(“Perryridge”, A-97322, 1321),
(“Perryridge”, A-97323, 1100),
(“Perryridge”, A-97324, 1000)

To reorder attributes, specify attribute names explicitly:


insert into account (bname , balance, accid)
values (“Perryridge”, 1200, A-9732)

® Add a new tuple to account with balance set to null


insert into account values ( “Perryridge”, “A-777”, null)

37
SQL: Modification of the database – delete, update, insert

Modification of the Database-Insertion


® Create a $200 savings account for all loan customers of the Perryridge branch.
Let the loan number serve as the account number for the new savings account.
depositor (#cname, #accid)
borrower (#cname, #loanid)
insert into account account (accid, #bname, balance
select loanid, bname , 200 loan (loanid, #bname, amount)
from loan
where bname =“Perryridge”

insert into depositor


select cname , loan-number select bname , loan-number, 200
into NewAccount
from loan, borrower from loan
where bname =“Perryridge” where bname =“Perryridge”
and loan.accid = borrower.accid

38
SQL: Modification of the database – delete, update, insert
Modification of the database- Updates
® Increase all accounts with balance over $10,000 by 6%, all
other accounts receive 5%.
account (accid, #bname, balance)
§ Write two update statements:
update account Sailors
sid sname rating age
set balance = balance *1.06 22 Dustin 7 45.0
29 Brutus 1 33.0
where balance >10000 31 Lubber 8 55.5
32 Andy 8 25.5
update account 58
update
Rusty
Sailors
10 35.0
64 Horatio 7 35.0
set balance = balance *1.05 71 set rating = 10
Zorba rating+116.0
where balance £ 10000 74
85
Horatio age >40
where
Art
9
3
35.0
25.5
95 Bob 3 63.5
§ the order is important
§ can be done better using the case statement

39
SQL: Modification of the database – delete, update, insert
Update of a View
® Create a view of all loan data in the loan relation, hiding the amount
attribute
create view branch-loan as
select bname , loan-number
from loan
loan (loanid, #bname, amount)
® Add a new tuple to branch-loan
insert into branch-loan
values(“Perryridge”, “L-307”)
This insertion must be represented by the insertion of the tuple
(“Perryridge”, “L-307”,null)
into the loan relation.

Note: Updates on more complex views are difficult or impossible


to translate, and hence are disallowed.

40

You might also like