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

Chapter 3 - SQL_PartA

sss

Uploaded by

YouTubeATP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Chapter 3 - SQL_PartA

sss

Uploaded by

YouTubeATP
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Chapter 3A.

Structured Query
Language (SQL I)
COMP3278 Introduction to
Database Management Systems

Department of Computer Science, The University of Hong Kong


Slides prepared by - Dr. Chui Chun Kit for students in COMP3278
For other uses, please email : [email protected]
Outcome based learning (OBL)

Outcome 1. Information Modeling


Able to understand the modeling of real life information in a database
system.

Outcome 2. Query Languages


Able to understand and use the languages designed for data access.

Outcome 3. System Design


Able to understand the design of an efficient and reliable database
system.

Outcome 4. Application Development


Able to implement a practical application on a real database.
2
Revision
Let's consider the following steps in developing a
database application in a banking enterprise.
Step 1. Information modeling (Chapter 2A).
name branch_id amount loan_id
asset

loan-
branch loan
branch

account-
branch borrower customer_id

account_id
name
balance account owner customer
address 3
Revision
Step 2. Reduce to database tables (Chapter 2B, 2C).
Branch ( branch_id, name, asset)
Foreign key : none.
Loan( loan_id, amount, branch_id)
Foreign key : branch_id REFERENCES Branch (branch_id).
Customer( customer_id, name, address)
Foreign key : none.
Account( account_id, balance, branch_id)
Foreign key : branch_id REFERENCES Branch (branch_id).

Borrower( loan_id, customer_id)


Foreign key : loan_id REFERENCES Loan (loan_id).
customer_id REFERENCES Customer (customer_id).
Owner( account_id, customer_id)
Foreign key : account_id REFERENCES Account (account_id).
customer_id REFERENCES Customer (customer_id). 4
Revision
Step 3. Create the database (this chapter).
Step 4. Design the SQL to access data for the
application (this chapter).
Step 5. Implementing the application (lab tutorials).

5
Running example
Foreign key Foreign key

Branch Loan Borrower


branch_id name asset branch_id loan_id amount customer_id loan_id
B1 Central 7100000 B3 L1 900 C1 L3
B2 Causeway Bay 9000000 B1 L2 1500 C4 L2
B3 Aberdeen 400000 B1 L3 1000 C2 L1
B4 North Point 3700000

Foreign
Foreign key
Foreign key
key
Account Owner Customer
account_id customer_id customer_id name address
branch_id account_id balance Foreign
A1 C1 key C1 Kit CB320
B1 A1 500
B2 A2 400 A1 C2 C2 Ben CB326
A2 C2 C3 Diana CB311
B2 A3 900
A3 C4 C4 Tom CB415
B1 A4 700
A4 C4

6
What is SQL?
Structured Query Language (pronounced as “sequel”)

Language for defining, modifying and querying data


in an RDBMS.
SQL is declarative
Concerns about the task we want to accomplish, without
specifying how.
SQL has many standards and implementations
Read the documentation on which features are supported
exactly.

7
Section 1

Data definition

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]
Create table
A database table is defined using the CREATE TABLE
command.
Table name (cannot be a keyword
in database, e.g., Create)

CREATE TABLE Branch Column type


Column name (
branch_id VARCHAR(15), NOT NULL means
Constraint (Primary Key), name VARCHAR(30) NOT NULL, each record's
value in the
optional in this statement, asset INT UNSIGNED NOT NULL,
can add back later using column must not
PRIMARY KEY(branch_id) be a null value.
the ALTER TABLE );
command.

PRIMARY KEY No comma in the


automatically ensures last instruction.
NOT NULL. 9
Drop table
DROP TABLE deletes all information about the
dropped table from the database.
DROP TABLE Branch;

The DBMS may reject the DROP TABLE instruction when


the table is referenced by another table via some
constraints (e.g., referential constraints).
Foreign key
After the foreign key is established,
Customer Borrower if we drop the Customer table, the
customer_id name address customer_id loan_id records in the Borrower table will
C1 Kit CB320 C1 L3 lost their references .
C2 Ben CB326 C4 L2 (i.e., Cannot find out who borrow
C3 Diana CB311 C2 L1
the loan anymore.)
10
C4 Tom CB415
Alter table
ALTER TABLE can be used to
Add columns to an existing table.
ALTER TABLE Branch ADD branch_phone INT (12);

