SlideShare une entreprise Scribd logo
1  sur  26
PRAKTIKUM SISTEM BASIS DATA
SQL Function
Sorting (Pengurutan)
ORDER BY
 Digunakan untuk mengurutkan hasil-ditetapkan oleh
kolom tertentu
 Secara default urutan yang terjadi adalah dari urutan
terkecil ke urutan terbesar (ASC)
 Namun apabila ingin menggunakan urutan yang
sebaliknya (dari besar ke kecil) maka dapat digunakan
key-word “DESC”
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
Now we want to select all the persons from the table
above, however, we want to sort the persons by their last name
SELECT * FROM Persons
ORDER BY LastName
Now we want to select all the persons from the table above,
however, we want to sort the persons descending by their last name
SELECT * FROM Persons
ORDER BY LastName DESC
FUNGSI-FUNGSI BUILT IN
 Secara umum fungsi-fungsi built in dalam SQL dibagi
menjadi 2, yaitu:
 Fungsi Agregasi
 AVG() – mencari nilai rata-rata
 COUNT() – mencari jumlah baris
 MAX() – mencari nilai terbesar
 MIN() – mencari nilai terkecil
 SUM() – mencari jumlah total
 Fungsi Skalar
 UPPER() – membuat karakter menjadi berhuruf kapital
 LOWER() – membuat karakter menjadi huruf biasa
 SUBSTRING() – menentukan karakter yang akan dicari
 LEN() – menentukan panjang karakter
AVG ()
SELECT AVG(column_name) FROM
table_name
Now we want to find the average value of the "OrderPrice"
fields.
SELECT AVG(OrderPrice) AS OrderAverage FROM
Orders
Now we want to find the customers that have an OrderPrice value
higher than the average OrderPrice value
SELECT Customer FROM Orders
WHERE OrderPrice>(SELECT AVG(OrderPrice)
FROM Orders)
Count()
The COUNT() function returns the number of rows that matches a
specified criteria.
• SQL COUNT(column_name) Syntax
The COUNT(column_name) function returns the number of
values (NULL values will not be counted) of the specified
column:
SELECT COUNT(column_name) FROM table_name
• SQL COUNT(*) Syntax
The COUNT(*) function returns the number of records in a table:
SELECT COUNT(*) FROM table_name
• SQL COUNT(DISTINCT column_name) Syntax
The COUNT(DISTINCT column_name) function returns the number
of distinct values of the specified column:
SELECT COUNT(DISTINCT column_name) FROM table_name
We have the following "Orders" table:
Now we want to count the number of orders from "Customer Nilsen".
SELECT COUNT(Customer) AS CustomerNilsen
FROM Orders
WHERE Customer='Nilsen'
The result of the SQL statement above will be 2, because the customer
Nilsen has made 2 orders in total:
If we omit the WHERE clause, like this:
SELECT COUNT(*) AS NumberOfOrders
FROM Orders
The result-set will look like this:
which is the total number of rows in the table.
Now we want to count the number of unique customers in the "Orders"
table.
SELECT COUNT(DISTINCT Customer) AS
NumberOfCustomers FROM Orders
The result-set will look like this:
which is the number of unique customers (Hansen, Nilsen, and Jensen) in
the "Orders" table.
SQL MAX() Function
The MAX() function returns the largest value of the selected column.
SELECT MAX(column_name) FROM
table_name
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
The MIN() Function
The MIN() function returns the smallest value of the selected column.
SELECT MIN(column_name) FROM
table_name
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
SQL SUM() Function
The SUM() function returns the total sum of a numeric column.
SELECT SUM(column_name) FROM table_name
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
SQL GROUP BY Statement
The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
Now we want to find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the customers.
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
GROUP BY More Than One Column
SELECT Customer, OrderDate,
SUM(OrderPrice) FROM Orders
GROUP BY Customer, OrderDate
The HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name)
operator value
Now we want to find if any of the customers have a total order of less than
2000.
SELECT Customer,SUM(OrderPrice)
FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
Now we want to find if the customers "Hansen" or "Jensen" have a total
order of more than 1500.
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer='Jensen'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
The UPPER() Function
The UPPER() function converts the value of a field to uppercase.
SELECT UPPER(column_name) FROM table_name
Now we want to select the content of the "LastName" and "FirstName"
columns above, and convert the "LastName" column to uppercase.
SELECT UPPER(LastName) as LastName,FirstName
FROM Persons
The LOWER() Function
The LOWER() function converts the value of a field to lowercase.
SELECT LOWER(column_name) FROM table_name
SELECT LOWER(LastName) as
LastName,FirstName FROM Persons
SQL SUBSTRING() Function
The SUBSTRING() function is used to extract characters from a text field.
SELECT SUBSTRING(column_name, start, length)
FROM table_name
Parameter Description
column_name Required. The field to extract characters from
start Required. Specifies the starting position (starts at 1)
length Optional. The number of characters to return. If omitted,
the SUBSTRING() function returns the rest of the text
Now we want to extract the first four characters of the "City" column above.
SELECT SUBSTRING(City,1,4) as SmallCity FROM
Persons
SQL LEN() Function
The LEN() function returns the length of the value in a text field.
SELECT LEN(column_name) FROM table_name
Now we want to select the length of the values in the "Address" column
above.
SELECT LEN(Address) as LengthOfAddress
FROM Persons
SEKIAN

