SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Semelhante a Si0302 20140320131934

Semelhante a 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
 

Último

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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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
 
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
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
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
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Último (20)

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...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
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
 
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
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
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...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
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
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 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