Remove a column from a table.


ALTER TABLE Branch DROP branch_phone;

Add constraints (e.g., PRIMARY KEY) to a table.


ALTER TABLE Branch ADD PRIMARY KEY (branch_id);

11
Foreign key constraints
A foreign key is a referential constraint between
two tables.
The columns in the referencing table must reference
the columns of the primary key or other superkey in
the referenced table.
I.e., The value in one row of the referencing columns
must occur in a single row in the referenced table (Why?).

Referenced table Referencing columns Referenced table

customer_id account_id

Foreign key customer_id C1 A1 Foreign key account_id


references C2 A1 references
Customer(customer_id) Account(account_id)
C3 A3 Account
Customer
12
Owner
Foreign key constraints
The foreign key can be established in the CREATE
TABLE command.
CREATE TABLE Owner
(
customer_id VARCHAR(15),
account_id VARCHAR(15),
PRIMARY KEY(customer_id, account_id),
FOREIGN KEY(customer_id) REFERENCES Customer(customer_id),
FOREIGN KEY(account_id) REFERENCES Account(account_id)
);

The foreign key can also be defined using the ALTER


TABLE command.
ALTER TABLE Owner
ADD FOREIGN KEY (customer_id) REFERENCES Customer(customer_id);
13
Foreign key constraints
In MySQL 5.5, the tables using the InnoDB storage
engine (but not MyISAM) supports foreign key
constraints.
Specify the storage engine while creating the table
CREATE TABLE Branch
(
branch_id VARCHAR(15),
name VARCHAR(30) NOT NULL,
asset INT UNSIGNED NOT NULL,
PRIMARY KEY(branch_id)
) ENGINE = INNODB;

Or we can change the storage engine after creating a table.


ALTER TABLE Branch ENGINE = INNODB;
14
Section 2

Insert, Delete
and Update

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]
The INSERT clause
The INSERT INTO command is used to insert records
(tuples) into the database table.
branch_id name asset branch_id name asset
Empty B1 Central 7100000

Branch Branch

Table name (Case sensitive)

INSERT INTO Branch VALUES ( 'B1' , 'Central', 7100000);

Value in the Value in the Value in the


first column second column third column
Inserting multiple records
INSERT INTO Branch VALUES
( 'B2' , 'Causeway Bay', 9000000),
( 'B3' , 'Aberdeen', 400000); 16
The INSERT clause
Most DBMS provide an alternative way to insert large
amount of records into a table.
E.g., LOAD DATA LOCAL INFILE in MySQL.

LOAD DATA LOCAL INFILE 'text.txt'


B1;Central;7100000
INTO TABLE Branch
B2;Causeway Bay;9000000
FIELDS TERMINATED BY ';'
B3;Aberdeen;400000
LINES TERMINATED BY '\n';
B4; North Point;3700000

text.txt

17
The DELETE clause
The DELETE FROM command is used to delete records
(tuples) from a database table.
Query: Delete all records from the Branch table.
branch_id name asset branch_id name asset
B1 Central 7100000 Empty
B2 Causeway Bay 9000000 Branch
B3 Aberdeen 400000
B4 North Point 3700000
Branch

DELETE FROM Branch;

18
The DELETE clause
The DELETE FROM command is used to delete records
(tuples) from a database table.
Query: Delete the branch “Central” from the Branch table.
branch_id name asset branch_id name asset
B1 Central 7100000 B2 Causeway Bay 9000000
B2 Causeway Bay 9000000 B3 Aberdeen 400000
B3 Aberdeen 400000 B4 North Point 3700000
B4 North Point 3700000
Branch
Branch

DELETE FROM Branch WHERE name = 'Central';

The tuples that satisfy the conditions specified here are deleted.

19
The DELETE clause
The UPDATE command is used to update records
(tuples) from a database table.
Query: Update the asset of branch with branch_id 'B1' to $0.

branch_id name asset branch_id name asset


