CPS405 ch04
CPS405 ch04
Table 1 Table 1
Join
Table 1 Table 2
2
Basic SELECT statement syntax
◦◦ SELECT
SELECT identifies
identifies the
the columns
columns toto be
be
displayed.
displayed.
◦◦ FROM
FROM identifies
identifies the
the table
table that
that contains
contains the
the
columns.
columns.
3
Writing SQL Statements
◦◦ Place
Place aa semicolon
semicolon (;)
(;) at
at the
the end
end of
of the
the last
last clause.
clause.
◦◦ SQL
SQL statements
statements are
are not
not case
case sensitive.
sensitive.
◦◦ SQL
SQL statements
statements can
can be
be on
on one
one or
or more
more lines.
lines.
◦◦ Keywords
Keywords cannot
cannot be
be abbreviated
abbreviated or
or split
split across
across lines.
lines.
◦◦ Clauses
Clauses are
are usually
usually placed
placed on
on separate
separate lines.
lines.
4
SELECT clause
The SELECT clause is the first part of a
query.
The SELECT clause says :
5
SELECTING fields
Select All Fields
SELECT *
FROM course;
Output:
CourseDesp MentorID
CourseID
DCS Diploma In Computer Studies 1006
6
SELECTING fields
Select One Field ( Projection)
SELECT CourseDesp
FROM course;
Output:
CourseDesp
Diploma In Computer Studies
Diploma in Gaming and Animation Techniques
Diploma In Computing
Diploma In Info-Comm Technology
Diploma In Information Technology
Diploma in Network and CyberSecurity
7
SELECTING fields
Select More Than One Field
SELECT LastName, GPA
FROM course;
Output:
LastName GPA
Bartell 3.21
• Take note on the ordering
Kebel 2.71
Lee 3.82 of the column
Lewis 2.51
Law 3.05
Mikulski 1.89
.
.
Chan 3.12
Jann 3.76
8
Arithmetic Expressions
Create
Create expressions
expressions on
on NUMBER
NUMBER and
and DATE
DATE
data
data types
types by
by using
using arithmetic
arithmetic operators.
operators.
Operator Description
+ Add
- Subtract
* Multiply
/ Divide
9
Using Arithmetic
Operators
SELECT LastName, DateEnrolled, GPA + 0.05
FROM emp;
Output:
...
10
Operator Precedence
_
* / +
◦◦ Multiplication
Multiplication andand division
division take
take priority
priority over
over
addition
addition and
and subtraction.
subtraction.
◦◦ Operators
Operators of of the
the same
same priority
priority are
are evaluated
evaluated
from
from left
left to
to right.
right.
◦◦ Parentheses
Parentheses are are used
used to
to force
force prioritized
prioritized
evaluation
evaluation andand to
to clarify
clarify statements.
statements.
11
Operator Precedence
Output:
LastName DateEnrolled Expr1002
Bartell 15-Feb-02 12.89
Kebel 23-Jun-01 10.89
Lee 05-Jan-02 15.33
Lewis 03-Mar-00 10.09
Law 01-Apr-01 12.25
Mikulski 12-Sep-03 7.61
Tham 19-Sep-03 15.61
…..
12
Using Parenthesis
SELECT LastName, DateEnrolled, (GPA + 0.05) * 4
FROM student;
Output:
LastName DateEnrolled Expr1002
Bartell 15-Feb-02 13.04
Kebel 23-Jun-01 11.04
Lee 05-Jan-02 15.48
Lewis 03-Mar-00 10.24
Law 01-Apr-01 12.4
Mikulski 12-Sep-03 7.76
Tham 19-Sep-03 15.76
……
13
Defining a Column Alias
◦◦ Renames
Renames aa column
column heading
heading
◦◦ Is
Is useful
useful with
with calculations
calculations
◦◦ Immediately
Immediately follows
follows column
column name;
name; MUST MUST
have
have ASAS keyword
keyword between
between column
column name name andand
alias
alias
◦◦ Requires
Requires square
square brackets
brackets (( [[ ]] )) ifif it
it is
is contains
contains
spaces
spaces or or special
special characters
characters
◦◦ The
The alias
alias case
case will
will follow
follow exactly
exactly what what is is
entered.
entered.
14
Using Column Aliases
Example 1
SELECT LastName, DateEnrolled, GPA + 0.05 AS NEWGPA
FROM student;
Output:
LastName DateEnrolled NEWGPA
Bartell 15-Feb-02 3.26
Kebel 23-Jun-01 2.76
Lee 05-Jan-02 3.87
Lewis 03-Mar-00 2.56
Law 01-Apr-01 3.1
Mikulski 12-Sep-03 1.94
Tham 19-Sep-03 3.94
…….
15
Using Column Aliases
Example 2
SELECT LastName AS [Family Name], DateEnrolled,
GPA + 0.05 AS [New GPA]
FROM student;
Output: Family Name DateEnrolled New GPA
Bartell 15-Feb-02 3.26
Kebel 23-Jun-01 2.76
Lee 05-Jan-02 3.87
Lewis 03-Mar-00 2.56
Law 01-Apr-01 3.1
Mikulski 12-Sep-03 1.94
Tham 19-Sep-03 3.94
…….
16
Concatenation Operator
◦◦ Concatenates
Concatenates columns
columns or or character
character strings
strings to
to
other
other columns
columns
◦◦ Is
Is represented
represented by by anan ampersand
ampersand & &
◦◦ Creates
Creates aa result
result column
column that
that is
is aa character
character
expression
expression
◦◦ As
As aa result
result is
is will
will join
join one
one or
or more
more column
column
become
become aa single-column
single-column
17
Using Concatenation
Operator
SELECT LastName & GPA AS [Student GPA]
FROM student;
Output:
Student GPA
Bartell3.21
Kebel2.71
Lee3.82
Lewis2.51
Law3.05
Mikulski1.89
Tham3.89
…..
18
Literals
◦◦ A
A literal
literal is
is aa constant
constant value
value of
of character,
character,
expression,
expression, or or number
number that that can
can be
be included
included in
in
the
the SELECT
SELECT list.
list.
◦◦ Date
Date and
and character
character literal
literal values
values must
must bebe
enclosed
enclosed in in single
single quotation
quotation marks.
marks.
◦◦ Each
Each character
character string
string is
is output
output once
once for
for each
each
row
row returned.
returned.
19
Using Literal Character
String
SELECT LastName & ' earns ' & GPA & ' points ' AS
[Student GPA]
FROM student;
Output:
Student GPA
Bartell earns 3.21 points
Kebel earns 2.71 points
Lee earns 3.82 points
Lewis earns 2.51 points
Law earns 3.05 points
Mikulski earns 1.89 points
Tham earns 3.89 points
……
20
Duplicate Rows
SELECT CourseID
FROM student;
Output: CourseID
DCS
DIC
DIT
DICT
DIT Duplicate
DCS
rows
DCS
DIC
DICT
DIC
DIT
DICT
DIT
DCS
DIC
DNC
21
Eliminating Duplicate
Rows(1)
Eliminate duplicate rows by using the
DISTINCT keyword in the SELECT clause.
Only can be applied to one column to find
22
Eliminating Duplicate
Rows(2)
SELECT Distinct CourseID
FROM student;
Output:
CourseID
DCS
DIC
DICT
DIT
DNC
23
Limiting Rows Using
Restriction
Objectives
sensitive.
Restricting data that are
Character String and Date
Single-quotation
SELECT LastName, FirstName,
Gender
FROM student
WHERE Gender = ‘F’; LastName GPA CourseID
Bartell 3.21 DCS
or Mikulski 1.89 DCS
SELECT LastName, FirstName, Tham 3.89 DCS
Gender
FROM student Williams 2.74 DCS
Note : using ‘f’ or ‘F’ will also return the same result
Comparison Operators
Operator Description
= Equal to
Operator Meaning
Output :
Lower limit Upper limit
LastName FirstName DateEnrolled
Kebel Laura N. 23-Jun-01
Law Arthur 01-Apr-01 Note:
Dates must enclosed
Faga Mark J. 25-Jun-01
in pound signs (# #)
Owen John M. 17-Sep-01
Ng Jian Xian 01-Apr-01
Jann How 01-Apr-01
IN operator
The IN operator is used when there is a list
of discrete values that satisfy the condition.
The set of all these valid values is placed in
Output :
LastName FirstName CourseID
Bartell Joseph P. DCS
Kebel Laura N. DIC
Mikulski Kathleen DCS
Tham Tian En DCS
Faga Mark J. DIC
Owen John M. DIC
Williams Jason R. DCS
Chan Xi Xi DIC
LIKE operator
The LIKE operator is used for finding patterns
in the data.
Patterns are specified using wildcard
characters
Wildcard characters Meaning
Output :
Operator Meaning
Output :
FirstName DateEnrolled CourseID
Derrick H. 03-Mar-00 DICT
Kathleen 12-Sep-03 DCS
Tian En 19-Sep-03 DCS
Anthony L. 01-Feb-02 DICT
Jennifer L. 25-Sep-03 DICT
Using NOT operator
NOT is used to reverse the meaning of the
original conditions.
NOT usually use together with IN,
Useparentheses
Useparentheses to
to override
override rules
rules of
of
precedence.
precedence.
Rules of Precedence
SELECT FirstName, GroupLeader, GPA
FROM student
WHERE GroupLeader = 'S007' OR GroupLeader ='S016‘
AND GPA > 3;
Output :
FirstName GroupLeader GPA
Laura N. S007 2.71
Choy Yan S016 3.82
Derrick H. S007 2.51
Jian Xian S007 1.88
Xi Xi S016 3.12
Rules of Precedence
Using Parenthesis to change the precedence
SELECT FirstName, GroupLeader, GPA
FROM student
WHERE (GroupLeader = 'S007' OR GroupLeader ='S016‘)
AND GPA > 3;
Output :
ANY QUESTION