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

CPS405 ch04

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

CPS405 ch04

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

Federal University Gashua

Department of Computer Science

CHAPTER THREE: SQL Statements

(Fundamentals of SQL Statement)

USMAN SULEIMAN IDRISS


Capabilities of SQL SELECT
Statements
Restriction Projection

Table 1 Table 1
Join

Table 1 Table 2

2
Basic SELECT statement syntax

SELECT [DISTINCT] {*, column AS[alias],...}


FROM table
[WHERE condition(s)]
[GROUP BY group_by_expression]
[ORDER BY column];

◦◦ 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 :

◦ which columns of information you want


◦ what order you want them in
◦ what you want them to be called.

5
SELECTING fields
 Select All Fields

SELECT *
FROM course;
Output:
CourseDesp MentorID
CourseID
DCS Diploma In Computer Studies 1006

DGAT Diploma in Gaming and Animation Techniques 1004

DIC Diploma In Computing 1006

DICT Diploma In Info-Comm Technology 1002

DIT Diploma In Information Technology 1001

DNC Diploma in Network and CyberSecurity 1003

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:

Lastname DateEnrolled Expr1002


Bartell 15-Feb-02 3.26

Kebel 23-Jun-01 2.76


Note: The column name
Lee 05-Jan-02 3.87
after applying arithmetic
Lewis 03-Mar-00 2.56

Law 01-Apr-01 3.1

...

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

SELECT LastName, DateEnrolled, GPA * 4 + 0.05


FROM student;

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

all of its values and list each of them only


once.

22
Eliminating Duplicate
Rows(2)
SELECT Distinct CourseID
FROM student;
Output:
CourseID
DCS
DIC
DICT
DIT
DNC

23
Limiting Rows Using
Restriction
Objectives

◦Limit the rows retrieved by a query

◦Sort the rows retrieved by a query


Limiting Rows Using
Restriction
LastName GPA CourseID
Retrieve only those
Bartell 3.21 DCS enrolled in DCS
Kebel 2.71 DIC course
Lee 3.82 DIT
Lewis 2.51 DICT

Law 3.05 DIT

Mikulski 1.89 DCS

Tham 3.89 DCS


LastName GPA CourseID
Faga 2.22 DIC
Bartell 3.21 DCS
Nicosia 3.11 DICT Mikulski 1.89 DCS
Owen 3.34 DIC Tham 3.89 DCS
Williams 2.74 DCS
……..
The WHERE clause
SELECT [DISTINCT] {*, column AS [alias],...}
FROM table
[WHERE condition(s)]

• WHERE clause to restrict the rows returned from the


query.
• A WHERE clause contains a condition that must be met
• Write the WHERE clause follows the FROM clause
• The WHERE clause consists of three elements:
 Column name
 Comparison operator
 Column name, constant, or list of values
Using the WHERE clause
SELECT LastName, GPA, CourseID
FROM student
WHERE CourseID = 'DCS';
Output

LastName GPA CourseID


Bartell 3.21 DCS

Mikulski 1.89 DCS

Tham 3.89 DCS

Williams 2.74 DCS


Restricting data that are
Character String and Date
 Character strings in the WHERE clause must