B1 Central 7100000 B1 Central 0
B2 Causeway Bay 9000000 B2 Causeway Bay 9000000
B3 Aberdeen 400000 B3 Aberdeen 400000
B4 North Point 3700000 B4 North Point 3700000
Branch Branch

UPDATE Branch
SET asset = 0
WHERE branch_id = 'B1';
20
The UPDATE clause
The UPDATE command can also be used with
arithmetic expressions.
Query: Increase all accounts with balances over $500 by 6%.

account_id branch_id balance account_id branch_id balance


A1 B1 500 A1 B1 500
A2 B2 400 A2 B2 400
A3 B2 900 A3 B2 954
A4 B1 700 A4 B1 742

Account Account

UPDATE Account
SET balance = balance * 1.06
WHERE balance > 500;
21
The UPDATE clause
The UPDATE command can also be used with
arithmetic expressions.
Query: Increase all accounts with balances under $500
by 5% and all other accounts by 6%.
account_id branch_id balance account_id branch_id balance
A1 B1 500 A1 B1 530
A2 B2 400 A2 B2 420
A3 B2 900 A3 B2 954
A4 B1 700 A4 B1 742

Account Account

UPDATE Account UPDATE Account


SET balance = balance * 1.05 SET balance = balance * 1.06
WHERE balance < 500; WHERE balance >= 500;
The order of executing these two is important! 22
The UPDATE clause
The CASE command can be used to perform
conditional update.

UPDATE Account
SET balance = CASE
WHEN balance <=500 THEN balance *1.05
ELSE balance * 1.06
END

Note: When there are multiple


WHEN … THEN in the query, only
the first true statement (from
top to bottom) will be executed.

23
Section 3

Querying

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]
The SELECT clause
The SELECT clause lists the attributes desired in the
result of a query.
Query: Find the names of all customers.
customer_id name address name
C1 Kit CB320 Kit
C2 Ben CB326 Ben
C3 Diana CB311 Diana
C4 Tom CB415 Tom
Customer Result

SELECT name FROM Customer;

An asterisk in the select clause denotes “all attributes”


Query: List all column values of all customer records.
SELECT * FROM Customer; 25
The SELECT clause
The SELECT clause can contain arithmetic
expressions (+, –, *, /) operating on constants or
attributes of tuples.
Query: List the loan_id and amount of each loan record,
display the amount in USD (originally stored in HKD).
loan_id branch_id amount loan_id amount /7.8

L1 B3 900 L1 115.385

L2 B2 1500 L2 192.308

L3 B1 1000 L3 128.205

Loan Loan

SELECT loan_id, amount/7.8


FROM Loan;
26
The FROM clause
The FROM clause lists the relations (tables) involved in
the query.
Query: Find the Cartesian product of Customer and Borrower
SELECT * customer_id name address customer_id loan_id

FROM Customer, Borrower;


customer_id name address
C1 Kit CB320
C2 Ben CB326
C3 Diana CB311
C4 Tom CB415
Customer
Cartesian product
of A and B means
customer_id loan_id generate all
C1 L3 possible pairs of
C4 L2
records from A
and B.
C2 L1
Borrower Cartesian product of Customer and Borrower 27
The FROM clause
The FROM clause lists the relations (tables) involved in
the query.
Query: Find the Cartesian product of Customer and Borrower
SELECT * customer_id name address customer_id loan_id
C1 Kit CB320 C1 L3
FROM Customer, Borrower;
customer_id name address
C1 Kit CB320
C2 Ben CB326
C3 Diana CB311
C4 Tom CB415
Customer
Cartesian product
of A and B means
customer_id loan_id generate all
C1 L3 possible pairs of
C4 L2
records from A
and B.
C2 L1
Borrower Cartesian product of Customer and Borrower 28
The FROM clause
The FROM clause lists the relations (tables) involved in
the query.
Query: Find the Cartesian product of Customer and Borrower
SELECT * customer_id name address customer_id loan_id
C1 Kit CB320 C1 L3
FROM Customer, Borrower;
C2 Ben CB326 C1 L3
customer_id name address
C1 Kit CB320
C2 Ben CB326
C3 Diana CB311
C4 Tom CB415
Customer
Cartesian product
of A and B means
customer_id loan_id generate all
C1 L3 possible pairs of
C4 L2
records from A
and B.
C2 L1
Borrower Cartesian product of Customer and Borrower 29
The FROM clause
The FROM clause lists the relations (tables) involved in
the query.
Query: Find the Cartesian product of Customer and Borrower
SELECT * customer_id name address customer_id loan_id
C1 Kit CB320 C1 L3
FROM Customer, Borrower;
C2 Ben CB326 C1 L3
C3 Diana CB311 C1 L3
Cartesian product is the most primitive way C4 Tom CB415 C1 L3
of joining two tables. However, many C1 Kit CB320 C4 L2
resulting tuples are not very useful. C2 Ben CB326 C4 L2
Therefore, we often need to specify the C3 Diana CB311 C4 L2
joining condition to filter out the non- C4 Tom CB415 C4 L2
meaningful results. C1 Kit CB320 C2 L1
C2 Ben CB326 C2 L1
C3 Diana CB311 C2 L1
C4 Tom CB415 C2 L1
Cartesian product of Customer and Borrower 30
The WHERE clause
The WHERE clause specifies conditions that the result
must satisfy.
Query: For each loan, find out the name of the customer who
borrow the loan.