Contenu connexe

Tendances

Oracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurOracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurIndiaOptions Softwares
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginnersRam Sagar Mourya
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinctBishal Ghimire
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteAl-Mamun Sarkar
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in databaseHemant Suthar
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1Iblesoft
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for BeginnersAbdelhay Shafi
 
Boost performance with MySQL 5.1 partitions
Boost performance with MySQL 5.1 partitionsBoost performance with MySQL 5.1 partitions
Boost performance with MySQL 5.1 partitionsGiuseppe Maxia
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with examplepranav kumar verma
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functionsVikas Gupta
 

Tendances (18)

Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
 
Oracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurOracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |Thrissur
 
MY SQL
MY SQLMY SQL
MY SQL
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
Oracle Database Sequence
Oracle Database SequenceOracle Database Sequence
Oracle Database Sequence
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Sql
SqlSql
Sql
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
 
SQL Tutorial for Beginners
SQL Tutorial for BeginnersSQL Tutorial for Beginners
SQL Tutorial for Beginners
 
Boost performance with MySQL 5.1 partitions
Boost performance with MySQL 5.1 partitionsBoost performance with MySQL 5.1 partitions
Boost performance with MySQL 5.1 partitions
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
SQL
SQLSQL
SQL
 
Exploring collections with example
Exploring collections with exampleExploring collections with example
Exploring collections with example
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 

Similaire à Si0302 20140320131934

Similaire à Si0302 20140320131934 (20)

Sql
SqlSql
Sql
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Query
QueryQuery
Query
 
SQL report
SQL reportSQL report
SQL report
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
Lab
LabLab
Lab
 
Sql practise for beginners
Sql practise for beginnersSql practise for beginners
Sql practise for beginners
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
sql language
sql languagesql language
sql language
 
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Ppt INFORMATIVE PRACTICES for class 11th chapter 14Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
SQL Query
SQL QuerySQL Query
SQL Query
 
SQL PPT.pptx
SQL PPT.pptxSQL PPT.pptx
SQL PPT.pptx
 
Clauses
ClausesClauses
Clauses
 
