SlideShare a Scribd company logo
1 of 30
Chapter 4
Restricting and Sorting Data
Objectives
 Limit the rows retrieved by a query
 Sort the rows retrieved by a query
Limiting Rows Using Restriction
LastName GPA CourseID
Bartell 3.21 DCS
Kebel 2.71 DIC
Lee 3.82 DIT
Lewis 2.51 DICT
Law 3.05 DIT
Mikulski 1.89 DCS
Tham 3.89 DCS
Faga 2.22 DIC
Nicosia 3.11 DICT
Owen 3.34 DIC
……..
LastName GPA CourseID
Bartell 3.21 DCS
Mikulski 1.89 DCS
Tham 3.89 DCS
Williams 2.74 DCS
Retrieve only those
enrolled in DCS
course
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
SELECT LastName, FirstName,
Gender
FROM student
WHERE Gender = ‘F’;
SELECT LastName, FirstName,
Gender
FROM student
WHERE Gender = “F”;
or
LastName GPA CourseID
Bartell 3.21 DCS
Mikulski 1.89 DCS
Tham 3.89 DCS
Williams 2.74 DCS
Note : using ‘f’ or ‘F’ will also return the same result
Single-quotation
double-quotation
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;
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
Output :
Other SQL Comparison
Operators
Operator
BETWEEN
...AND...
IN(list)
LIKE
IS NULL
Meaning
Between two values (inclusive)
Match any of a list of specific values
Match a character pattern
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 :
LastName FirstName DateEnrolled
Kebel Laura N. 23-Jun-01
Law Arthur 01-Apr-01
Faga Mark J. 25-Jun-01
Owen John M. 17-Sep-01
Ng Jian Xian 01-Apr-01
Jann How 01-Apr-01
Lower limit Upper limit
Note:
Dates must enclosed
in pound signs (# #)
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
dash)
Range of characters
[!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’
‘???e’ ‘none’
‘123e’
Starting with a letter between
‘a’ and ‘c’ followed by two
digits
‘[a-c]##’ ‘a12’
‘c33’
LIKE operator (example)
SELECT LastName, FirstName, CourseID
FROM student
WHERE LastName Like ‘Le*’;
LastName FirstName CourseID
Lee Choy Yan DIT
Lewis Derrick H. DICT
Output :
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
AND
OR
NOT
Meaning
Returns TRUE if both component
conditions are TRUE
Returns TRUE if either component
condition is TRUE
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
Use parentheses to override rules of
precedence.
Order Evaluated Operator
1 All comparison
operators
2 NOT
3 AND
4 OR
Rules of Precedence
SELECT FirstName, GroupLeader, GPA
FROM student
WHERE GroupLeader = 'S007' OR GroupLeader ='S016‘
AND GPA > 3;
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
Output :
Rules of Precedence
Using Parenthesis to change the precedence
FirstName GroupLeader GPA
Choy Yan S016 3.82
Xi Xi S016 3.12
SELECT FirstName, GroupLeader, GPA
FROM student
WHERE (GroupLeader = 'S007' OR GroupLeader ='S016‘)
AND GPA > 3;
Output :
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 by single column
SELECT LastName, FirstName
FROM student
ORDER By LastName;
Output :
Query1
LastName FirstName
Bartell Joseph P.
Chan Xi Xi
Faga Mark J.
Jann How
Kebel Laura N.
Law Arthur
Lee Choy Yan
Lewis Derrick H.
Maser Jennifer L.
Mikulski Kathleen
……
Ascending
order
Sorting Data (several columns)
SELECT GroupLeader, LastName,
FirstName
FROM student
ORDER By GroupLeader, LastName;
1 2
Output :
Query1
GroupLeader LastName FirstName
Jann How
Law Arthur
Owen John M.
Tham Tian En
S005 Bartell Joseph P.
S005 Nicosia Anthony L.
S005 Roche Stephanie N.
S007 Kebel Laura N.
S007 Lewis Derrick H.
S007 Ng Jian Xian
S010 Faga Mark J.
S010 Mikulski Kathleen
S010 Williams Jason R.
…….
1
2
2
2
2
Sorting Data (in descending)
Query1
MentorID MentorName
1004 Schubert
1002 Rimes
1005 Norman
1001 Goile
1003 Christopher
1006 Carroll
SELECT MentorID, MentorName
FROM Mentor
Order By MentorName Desc;
Output :
Descending
order
Sorting Data (various orders)
Query1
CourseID LastName GPA
DCS Tham 3.89
DCS Bartell 3.21
DCS Williams 2.74
DCS Mikulski 1.89
DIC Owen 3.34
DIC Chan 3.12
DIC Kebel 2.71
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
SELECT CourseID, LastName,
GPA
FROM Student
Order By CourseID Asc,
GPA Desc ;
Summary
 At the end of this chapter, you should
know how to use WHERE clause to
restrict data and also ORDER BY
clause to arrange data.
 The different types of operator used in
SQL.

More Related Content

Similar to Chapter-4.ppt

Sql intro
Sql introSql intro
Sql introglubox
 
SQL Keywords and Functions.pptx
SQL Keywords and Functions.pptxSQL Keywords and Functions.pptx
SQL Keywords and Functions.pptxRUBAB79
 
SQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTSSQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTSGagandeep Nanda
 
Sqlppt 120914120535-phpapp01
Sqlppt 120914120535-phpapp01Sqlppt 120914120535-phpapp01
Sqlppt 120914120535-phpapp01Ankit Dubey
 
5 surprising oracle sql behaviors that very few people know
5 surprising oracle sql behaviors that very few people know5 surprising oracle sql behaviors that very few people know
5 surprising oracle sql behaviors that very few people knowBoutros CHALOUHY
 
Structured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptxStructured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptxEllenGracePorras
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIMsKanchanaI
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptxEllenGracePorras
 
Mysql Statments
Mysql StatmentsMysql Statments
Mysql StatmentsSHC
 
How did i steal your database CSCamp2011
How did i steal your database CSCamp2011How did i steal your database CSCamp2011
How did i steal your database CSCamp2011Mostafa Siraj
 
Database object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentDatabase object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentArun Sial
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfMadhusha15
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptxANSHVAJPAI
 

Similar to Chapter-4.ppt (20)

0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
Chapter-9.ppt
Chapter-9.pptChapter-9.ppt
Chapter-9.ppt
 
Sql intro
Sql introSql intro
Sql intro
 
SQL Keywords and Functions.pptx
SQL Keywords and Functions.pptxSQL Keywords and Functions.pptx
SQL Keywords and Functions.pptx
 
unit-5 sql notes.docx
unit-5 sql notes.docxunit-5 sql notes.docx
unit-5 sql notes.docx
 
SQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTSSQL- MOST IMPORTANT CONCEPTS
SQL- MOST IMPORTANT CONCEPTS
 
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
 
Sqlppt 120914120535-phpapp01
Sqlppt 120914120535-phpapp01Sqlppt 120914120535-phpapp01
Sqlppt 120914120535-phpapp01
 
5 surprising oracle sql behaviors that very few people know
5 surprising oracle sql behaviors that very few people know5 surprising oracle sql behaviors that very few people know
5 surprising oracle sql behaviors that very few people know
 
Structured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptxStructured Query Language (SQL) Part 2.pptx
Structured Query Language (SQL) Part 2.pptx
 
Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Mysql Statments
Mysql StatmentsMysql Statments
Mysql Statments
 
How did i steal your database CSCamp2011
How did i steal your database CSCamp2011How did i steal your database CSCamp2011
How did i steal your database CSCamp2011
 
Database object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentDatabase object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab Assignment
 
Query
QueryQuery
Query
 
Module03
Module03Module03
Module03
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 

Recently uploaded

Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 

Recently uploaded (20)

Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Chapter-4.ppt

  • 2. Objectives  Limit the rows retrieved by a query  Sort the rows retrieved by a query
  • 3. Limiting Rows Using Restriction LastName GPA CourseID Bartell 3.21 DCS Kebel 2.71 DIC Lee 3.82 DIT Lewis 2.51 DICT Law 3.05 DIT Mikulski 1.89 DCS Tham 3.89 DCS Faga 2.22 DIC Nicosia 3.11 DICT Owen 3.34 DIC …….. LastName GPA CourseID Bartell 3.21 DCS Mikulski 1.89 DCS Tham 3.89 DCS Williams 2.74 DCS Retrieve only those enrolled in DCS course
  • 4. 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
  • 5. 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
  • 6. 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.
  • 7. Restricting data that are Character String and Date SELECT LastName, FirstName, Gender FROM student WHERE Gender = ‘F’; SELECT LastName, FirstName, Gender FROM student WHERE Gender = “F”; or LastName GPA CourseID Bartell 3.21 DCS Mikulski 1.89 DCS Tham 3.89 DCS Williams 2.74 DCS Note : using ‘f’ or ‘F’ will also return the same result Single-quotation double-quotation
  • 8. Comparison Operators Operator = > >= < <= <> Description Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to
  • 9. Using Comparison Operator SELECT StudID, GPA FROM student WHERE GPA > 2.9; 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 Output :
  • 10. Other SQL Comparison Operators Operator BETWEEN ...AND... IN(list) LIKE IS NULL Meaning Between two values (inclusive) Match any of a list of specific values Match a character pattern Is a null value (find nulls)
  • 11. BETWEEN..AND.. operator SELECT LastName, FirstName, DateEnrolled FROM student WHERE DateEnrolled Between #01-Jan-2001# And #31-Dec-2001# ; Output : LastName FirstName DateEnrolled Kebel Laura N. 23-Jun-01 Law Arthur 01-Apr-01 Faga Mark J. 25-Jun-01 Owen John M. 17-Sep-01 Ng Jian Xian 01-Apr-01 Jann How 01-Apr-01 Lower limit Upper limit Note: Dates must enclosed in pound signs (# #)
  • 12. 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
  • 13. 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
  • 14. 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 dash) Range of characters [!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.
  • 15. 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’ ‘???e’ ‘none’ ‘123e’ Starting with a letter between ‘a’ and ‘c’ followed by two digits ‘[a-c]##’ ‘a12’ ‘c33’
  • 16. LIKE operator (example) SELECT LastName, FirstName, CourseID FROM student WHERE LastName Like ‘Le*’; LastName FirstName CourseID Lee Choy Yan DIT Lewis Derrick H. DICT Output :
  • 17. 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
  • 18. Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns TRUE if the following condition is FALSE
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. Rules of Precedence Use parentheses to override rules of precedence. Order Evaluated Operator 1 All comparison operators 2 NOT 3 AND 4 OR
  • 23. Rules of Precedence SELECT FirstName, GroupLeader, GPA FROM student WHERE GroupLeader = 'S007' OR GroupLeader ='S016‘ AND GPA > 3; 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 Output :
  • 24. Rules of Precedence Using Parenthesis to change the precedence FirstName GroupLeader GPA Choy Yan S016 3.82 Xi Xi S016 3.12 SELECT FirstName, GroupLeader, GPA FROM student WHERE (GroupLeader = 'S007' OR GroupLeader ='S016‘) AND GPA > 3; Output :
  • 25. 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
  • 26. Sorting Data  Sorting the rows by single column SELECT LastName, FirstName FROM student ORDER By LastName; Output : Query1 LastName FirstName Bartell Joseph P. Chan Xi Xi Faga Mark J. Jann How Kebel Laura N. Law Arthur Lee Choy Yan Lewis Derrick H. Maser Jennifer L. Mikulski Kathleen …… Ascending order
  • 27. Sorting Data (several columns) SELECT GroupLeader, LastName, FirstName FROM student ORDER By GroupLeader, LastName; 1 2 Output : Query1 GroupLeader LastName FirstName Jann How Law Arthur Owen John M. Tham Tian En S005 Bartell Joseph P. S005 Nicosia Anthony L. S005 Roche Stephanie N. S007 Kebel Laura N. S007 Lewis Derrick H. S007 Ng Jian Xian S010 Faga Mark J. S010 Mikulski Kathleen S010 Williams Jason R. ……. 1 2 2 2 2
  • 28. Sorting Data (in descending) Query1 MentorID MentorName 1004 Schubert 1002 Rimes 1005 Norman 1001 Goile 1003 Christopher 1006 Carroll SELECT MentorID, MentorName FROM Mentor Order By MentorName Desc; Output : Descending order
  • 29. Sorting Data (various orders) Query1 CourseID LastName GPA DCS Tham 3.89 DCS Bartell 3.21 DCS Williams 2.74 DCS Mikulski 1.89 DIC Owen 3.34 DIC Chan 3.12 DIC Kebel 2.71 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 SELECT CourseID, LastName, GPA FROM Student Order By CourseID Asc, GPA Desc ;
  • 30. Summary  At the end of this chapter, you should know how to use WHERE clause to restrict data and also ORDER BY clause to arrange data.  The different types of operator used in SQL.