Let us learn the process of


constructing the SQL for this query.

31
The WHERE clause
The WHERE clause specifies conditions that the result
must satisfy.
Query: For each loan, find out the name of the customer who
borrow the loan.
Step 1. What are the table(s) that contain
customer_id loan_id the information to answer this query?
C1 L3
C4 L2
C2 L1
Borrower
Observation 1.
First, the information of customers (customer_id)
customer_id name address who borrow loan is in the Borrower table.
C1 Kit CB320
C2 Ben CB326 Observation 2.
C3 Diana CB311 Second, we need to find out the name of the
C4 Tom CB415 customer, the name is in the Customer table.
Customer 32
32
The WHERE clause
Step 2. Now we want to relate
SELECT Borrower.loan_id, Customer.name two tables, if no conditions is
FROM Customer, Borrower specified, Cartesian product
will be returned. What is the
joining condition?

customer_id name address customer_id loan_id


C1 Kit CB320 C1 L3
customer_id loan_id C2 Ben CB326 C1 L3
C1 L3 C3 Diana CB311 C1 L3
C4 L2 C4 Tom CB415 C1 L3
C2 L1 C1 Kit CB320 C4 L2
Borrower
C2 Ben CB326 C4 L2
C3 Diana CB311 C4 L2
customer_id name address
C4 Tom CB415 C4 L2
C1 Kit CB320
C1 Kit CB320 C2 L1
C2 Ben CB326
C2 Ben CB326 C2 L1
C3 Diana CB311
C3 Diana CB311 C2 L1
C4 Tom CB415
C4 Tom CB415 C2 L1
Customer
Cartesian product of Customer and Borrower 33
The WHERE clause
loan_id name
L3 Kit
SELECT Borrower.loan_id, Customer.name
L2 Tom
FROM Customer, Borrower L1 Ben
WHERE Customer.customer_id = Result
Borrower.customer_id
customer_id name address customer_id loan_id
C1 Kit CB320 C1 L3
customer_id loan_id C2 Ben CB326 C1 L3
C1 L3 C3 Diana CB311 C1 L3
C4 L2 C4 Tom CB415 C1 L3
C2 L1 C1 Kit CB320 C4 L2
Borrower
C2 Ben CB326 C4 L2
C3 Diana CB311 C4 L2
customer_id name address
C4 Tom CB415 C4 L2
C1 Kit CB320
C1 Kit CB320 C2 L1
C2 Ben CB326
C2 Ben CB326 C2 L1
C3 Diana CB311
C3 Diana CB311 C2 L1
C4 Tom CB415
C4 Tom CB415 C2 L1
Customer
Cartesian product of Customer and Borrower 34
The WHERE clause
The WHERE clause specifies conditions that the result
must satisfy.
Comparison results can be combined using logical
connectives AND, OR, and NOT.
Query: Find all loan ID of loans made at branch_id B1 with
loan amounts >$1200.
branch_id loan_id amount loan_id
B3 L1 900 L2

There are two B1 L2 1500 Result


