SlideShare a Scribd company logo
S Q L
DBMS
Bishal Ghimire
SQL Alias
 With SQL, an alias name can be given to a table or to
a column
 Syntax
 SELECT column_name(s)
FROM table_name
AS alias_name
 SELECT O.OrderID, p.LastName, p.FirstName
FROM Persons AS p,
Product_Orders AS O
WHERE p.LastName='Hansen' AND p.FirstName='Ola'
SELECT statement without aliases
 SELECT Product_Orders.OrderID, Persons.LastName,
Persons.FirstName
FROM Persons,
Product_Orders
WHERE Persons.LastName='Hansen' AND
Persons.FirstName='Ola'
SQL AVG() Function
 The AVG() function returns the average value of a
numeric column
 Syntax
 SELECT AVG(column_name) FROM table_name
 Eg-
 SELECT AVG(OrderPrice) AS OrderAverage FROM
OrdersTable
 Output
OrderAverage
950
SQL AVG() Function
 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() Function
 Syntax
 SELECT COUNT(column_name) FROM table_name
 SELECT COUNT(*) FROM table_name
 SELECT COUNT(DISTINCT column_name) FROM
table_name
 Eg
 SELECT COUNT(Customer) AS CustomerNilsen FROM
Orders
WHERE Customer='Nilsen'
MAX() Function
 The MAX() function returns the largest value of the
selected column.
 Syntax
 SELECT MAX(column_name) FROM table_name
 Eg
 SELECT MAX(OrderPrice) AS LargestOrderPrice FROM
Orders
MIN() Function
 MIN() function returns the smallest value of the
selected column
 Syntax
 SELECT MIN(column_name) FROM table_name
 Eg
SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
HAVING Clause
 The HAVING clause was added to SQL because the
WHERE keyword could not be used with aggregate
functions
 Syntax
 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
HAVING Clause
 SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
 SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer='Jensen'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
Customer SUM(OrderPrice)
Nilsen 1700
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000
HAVING Clause
 SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer='Jensen'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
SELECT Customer,SUM(OrderPrice) FROM Orders
WHERE Customer='Hansen' OR Customer='Jensen'
GROUP BY Customer
HAVING SUM(OrderPrice)>1500
Customer SUM(OrderPrice)
Hansen 2000
Jensen 2000
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
 Syntax
 SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
To find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the customers.
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
Customer SUM(OrderPrice)
Hansen 2000
Nilsen 1700
Jensen 2000
 what happens if we omit the GROUP BY statement
Customer SUM(OrderPrice)
Hansen 5700
Nilsen 5700
Hansen 5700
Hansen 5700
Jensen 5700
Nilsen 5700

More Related Content

Similar to 06.02 sql alias

Similar to 06.02 sql alias (13)

Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2
 
Si0302 20140320131934
Si0302 20140320131934Si0302 20140320131934
Si0302 20140320131934
 
ADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptxADV Powepoint 4 Lec.pptx
ADV Powepoint 4 Lec.pptx
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Higher SQL
Higher SQLHigher SQL
Higher SQL
 
Les08-Oracle
Les08-OracleLes08-Oracle
Les08-Oracle
 
Learn sql queries
Learn sql queriesLearn sql queries
Learn sql queries
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
75864 sql
75864 sql75864 sql
75864 sql
 
SQL Portfolio
SQL PortfolioSQL Portfolio
SQL Portfolio
 
Lec 07 SQL - 1.pptx
Lec 07 SQL - 1.pptxLec 07 SQL - 1.pptx
Lec 07 SQL - 1.pptx
 
Sql
SqlSql
Sql
 
Sql powerpoint
Sql powerpointSql powerpoint
Sql powerpoint
 

More from Bishal Ghimire

Counseling Ethics in Astrology for better Mental Health
Counseling Ethics in Astrology for better Mental HealthCounseling Ethics in Astrology for better Mental Health
Counseling Ethics in Astrology for better Mental HealthBishal Ghimire
 
Agile Intro to Manifesto - Values 1 - Interaction over Process
Agile Intro to Manifesto - Values 1 - Interaction over ProcessAgile Intro to Manifesto - Values 1 - Interaction over Process
Agile Intro to Manifesto - Values 1 - Interaction over ProcessBishal Ghimire
 
Earthquake in Nepal 2015
Earthquake in Nepal 2015Earthquake in Nepal 2015
Earthquake in Nepal 2015Bishal Ghimire
 
09.02 normalization example
09.02 normalization example09.02 normalization example
09.02 normalization exampleBishal Ghimire
 
07.03 cartesian product
07.03 cartesian product07.03 cartesian product
07.03 cartesian productBishal Ghimire
 
07.02 relational union intersection
07.02 relational union intersection07.02 relational union intersection
07.02 relational union intersectionBishal Ghimire
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebraBishal Ghimire
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebraBishal Ghimire
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinctBishal Ghimire
 
02.02 querying relational database
02.02 querying relational database02.02 querying relational database
02.02 querying relational databaseBishal Ghimire
 
02.01 relational databases
02.01 relational databases02.01 relational databases
02.01 relational databasesBishal Ghimire
 
01.01 introduction to database
01.01 introduction to database01.01 introduction to database
01.01 introduction to databaseBishal Ghimire
 
00.00 fundamentals of database management syllabus
00.00 fundamentals of database management syllabus00.00 fundamentals of database management syllabus
00.00 fundamentals of database management syllabusBishal Ghimire
 

More from Bishal Ghimire (14)

Counseling Ethics in Astrology for better Mental Health
Counseling Ethics in Astrology for better Mental HealthCounseling Ethics in Astrology for better Mental Health
Counseling Ethics in Astrology for better Mental Health
 