be enclosed in single quotation marks (' ')
or double quotation marks (“ ”).
 Dates must enclosed in pound signs (# #)
 Number constants, however, must not.
 All character searches are non-case

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

WHERE Gender = “F”;


double-quotation

Note : using ‘f’ or ‘F’ will also return the same result
Comparison Operators

Operator Description

= Equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

<> Not equal to


Using Comparison
Operator
SELECT StudID, GPA
FROM student
WHERE GPA > 2.9;
Output :
StudID GPA
S001 3.21
S003 3.82
S005 3.05
S007 3.89
S009 3.11
S010 3.34
S012 3
S015 3.12
S016 3.76
Other
Other SQL
SQL Comparison
Comparison Operators
Operators

Operator Meaning

BETWEEN Between two values (inclusive)


...AND...

IN(list) Match any of a list of specific values

LIKE Match a character pattern

IS NULL Is a null value (find nulls)


BETWEEN..AND.. operator
SELECT LastName, FirstName, DateEnrolled
FROM student
WHERE DateEnrolled Between #01-Jan-2001# And #31-Dec-2001# ;

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

parenthesis as a comma-delimited list.


 All values must have the same data type
IN operator (example)
SELECT LastName, FirstName, CourseID
FROM student
WHERE CourseID IN (‘DCS’, ‘DIC’);

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

* (asterisk) A string of characters of any length

? (question mark) One character

# (pound sign) One digit (numeric character)

[b-f] (square brackets with a Range of characters


dash)
[!b-f] Outside a range of characters

[*] , [?], [#] Putting a character in square brackets means to


take it literally, rather than using as a wildcard
character.
LIKE operator
Examples of wildcard patterns in Access
Pattern Wildcard Pattern Examples
Text string beginning with a ‘s’ ‘s*’ ‘start’
‘stop’
‘Smith’

Four characters ending with an ‘???e’ ‘none’


‘e’ ‘123e’

Starting with a letter between ‘[a-c]##’ ‘a12’


‘a’ and ‘c’ followed by two ‘c33’
digits
LIKE operator (example)
SELECT LastName, FirstName, CourseID
FROM student
WHERE LastName Like ‘Le*’;

Output :

LastName FirstName CourseID


Lee Choy Yan DIT

Lewis Derrick H. DICT


IS NULL operator
 IS NULL operator only can be used to
retrieve NULL values in a table.

SELECT LastName, FirstName


FROM student
WHERE GroupLeader IS NULL;
Output :
LastName FirstName
Law Arthur
Tham Tian En
Owen John M.
Jann How
Logical Operators

Operator Meaning

AND Returns TRUE if both component


conditions are TRUE
OR Returns TRUE if either component
condition is TRUE

NOT Returns TRUE if the following


condition is FALSE
Using AND Operator
AND requires both conditions to be TRUE.

SELECT FirstName, Gender, GPA


FROM student
WHERE Gender = ‘F’ AND GPA > 3;
Output :

FirstName Gender GPA


Choy Yan F 3.82
Tian En F 3.89
Xi Xi F 3.12
How F 3.76
Using OR operator
 OR requires either condition to be TRUE

SELECT FirstName, DateEnrolled,CourseID


FROM student
WHERE DateEnrolled > #01-MAY-2003# OR CourseID = 'DICT';

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,

BETWEEN, LIKE and IS NULL.


 Example:
 …. WHERE CourseID NOT IN (‘DCS’, ‘DICT’);
 …. WHERE GPA NOT BETWEEN 2.0 AND 3.0;
 …. WHERE MentorName NOT LIKE ‘C*’;
 …. WHERE GroupLeader IS NOT NULL
Rules of Precedence

Order Evaluated Operator


1 All comparison
operators
2 NOT
3 AND
4 OR

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 :

FirstName GroupLeader GPA

Choy Yan S016 3.82


Xi Xi S016 3.12
ORDER BY clause
 The ORDER BY clause determines how the
rows of the result table are sorted
 ORDER BY clause must be placed at the last

clause in the SELECT statement.


 The default sorting order is ascending.
 Sort order options for each column
 asc - means ascending order
 desc - means descending order
Sorting Data
 Sorting the rows byQuery1
single column
SELECT LastName, FirstName
FROM student
ORDER By LastName;
Output : LastName FirstName
Bartell Joseph P.
Chan Xi Xi
Faga Mark J.
Ascending Jann How
order Kebel Laura N.
Law Arthur
Lee Choy Yan
Lewis Derrick H.
Maser Jennifer L.
Mikulski Kathleen
……
Sorting DataQuery1 (several
columns)
SELECT GroupLeader, LastName, Output :
FirstName
FROM student GroupLeader LastName FirstName
Jann How
ORDER By GroupLeader, LastName;
Law Arthur
1 2 2 Owen John M.
Tham Tian En
S005 Bartell Joseph P.
S005 2 Nicosia Anthony L.
S005 Roche Stephanie N.

1 S007 Kebel Laura N.


S007 2 Lewis Derrick H.
S007 Ng Jian Xian
S010 Faga Mark J.
S010 2 Mikulski Kathleen
S010 Williams Jason R.
…….
Sorting Data (in
descending)
SELECT MentorID, MentorName
FROM Mentor
Order By MentorNameQuery1
Desc;
Output :
MentorID MentorName
1004 Schubert
1002 Rimes
1005 Norman
Descending
1001 Goile
order
1003 Christopher
1006 Carroll
Sorting Data (various
orders) Query1
CourseID LastName GPA
DCS Tham 3.89
DCS Bartell 3.21
DCS Williams 2.74
SELECT CourseID, LastName,
DCS Mikulski 1.89
GPA DIC Owen 3.34
FROM Student DIC Chan 3.12
Order By CourseID Asc, DIC Kebel 2.71

GPA Desc ; DIC Faga 2.22


DICT Nicosia 3.11
DICT Maser 3
DICT Lewis 2.51
DIT Lee 3.82
DIT Law 3.05
DIT Ng 1.88
DIT Roche 1.88
DNC Jann 3.76
 END

 ANY QUESTION

You might also like