conditions in the query! B1 L3 1000

Loan

SELECT loan_id
FROM Loan
WHERE branch_id = 'B1' AND
amount > 1200; 35
Exercise
Query: Find the names of all branches that have a loan.
Step 1. Identify the tables that contain the necessary
information to answer the query.
Branch Loan Borrower
branch_id name asset branch_id loan_id amount customer_id loan_id
B1 Central 7100000 B3 L1 900 C1 L3
B2 Causeway Bay 9000000 B1 L2 1500 C4 L2
B3 Aberdeen 400000 B1 L3 1000 C2 L1
B4 North Point 3700000

Account Owner Customer


account_id customer_id customer_id name address
branch_id account_id balance
A1 C1 C1 Kit CB320
B1 A1 500
A1 C2 C2 Ben CB326
B2 A2 400
A3 C3 C3 Diana CB311
B2 A3 900
A4 C4 C4 Tom CB415
B1 A4 700
A5 C4
36
Exercise
Query: Find the names of all branches that have a loan.
Step 1. Identify the tables that contain the necessary
information to answer the query.
Branch Loan
branch_id name asset branch_id loan_id amount
B1 Central 7100000 B3 L1 900
B2 Causeway Bay 9000000 B1 L2 1500
B3 Aberdeen 400000 B1 L3 1000
B4 North Point 3700000

Step 2. Construct the SELECT statement.


SELECT ?
FROM Branch, Loan
WHERE ?
;
37
Exercise
Query: Find the names of all branches that have a loan.
Step 1. Identify the tables that contain the necessary
information to answer the query.
Branch Loan
branch_id name asset branch_id loan_id amount
B1 Central 7100000 B3 L1 900
B2 Causeway Bay 9000000 B1 L2 1500
B3 Aberdeen 400000 B1 L3 1000
B4 North Point 3700000

Step 2. Construct the SELECT statement.


SELECT Branch.name
FROM Branch, Loan
WHERE ?
;
38
Exercise
Query: Find the names of all branches that have a loan.
Step 1. Identify the tables that contain the necessary
information to answer the query.
Usually, when linking
Branch Loan the information of
branch_id name asset branch_id loan_id amount
two tables, we need
B1 Central 7100000 B3 L1 900
to specify the joining
B2 Causeway Bay 9000000 B1 L2 1500 condition. Often we
B3 Aberdeen 400000 B1 L3 1000 need to join the
B4 North Point 3700000 columns that
participate in the
Step 2. Construct the SELECT statement. referential constraint
between the two
SELECT Branch.name tables.
FROM Branch, Loan
WHERE Branch.branch_id = Loan.branch_id
; Joining condition
39
Exercise
Query: Find the names of all branches that have a loan.
Step 1. Identify the tables that contain the necessary
information to answer the query.
Branch Loan
branch_id name asset branch_id loan_id amount
B1 Central 7100000 B3 L1 900
B2 Causeway Bay 9000000 B1 L2 1500
B3 Aberdeen 400000 B1 L3 1000
Duplicate values
B4 North Point 3700000
return!
Step 2. Construct the SELECT statement.
name
SELECT Branch.name
Central
FROM Branch, Loan
Central
WHERE Branch.branch_id = Loan.branch_id
Aberdeen
;
Result
40
Exercise
Query: Find the names of all branches that have a loan.
Step 1. Identify the tables that contain the necessary
information to answer the query. You can eliminate duplicate
values in the results by using
Branch Loan the DISTINCT keyword.
branch_id name asset branch_id loan_id amount
B1 Central 7100000 B3 L1 900
B2 Causeway Bay 9000000 B1 L2 1500
B3 Aberdeen 400000 B1 L3 1000
Duplicate values
B4 North Point 3700000
return!
Step 2. Construct the SELECT statement.
name
SELECT DISTINCT Branch.name
Central
FROM Branch, Loan
Aberdeen
WHERE Branch.branch_id = Loan.branch_id
Result
;
41
Section 4

Renaming

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]
Renaming
name
SELECT DISTINCT Branch.name
Central
FROM Branch, Loan
Aberdeen
WHERE Branch.branch_id = Loan.branch_id
Result
;

I want to rename the