Agile Intro to Manifesto - Values 1 - Interaction over Process
Agile Intro to Manifesto - Values 1 - Interaction over ProcessAgile Intro to Manifesto - Values 1 - Interaction over Process
Agile Intro to Manifesto - Values 1 - Interaction over Process
 
Earthquake in Nepal 2015
Earthquake in Nepal 2015Earthquake in Nepal 2015
Earthquake in Nepal 2015
 
09.02 normalization example
09.02 normalization example09.02 normalization example
09.02 normalization example
 
07.04 joins
07.04 joins07.04 joins
07.04 joins
 
07.03 cartesian product
07.03 cartesian product07.03 cartesian product
07.03 cartesian product
 
07.02 relational union intersection
07.02 relational union intersection07.02 relational union intersection
07.02 relational union intersection
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebra
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebra
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
 
02.02 querying relational database
02.02 querying relational database02.02 querying relational database
02.02 querying relational database
 
02.01 relational databases
02.01 relational databases02.01 relational databases
02.01 relational databases
 
01.01 introduction to database
01.01 introduction to database01.01 introduction to database
01.01 introduction to database
 
00.00 fundamentals of database management syllabus
00.00 fundamentals of database management syllabus00.00 fundamentals of database management syllabus
00.00 fundamentals of database management syllabus
 

Recently uploaded

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaRTTS
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingThijs Feryn
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsVlad Stirbu
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)Ralf Eggert
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsPaul Groth
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀DianaGray10
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Thierry Lestable
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxDavid Michel
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesBhaskar Mitra
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Frank van Harmelen
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backElena Simperl
 

Recently uploaded (20)

JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
Exploring UiPath Orchestrator API: updates and limits in 2024 🚀
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 

06.02 sql alias

  • 2. SQL Alias  With SQL, an alias name can be given to a table or to a column  Syntax  SELECT column_name(s) FROM table_name AS alias_name  SELECT O.OrderID, p.LastName, p.FirstName FROM Persons AS p, Product_Orders AS O WHERE p.LastName='Hansen' AND p.FirstName='Ola'
  • 3. SELECT statement without aliases  SELECT Product_Orders.OrderID, Persons.LastName, Persons.FirstName FROM Persons, Product_Orders WHERE Persons.LastName='Hansen' AND Persons.FirstName='Ola'
  • 4. SQL AVG() Function  The AVG() function returns the average value of a numeric column  Syntax  SELECT AVG(column_name) FROM table_name  Eg-  SELECT AVG(OrderPrice) AS OrderAverage FROM OrdersTable  Output OrderAverage 950
  • 5. SQL AVG() Function  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)
  • 6. COUNT() Function  Syntax  SELECT COUNT(column_name) FROM table_name  SELECT COUNT(*) FROM table_name  SELECT COUNT(DISTINCT column_name) FROM table_name  Eg  SELECT COUNT(Customer) AS CustomerNilsen FROM Orders WHERE Customer='Nilsen'
  • 7. MAX() Function  The MAX() function returns the largest value of the selected column.  Syntax  SELECT MAX(column_name) FROM table_name  Eg  SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders
  • 8. MIN() Function  MIN() function returns the smallest value of the selected column  Syntax  SELECT MIN(column_name) FROM table_name  Eg SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders
  • 9. HAVING Clause  The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions  Syntax  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
  • 10. HAVING Clause  SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000  SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Hansen' OR Customer='Jensen' GROUP BY Customer HAVING SUM(OrderPrice)>1500
  • 11. O_Id OrderDate OrderPrice Customer 1 2008/11/12 1000 Hansen 2 2008/10/23 1600 Nilsen 3 2008/09/02 700 Hansen 4 2008/09/03 300 Hansen 5 2008/08/30 2000 Jensen 6 2008/10/04 100 Nilsen Customer SUM(OrderPrice) Nilsen 1700 SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000
  • 12. HAVING Clause  SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Hansen' OR Customer='Jensen' GROUP BY Customer HAVING SUM(OrderPrice)>1500 O_Id OrderDate OrderPrice Customer 1 2008/11/12 1000 Hansen 2 2008/10/23 1600 Nilsen 3 2008/09/02 700 Hansen 4 2008/09/03 300 Hansen 5 2008/08/30 2000 Jensen 6 2008/10/04 100 Nilsen SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer='Hansen' OR Customer='Jensen' GROUP BY Customer HAVING SUM(OrderPrice)>1500 Customer SUM(OrderPrice) Hansen 2000 Jensen 2000
  • 13. 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  Syntax  SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name
  • 14. O_Id OrderDate OrderPrice Customer 1 2008/11/12 1000 Hansen 2 2008/10/23 1600 Nilsen 3 2008/09/02 700 Hansen 4 2008/09/03 300 Hansen 5 2008/08/30 2000 Jensen 6 2008/10/04 100 Nilsen To find the total sum (total order) of each customer. We will have to use the GROUP BY statement to group the customers.
  • 15. O_Id OrderDate OrderPrice Customer 1 2008/11/12 1000 Hansen 2 2008/10/23 1600 Nilsen 3 2008/09/02 700 Hansen 4 2008/09/03 300 Hansen 5 2008/08/30 2000 Jensen 6 2008/10/04 100 Nilsen SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer Customer SUM(OrderPrice) Hansen 2000 Nilsen 1700 Jensen 2000
  • 16.  what happens if we omit the GROUP BY statement Customer SUM(OrderPrice) Hansen 5700 Nilsen 5700 Hansen 5700 Hansen 5700 Jensen 5700 Nilsen 5700