Lecture 3 - Structural Query Language (SQL) - Part I PDF
Lecture 3 - Structural Query Language (SQL) - Part I PDF
Database Systems
Unit 3 :
Structural Query
Language
(SQL) Part 1
Unit 3 :
Structural Query Language (SQL)
Topic 3
Objective :
At the end of this unit, you should be able to:
Use SQL to query data from tables
Use SQL statements with aggregate
functions
Design SQL statement with parameters
section
Topic 3
Content
SQL statements; including SELECT, FROM,
WHERE, GROUP BY, HAVING and ORDER BY.
SQL Aggregate Functions; including COUNT (),
AVG (), SUM(), MAX(), MIN(), CONCAT () etc
SQL statements beginning with a
PARAMETERS keyword followed by a list of
parameter identifier(s) to add a Parameters
section to the SQL statement.
IT1768 Database Systems
Topic 3
Topic 3
Overview of SQL
Data Definition Language (DDL)
CREATE, DROP and ALTER of relational tables
Data control language (DCL)
GRANT and REVOKE
Topic 3
Applications of SQL
What can
SQL do ?
Topic 3
SELECT
[DISTINCT] column_list
FROM
table_list
WHERE [condition]
GROUP BY [column_list]
HAVING [condition]
ORDER BY [column_list [DESC]]
Only SELECT & FROM are mandatory in a SQL statement
Other clauses are optional, depending on the conditions for use.
Order of the clauses cannot be changed
Topic 3
FROM
WHERE
GROUP BY
HAVING
ORDER BY
Topic 3
SELECTFROM Statement
The SELECT Statement
SELECT is used to retrieve and display data from one or
more tables
FROM Specifies table(s) to be used
Customer
CustNum Fname
SQL Syntax:
SELECT [column_list]
FROM [table_list]
SELECT CustNum
FROM Customer
Lname
Address
zipcode
C-01
Hillary
Clinton
1 Street 1
010-12-345
C-02
John
Smith
2 Street 10
022-23-561
(Customer)
Topic 3
Address
Zipcode
C-01
Hillary
Clinton
1 Street 1
010-12-345
C-02
John
Smith
2 Street 10
022-23-561
10
Topic 3
SELECT Module
FROM StudMod
Example 2 :
SELECT Distinct Module
FROM StudMod
SID
Module
Module
Module
S01
Maths
Maths
Maths
S02
Accounting
Accounting
Accounting
S03
Physics
Physics
Physics
S04
Physics
Physics
Chemistry
S05
Accounting
Accounting
S06
Chemistry
Chemistry
Note :
Repeated rows
Note : DISTINCT
Removed
duplicated rows
11
Topic 3
12
Topic 3
Syntax :
SELECT [column_list], [SQL function]
FROM [table_list]
Reference:
http://www.1keydata.com/sql/sql-concatenate.html
http://www.w3schools.com/sql/
13
Topic 3
11
In MS Access, use:
Example 6 :
SELECT
CONCAT( Fname, , Lname ) AS EmpName
FROM
EMPLOYEE
WHERE
Designation=Mgr
Fname Lname Designation
Aaron
Ang
Mgr
EmpName
Bruce
Lee
Accountant
Aaron Ang
Charlie Chan
QA
Eileen Er
Don
Ho
Designer
Eileen
Er
Mgr
14
Topic 3
From Customer ;
Customer
custID area
zipcode
zipcode
C-01
010-123
010
C-02
012-398
012
C-03
024-629
024
Reference:
http://www.sql-ref.com/
15
Topic 3
Syntax :
WHERE [column_name] <operator> [value(s)] ;
16
Topic 3
Topic 3
18
Topic 3
Example 1:
SELECT SName, Age
FROM
STUDENT
WHERE Age BETWEEN 15 AND 19
SID
SName
Major
Age
100
Jones
Accounting
21
150
Parks
Math
19
200
Baker
History
16
250
Glass
History
18
300
Andy
Accounting
17
SName
Age
Parks
19
Baker
16
Glass
18
Andy
17
19
Topic 3
http://www.techonthenet.com/sql/like.php
Example 2:
SELECT SName, Age
FROM
STUDENT
WHERE SName LIKE %a%
SID
SName
Major
Age
100
Jones
Accounting
21
150
Parks
Math
19
200
Baker
History
16
250
Glass
History
18
300
Andy
Accounting
17
SName
Age
Parks
19
Baker
16
Glass
18
a*
The % sign functions as a wildcard. It signifies any character. Thus, a%" means all strings
that begin with the alphabet a. %s" means all strings that end with the alphabet s.
IT1768 Database Systems
*s
20
Topic 3
Compare with:
SELECT SName, Age
FROM STUDENT
WHERE Major = Math ;
Example 3:
SELECT SName, Age
FROM
STUDENT
WHERE Major IN (Math, Accounting)
SID
SName
Major
Age
100
Jones
Accounting
21
150
Parks
Math
19
200
Baker
History
16
250
Glass
History
18
300
Andy
Accounting
17
SName
Age
Jones
19
Parks
16
Andy
18
The IN operator in the Where clause can accommodate more than one selected values;
like the case above, it accommodates Math and Accounting in the selection. In
21
contrast,
the = operator can accommodate only one value in the selection.
IT1768 Database
Systems
Topic 3
Syntax :
Order by column_list [desc]
where column_list is:
a column name in the select clause; or
a column number
Example 1:
SELECT Ename
FROM EMPLOYEE
ORDER BY Ename Desc
Order by 1, 2 desc
Order by 1 desc, 2
IT1768 Database Systems
Example 2:
SELECT Ename, Salary
FROM EMPLOYEE
ORDER BY 2 Desc
22
Topic 3
Examples :
Sort in descending order of ZIPCODE :
SELECT *
FROM CUSTOMER
ORDER BY ZIPCODE DESC ;
Sort in ascending order of LNAME
SELECT ZIPCODE, LNAME, FNAME
FROM CUSTOMER
(Or ORDER BY 2 Asc)
ORDER BY 2 ;
Note: If unspecified, it is
defaulted as Asc
23
Topic 3
Example 1 :
SELECT
FROM
WHERE
ORDER BY
SID
SName
Major
Age
SName
Major
Age
100
Jones
Accounting
21
Baker
History
16
150
Parks
Math
19
Glass
History
18
200
Baker
History
16
250
Glass
History
18
300
Andy
Accounting
17
24
Topic 3
Example 2 :
SELECT
FROM
WHERE
ORDER BY
SID
SName
Major
Age
SName
Major
Age
100
Jones
Accounting
21
Jones
Accounting
21
150
Parks
Math
19
Andy
Accounting
17
200
Baker
History
16
Glass
History
18
250
Glass
History
18
Baker
History
16
300
Andy
Accounting
17
25
Topic 3
custNum from
Orders table
26
Topic 3
Multiple
table query :
- Select CustName, OrderNo, OrderDate, PaidDate
From
Customer As c, Orders As o
Where c.custNum = o.custNo
o
o
27
Reference 1: http://sqlzoo.net/
Reference 2: http://www.sql-tutorial.net/
Topic 3
(Joining tables) 3
Orders
CustName
CustNo
OrderNo
OrderDate
1000
1000
01/03/2008
1001
1000
02/03/2008
1002
1001
03/03/2008
1003
1002
04/03/2008
1004
1004
05/03/2008
PaidDate
28/03/2008
30/03/2008
CustName
o.CustNo
OrderNo
OrderDate
1000
1000
01/03/2008
1000
1000
02/03/2008
1001
1001
03/03/2008
1002
1002
04/03/2008
1004
1004
05/03/2008
(The result)
PaidDate
28/03/2008
30/03/2008
28
Topic 3
http://www.w3schools.com/sql/sql_functions.asp
29
Topic 3
Returns
COUNT(*)
COUNT(DISTINCT
column-name)
AVG
AVG (column-name)
SUM
MIN
MIN (column-name)
MAX
ABS
ABS(column-name)
Topic 3
COUNT()
STUDENT
SID
SName
Major
Age
100
Jones
Accounting
21
150
Parks
Math
19
200
Baker
History
16
250
Glass
History
18
300
Andy
Accounting
17
31
Topic 3
Example 2 :
SELECT
FROM
COUNT(Major)
STUDENT
5
Will it return 5 ?
At least it does not works in MS Access.
COUNT(DISTINCT Major)
STUDENT
COUNT(*)
(Select DISTINCT Major From STUDENT);
3
32
Topic 3
GROUP BY SName
WARNING 2 :
Typically, built-in functions cannot be used as part of
the WHERE clause
Built-in functions can be applied to groups of rows
within a table
For example, students can be grouped by Major,
meaning one group will be formed for each value of
Major
IT1768 Database Systems
33
Topic 3
SELECT
Major, COUNT( ) As SNumber
FROM
STUDENT
GROUP BY Major
SID
SName
Major
Age
100
Jones
Accounting
21
150
Parks
Math
19
200
Baker
History
16
250
Glass
History
18
300
Andy
Accounting
17
Major
SNumber
Accounting
History
Math
34
Topic 3
Example:
SELECT
FROM
GROUP BY
HAVING
SID
SName
Major
Age
100
Jones
Accounting
21
150
Parks
Math
19
200
Baker
History
16
250
Glass
History
18
300
Andy
Accounting
17
Major
SNumber
Accounting
History
35
Topic 3
The SQL AVG aggregate function selects the average value for
certain table column.
SELECT
FROM
WHERE
GROUP BY
HAVING
SID
SName
Major
Age
100
Jones
Accounting
21
150
Parks
Math
19
200
Baker
History
16
250
Glass
History
18
300
Andy
Accounting
17
Major
History
AVG _Age)
17
Topic 3
SName
Salary
Age
D1
Jones
2000
21
D1
Parks
4000
19
D1
Baker
3000
16
D2
Glass
5000
18
D2
Andy
3500
17
Dep
Highest_Sal
D1
4000
D2
5000
Topic 3
10
The SQL SUM aggregate function allows selecting the total for a
numeric column.
SName
Salary
Age
D1
Jones
2000
21
D1
Parks
4000
19
D1
Baker
3000
16
D2
Glass
5000
18
D2
Andy
3500
17
Dep
SUM_Pay
D1
9000
D2
8500
Topic 3
Example 1:
PARAMETERS *Choose Employees Department:+ Text ;
SELECT
Ename, Department
FROM
EMPLOYEES
WHERE
Department= *Choose Employees Department:+ ;
EID
Ename
Salary Department
E-01
Robert
2,500
QA
E-02
Kelly
3,500
Accounting
E-03
Mohd
4,200
Design
E-04
Chandra
6,500
Production
E-05
Jasmine
3,800
Sales
Ename
Department
Robert
QA
39
Topic 3
40
Topic 3
Example 2:
PARAMETERS *Indicate Employees Salary :+ Number ;
SELECT
Ename, Salary
FROM
EMPLOYEES
WHERE
Salary= *Indicate Employees Salary :+ ;
EID
Ename
Salary Department
E-01
Robert
2,500
QA
E-02
Kelly
3,500
Accounting
E-03
Mohd
4,200
Design
E-04
Chandra
6,500
Production
E-05
Jasmine
3,800
Sales
Ename
Salary
Chandra
6,500
41
Topic 3
Summary
SQL Syntax
SELECT .. FROM are mandatory clauses in SQL
statements
WHERE, GROUP BY, HAVING, ORDER BY are optional
clauses in SQL statements.
42
Topic 3
EXERCISE REVIEW 1
Match the function of the SELECT statement to the correct
descriptions.
HAVING
FROM
ORDER BY
SELECT
GROUP BY
WHERE
43
Topic 3
Quiz
Give an example of using GROUP BY clause.
When do we need to use the HAVING clause?
Which clause shall be used if the result displayed
is arranged in descending?
Arrange in order in SQL statement using Having,
Group by , Order By, Select, Where, From.
IT1768 Database Systems
44
Topic 3
EXERCISE REVIEW 2
From Table : SalesInfor. Write a SQL statement to display the
following:
a) Sales between $2,000 and $3,000
b) All Sales in descending order
c) Total sales for each branch
d) The branch with sales over $4,000
Branch
Sales
Promoter
Orchard
3500
Aaron
Bedok
1500
Bruce
Orchard
2000
Charlie
JurongP
800
Don
NorthP
1750
Eileen
JurongP
3000
Francis
45
Topic 3
EXERCISE REVIEW 3
From Table : Sales. Write a SQL statement to display
the following:
a) All customers and their sub-total sum
b) The highest transaction
ItemNo
Unit_price
Qty
Customer
250
Aaron
500
Bruce
2000
Charlie
800
Don
750
Eileen
3000
Francis
46
Topic 3
EXERCISE REVIEW 1
(Solution)
FROM
ORDER BY
SELECT
GROUP BY
WHERE
47
Topic 3
EXERCISE REVIEW 2
a)
b)
c)
d)
a) Solution
Branch
Sales
Promoter
Orchard
3500
Aaron
Orchard
2000
Charlie
Branch
Sales
Promoter
Orchard
3500
Aaron
Bedok
1500
Bruce
Orchard
2000
Charlie
JurongP
800
Don
NorthP
1750
Eileen
JurongP
3000
Francis
c) Solution
b) Solution
(Solution)
Branch
Sales
Orchard
3500
JurongP
3000
Orchard
2000
NorthP
1750
Bedok
1500
JurongP
800
d) Solution
JurongP
3800
NorthP
1750
Sub-total
5500
48
Topic 3
EXERCISE REVIEW 3
(Solution)
ItemNo
a) Solution
Unit_price
Qty
Customer
250
Aaron
500
Bruce
2000
Charlie
800
Don
750
Eileen
3000
Francis
SELECT
Customer, Unit_price * Qty As Sub-total
FROM
Sales
GROUP BY Customer
b) Solution
SELECT MAX (Unit_price * Qty ) As Highest
FROM
Sales
For b), how to display the customer with the highest transaction ?
IT1768 Database Systems
49
Topic 3
Quiz
(Solution)
4)
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
50