Rename can be operated on column “name” in the
both tables and attributes. result into “Branch
name”.
Rename on attribute.
We use the keyword AS to signify renaming.

SELECT DISTINCT Branch.name AS 'Branch name' Branch name

FROM Branch, Loan Central

WHERE Branch.branch_id = Loan.branch_id Aberdeen

; Result

43
Renaming
name
SELECT DISTINCT Branch.name
Central
FROM Branch, Loan
Aberdeen
WHERE Branch.branch_id = Loan.branch_id
Result
;

The two SQLs are


Rename can be operated on equivalent to each other.
both tables and attributes.
Rename on tables.

name
SELECT DISTINCT B.name
Central
FROM Branch B, Loan L
Aberdeen
WHERE B.branch_id = L.branch_id
Result
;
44
Section 5

String operations

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]
The LIKE clause
The most commonly used operation on strings is
pattern matching using LIKE.
Percent(%):matches any substring.
Underscore(_): matches any character.

'Perry%' matches any string beginning with “Perry”.

'_ _ _ %' matches any string of at least 3 characters.

Note: Patterns are case sensitive.

46
The LIKE clause
Query: Find the names of all customers whose
address includes the substring '320'.
Customer
customer_id name address name
C1 Kit CB320 Kit
C2 Ben CB326 Result
C3 Diana CB311
C4 Tom CB415

SELECT name
FROM Customer
WHERE address LIKE '%320%';

Question: How about matching using


regular expression? ☺ 47
Section 6

Ordering results

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]
The ORDER BY clause
The ORDER BY clause list the result in sorted order.
Query: List the names of all customers in alphabetic order.
Customer
customer_id name address name SELECT name
Ben
C1 Kit CB320 FROM Customer
C2 Ben CB326 Diana
Kit
ORDER BY name ASC;
C3 Diana CB311
C4 Tom CB415 Tom
Result

Use DESC for descending order, and ASC for ascending


order. Default: ascending name
Tom SELECT name
Kit FROM Customer
Diana ORDER BY name DESC;
Ben
49
Result
The ORDER BY clause
The ORDER BY clause list the result in sorted order.
Query: List the loan records in ascending order of the
branch_id, if two tuples having the same branch_id,
order by their loan amount in descending order.
Loan
branch_id loan_id Amount branch_id loan_id Amount
branch_id loan_id amount
B3 L1 900 B1 L3 1000 B1 L2 1500

B1 L3 1000 B1 L5 1500 B1 L3 1000

B1 L5 1500 B3 L1 900 B3 L1 900

Intermediate Result Final Result

SELECT *
FROM Loan
ORDER BY branch_id ASC,
amount DESC;
50
Section 7

Simple Nested
Query

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]
The IN clause
The IN clause allows you to specify discrete values in
the WHERE search criteria.
Query: Find the customer_id of all customers who
have both an account and a loan.
Borrower Owner
customer_id loan_id account_id customer_id customer_id
C1 L3 A1 C1 C1
C4 L2 A1 C2 C2
C2 L1 A2 C2
Result

SELECT DISTINCT customer_id


The result of
FROM Borrower this sub-query
WHERE customer_id IN is {C1,C2,C2}.
(SELECT customer_id FROM Owner); 52
The IN clause
The IN clause allows you to specify discrete values in
the WHERE search criteria.
Query: Find the customer_id of all customers who
have a lone but not having an account.
Borrower Owner
customer_id loan_id account_id customer_id customer_id
C1 L3 A1 C1 C4
C4 L2 A1 C2
C2 L1 A2 C2 Result

SELECT DISTINCT customer_id


The result of
FROM Borrower this sub-query
WHERE customer_id NOT IN is {C1,C2,C2}.
(SELECT customer_id FROM Owner); 53
Section 8

Aggregation

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]
Aggregate functions
Aggregation functions take a collection of values as
input and return a single value..

Query: Find the average balance of all accounts at


the branch with branch_id 'B2'.
Account
branch_id account_id balance AVG (balance)
B1 A1 500 650.0000
B2 A2 400
Result
B2 A3 900
B1 A4 700

SELECT AVG(balance)
FROM Account
WHERE branch_id = 'B2'; 55
Aggregate functions
Aggregation functions.
AVG
MIN
MAX
SUM
COUNT