Mysql1
Mysql1Mysql1
Mysql1
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Dernier (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
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)
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Si0302 20140320131934

  • 1. PRAKTIKUM SISTEM BASIS DATA SQL Function
  • 2. Sorting (Pengurutan) ORDER BY  Digunakan untuk mengurutkan hasil-ditetapkan oleh kolom tertentu  Secara default urutan yang terjadi adalah dari urutan terkecil ke urutan terbesar (ASC)  Namun apabila ingin menggunakan urutan yang sebaliknya (dari besar ke kecil) maka dapat digunakan key-word “DESC” SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC
  • 3. Now we want to select all the persons from the table above, however, we want to sort the persons by their last name SELECT * FROM Persons ORDER BY LastName
  • 4. Now we want to select all the persons from the table above, however, we want to sort the persons descending by their last name SELECT * FROM Persons ORDER BY LastName DESC
  • 5. FUNGSI-FUNGSI BUILT IN  Secara umum fungsi-fungsi built in dalam SQL dibagi menjadi 2, yaitu:  Fungsi Agregasi  AVG() – mencari nilai rata-rata  COUNT() – mencari jumlah baris  MAX() – mencari nilai terbesar  MIN() – mencari nilai terkecil  SUM() – mencari jumlah total  Fungsi Skalar  UPPER() – membuat karakter menjadi berhuruf kapital  LOWER() – membuat karakter menjadi huruf biasa  SUBSTRING() – menentukan karakter yang akan dicari  LEN() – menentukan panjang karakter
  • 6. AVG () SELECT AVG(column_name) FROM table_name Now we want to find the average value of the "OrderPrice" fields.
  • 7. SELECT AVG(OrderPrice) AS OrderAverage FROM Orders Now we want to find the customers that have an OrderPrice value higher than the average OrderPrice value SELECT Customer FROM Orders WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders)
  • 8. Count() The COUNT() function returns the number of rows that matches a specified criteria. • SQL COUNT(column_name) Syntax The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: SELECT COUNT(column_name) FROM table_name • SQL COUNT(*) Syntax The COUNT(*) function returns the number of records in a table: SELECT COUNT(*) FROM table_name • SQL COUNT(DISTINCT column_name) Syntax The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column: SELECT COUNT(DISTINCT column_name) FROM table_name
  • 9. We have the following "Orders" table: Now we want to count the number of orders from "Customer Nilsen". SELECT COUNT(Customer) AS CustomerNilsen FROM Orders WHERE Customer='Nilsen' The result of the SQL statement above will be 2, because the customer Nilsen has made 2 orders in total:
  • 10. If we omit the WHERE clause, like this: SELECT COUNT(*) AS NumberOfOrders FROM Orders The result-set will look like this: which is the total number of rows in the table.
  • 11. Now we want to count the number of unique customers in the "Orders" table. SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders The result-set will look like this: which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table.
  • 12. SQL MAX() Function The MAX() function returns the largest value of the selected column. SELECT MAX(column_name) FROM table_name SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
  • 13. The MIN() Function The MIN() function returns the smallest value of the selected column. SELECT MIN(column_name) FROM table_name SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
  • 14. SQL SUM() Function The SUM() function returns the total sum of a numeric column. SELECT SUM(column_name) FROM table_name SELECT SUM(OrderPrice) AS OrderTotal FROM Orders
  • 15. SQL GROUP BY Statement The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name
  • 16. Now we want to find the total sum (total order) of each customer. We will have to use the GROUP BY statement to group the customers. SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer
  • 17. GROUP BY More Than One Column SELECT Customer, OrderDate, SUM(OrderPrice) FROM Orders GROUP BY Customer, OrderDate
  • 18. The HAVING Clause The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value
  • 19. Now we want to find if any of the customers have a total order of less than 2000. SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000
  • 20. Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than 1500. SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Hansen' OR Customer='Jensen' GROUP BY Customer HAVING SUM(OrderPrice)>1500
  • 21. The UPPER() Function The UPPER() function converts the value of a field to uppercase. SELECT UPPER(column_name) FROM table_name Now we want to select the content of the "LastName" and "FirstName" columns above, and convert the "LastName" column to uppercase. SELECT UPPER(LastName) as LastName,FirstName FROM Persons
  • 22. The LOWER() Function The LOWER() function converts the value of a field to lowercase. SELECT LOWER(column_name) FROM table_name SELECT LOWER(LastName) as LastName,FirstName FROM Persons
  • 23. SQL SUBSTRING() Function The SUBSTRING() function is used to extract characters from a text field. SELECT SUBSTRING(column_name, start, length) FROM table_name Parameter Description column_name Required. The field to extract characters from start Required. Specifies the starting position (starts at 1) length Optional. The number of characters to return. If omitted, the SUBSTRING() function returns the rest of the text
  • 24. Now we want to extract the first four characters of the "City" column above. SELECT SUBSTRING(City,1,4) as SmallCity FROM Persons
  • 25. SQL LEN() Function The LEN() function returns the length of the value in a text field. SELECT LEN(column_name) FROM table_name Now we want to select the length of the values in the "Address" column above. SELECT LEN(Address) as LengthOfAddress FROM Persons