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

Más contenido relacionado

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

Más de Bishal Ghimire

09.02 normalization example
09.02 normalization example09.02 normalization example
09.02 normalization example
Bishal Ghimire
 
07.03 cartesian product
07.03 cartesian product07.03 cartesian product
07.03 cartesian product
Bishal Ghimire
 
07.02 relational union intersection
07.02 relational union intersection07.02 relational union intersection
07.02 relational union intersection
Bishal Ghimire
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebra
Bishal Ghimire
 
07.01 relational algebra
07.01 relational algebra07.01 relational algebra
07.01 relational algebra
Bishal Ghimire
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
Bishal Ghimire
 
02.02 querying relational database
02.02 querying relational database02.02 querying relational database
02.02 querying relational database
Bishal Ghimire
 
02.01 relational databases
02.01 relational databases02.01 relational databases
02.01 relational databases
Bishal 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 syllabus
Bishal Ghimire
 

Más de 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
 

Último

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
Safe Software
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
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
panagenda
 

Último (20)

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
 
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
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
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...
 
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
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
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
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

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