56
The GROUP BY clause
Aggregation function can be applied to a group of sets
of tuples by using GROUP BY clause.
Query: Find the average balance at each branch.

Step1. Grouping
GROUP BY branch_id
Account
branch_id account_id balance branch_id account_id balance
B1 A1 500 A1 500
B1
B2 A2 400 A4 700
B2 A3 900 A2 400
B2
B1 A4 700 A3 900 57
The GROUP BY clause
Aggregation function can be applied to a group of sets
of tuples by using GROUP BY clause.
Query: Find the average balance at each branch.
SELECT branch_id, AVG(balance)
FROM Account
GROUP BY branch_id;

Step1. Grouping Step2. Aggregation


GROUP BY branch_id AVG(balance)
Account
branch_id account_id balance branch_id account_id balance branch_id AVG (balance)
B1 A1 500 A1 500 B1 600.0000
B1
B2 A2 400 A4 700 B2 650.0000
B2 A3 900 A2 400
B2 Result
B1 A4 700 A3 900 58
The HAVING clause
It is useful to state a condition that applies to groups
rather than to tuples.
Query: Find the branches where the average account
balance is no less than $650.
SELECT branch_id, AVG(balance) branch_id AVG (balance)
FROM Account B2 650.0000

GROUP BY branch_id Result


HAVING AVG(balance) >= 650; Step3. Filtering
(Having AVG(balance) >= 650)
Account
branch_id account_id balance branch_id account_id balance branch_id AVG (balance)
B1 A1 500 A1 500 B1 600.0000
B1
B2 A2 400 A4 700 B2 650.0000
B2 A3 900 A2 400
B2
B1 A4 700 A3 900 59
Section 9

Join
Slides prepared by - Dr. Chui Chun Kit for students in COMP3278
For other uses, please email : [email protected]
Join
A join takes 2 tables as input and returns a table.
Employee Department
e_name department_id department_id d_name
Kit 31 31 CS Cartesian product, then
33 Civil
E.department_id = D.department_id
Ben 33
John 33 34 ME

Diana 34 35 EEE

Tom 34
David NULL

SELECT * e_name E.department_id D.department_id d_name

FROM Employee E, Department D Kit 31 31 CS

WHERE E.department_id = Ben 33 33 Civil

D.department_id; John 33 33 Civil


Diana 34 34 ME
Tom 34 34 ME

Result 61
The OUTER JOIN clause
An outer join does not require each record in the two
joined tables to have a matching record.
Employee Department
e_name department_id department_id d_name
Even if the LEFT table record does not
have matching records in the RIGHT
Kit 31 31 CS
table, we still output the tuple in the
Ben 33 33 Civil
LEFT table (with null values for the
John 33 34 ME columns of the RIGHT table).
Diana 34 35 EEE

Tom 34
David NULL

e_name E.department_id D.department_id d_name

SELECT * Kit 31 31 CS
Ben 33 33 Civil
FROM Employee E LEFT OUTER JOIN
John 33 33 Civil
Department D
Diana 34 34 ME
ON E.department_id =
Tom 34 34 ME
D.department_id; David NULL NULL NULL
Result 62
The OUTER JOIN clause
An outer join does not require each record in the two
joined tables to have a matching record.
Employee Department
e_name department_id department_id d_name
Even if the RIGHT table record does
not have matching records in the LEFT
Kit 31 31 CS
table, we still output the tuple in the
Ben 33 33 Civil
RIGHT table (with null values for the
John 33 34 ME columns of the LEFT table).
Diana 34 35 EEE

Tom 34
David NULL

e_name E.department_id D.department_id d_name

SELECT * Kit 31 31 CS
Ben 33 33 Civil
FROM Employee E RIGHT OUTER JOIN
John 33 33 Civil
Department D
Diana 34 34 ME
ON E.department_id =
Tom 34 34 ME
D.department_id; NULL NULL 35 EEE
Result 63
Chapter 3A.

END
COMP3278 Introduction to
Database Management Systems

Slides prepared by - Dr. Chui Chun Kit for students in COMP3278


For other uses, please email : [email protected]

You might also like