SlideShare a Scribd company logo
1 of 19
Download to read offline
© 2013 1keydata.com All Rights Reserved
SQL Tutorial
Basic SQL Commands
Agenda
• Database Basics
• SQL Commands
– SELECT … FROM
– WHERE
– ORDER BY
– GROUP BY
– HAVING
© 2013 1keydata.com All Rights Reserved
Database Basics
Database
Tables
© 2013 1keydata.com All Rights Reserved
In a relational database, data is stored in tables.
Database Basics
Database
Tables
Columns
© 2013 1keydata.com All Rights Reserved
Each table consists of columns and rows. Each column is a
field in a record, and there is a column name associated with
each column.
Database Basics
Database
Tables
Columns
Rows
© 2013 1keydata.com All Rights Reserved
Each row represents one record. When we say how many
records we have, we are referring to the number of rows.
SELECT … FROM
© 2013 1keydata.com All Rights Reserved
SELECT “COLUMN_NAME”
FROM “TABLE_NAME”
SQL is structured similar to the English language. The basic
command for retrieving data from a database table is to
SELECT data FROM a table. Not surprisingly, the keywords
"SELECT" and "FROM" make up the core of a SQL
statement.
The syntax for “SELECT… FROM” is:
SELECT … FROM
© 2013 1keydata.com All Rights Reserved
Select more than 1 column:
SELECT “COLUMN_NAME_1”, “COLUMN_NAME_2”
FROM “TABLE_NAME”
Select all columns:
SELECT *
FROM “TABLE_NAME”
Different ways of selecting data:
Select unique values:
SELECT DISTINCT “Column_Name”
FROM “TABLE_NAME”
WHERE
SELECT “COLUMN_NAME”
FROM “TABLE_NAME”
WHERE “CONDITION”
© 2013 1keydata.com All Rights Reserved
Sometimes we want to retrieve only a subset of the data. In
those cases, we use the “WHERE” keyword.
The syntax for “WHERE” is:
CONDITION represents how we want the data to be filtered.
ORDER BY
SELECT “COLUMN_NAME”
FROM “TABLE_NAME”
WHERE “CONDITION”
ORDER BY “COLUMN_NAME” [ASC | DESC]
© 2013 1keydata.com All Rights Reserved
When we want to list the results in a particular order
(ascending or descending), we use the ORDER BY keyword at
the end of the SQL statement.
The syntax for “ORDER BY” is:
MATHEMATICAL FUNCTIONS
© 2013 1keydata.com All Rights Reserved
SQL has built-in mathematical functions to allow us to
perform mathematical operations on the data. Common
mathematical functions include:
• SUM
• AVG
• COUNT
• MAX
• MIN
GROUP BY
© 2013 1keydata.com All Rights Reserved
Date Store Sales_Amount
SELECT MAX(Sales_Amount)
FROM SALES_HISTORY;
To find the highest Sales_Amount across all stores, we use the
MAX( ) function in the following SQL:
SALES_HISTORY
GROUP BY
© 2013 1keydata.com All Rights Reserved
Date Store Sales_Amount
SELECT Store, MAX(Sales_Amount)
FROM SALES_HISTORY;
To find the highest Sales_Amount for each store, we change
the SELECT portion to include “Store”:
SALES_HISTORY
GROUP BY
© 2013 1keydata.com All Rights Reserved
Date Store Sales_Amount
SELECT Store, MAX(Sales_Amount)
FROM SALES_HISTORY
GROUP BY Store;
However, this SELECT statement by itself is not enough. To
allow SQL to correctly calculate what we want, we need to use
the GROUP BY keyword. In the following example, the Store
column after GROUP BY tells SQL to apply the MAX
function for each Store.
SALES_HISTORY
SELECT “COLUMN_NAME_1”,
FUNCTION(“COLUMN_NAME_2”)
FROM “TABLE_NAME”
WHERE “CONDITION”
GROUP BY “COLUMN_NAME_1”
GROUP BY
© 2013 1keydata.com All Rights Reserved
To summarize, the syntax for GROUP BY is as follows:
HAVING
© 2013 1keydata.com All Rights Reserved
Previously we had talked about using the WHERE keyword to
filter results.
We cannot use WHERE to filter based on the result of a
function, because we need to specify the filtering condition
after SQL has calculated the function, and consequently any
filtering condition based on the function needs to be specified
after the GROUP BY phrase. So we cannot use the WHERE
keyword because it is always used before GROUP BY.
HAVING is used to filter based on the result of a function.
HAVING
SELECT “COLUMN_NAME_1”,
FUNCTION(“COLUMN_NAME_2”)
FROM “TABLE_NAME”
GROUP BY “COLUMN_NAME_1”
HAVING (CONDITION based on
FUNCTION)
© 2013 1keydata.com All Rights Reserved
The syntax for HAVING is as follows:
HAVING
© 2013 1keydata.com All Rights Reserved
Date Store Sales_Amount SELECT Store, SUM(Sales_Amount)
FROM SALES_HISTORY
GROUP BY Store
HAVING SUM(Sales_Amount) > 100;
Using the SALES_HISTORY table we had earlier. If we want
to sum the sales amount for each store, but only want to see
results for stores with total sales amount greater than 100, we
use the following SQL:
SALES_HISTORY
Order of SQL Commands
A SELECT statement has the following order:
• SELECT … FROM
• WHERE
• GROUP BY
• HAVING
• ORDER BY
© 2013 1keydata.com All Rights Reserved
1Keydata SQL Tutorial
http://www.1keydata.com/sql/sql.html
© 2013 1keydata.com All Rights Reserved

More Related Content

Similar to sqltutorialbasiccommands-130310014513-phpapp01.pdf

Similar to sqltutorialbasiccommands-130310014513-phpapp01.pdf (20)

Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Veri Ambarları için Oracle'ın Analitik SQL Desteği
Veri Ambarları için Oracle'ın Analitik SQL DesteğiVeri Ambarları için Oracle'ın Analitik SQL Desteği
Veri Ambarları için Oracle'ın Analitik SQL Desteği
 
SQL.pptx for the begineers and good know
SQL.pptx for the begineers and good knowSQL.pptx for the begineers and good know
SQL.pptx for the begineers and good know
 
Mysql
MysqlMysql
Mysql
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
 
Hira
HiraHira
Hira
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
MySQL basics
MySQL basicsMySQL basics
MySQL basics
 
Sql
SqlSql
Sql
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Beg sql
Beg sqlBeg sql
Beg sql
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
SQL Injection Attacks
SQL Injection AttacksSQL Injection Attacks
SQL Injection Attacks
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
SQL Query
SQL QuerySQL Query
SQL Query
 

Recently uploaded

Regional Snapshot Atlanta Aging Trends 2024
Regional Snapshot Atlanta Aging Trends 2024Regional Snapshot Atlanta Aging Trends 2024
Regional Snapshot Atlanta Aging Trends 2024ARCResearch
 
Human-AI Collaboration for Virtual Capacity in Emergency Operation Centers (E...
Human-AI Collaborationfor Virtual Capacity in Emergency Operation Centers (E...Human-AI Collaborationfor Virtual Capacity in Emergency Operation Centers (E...
Human-AI Collaboration for Virtual Capacity in Emergency Operation Centers (E...Hemant Purohit
 
VIP Model Call Girls Lohegaon ( Pune ) Call ON 8005736733 Starting From 5K to...
VIP Model Call Girls Lohegaon ( Pune ) Call ON 8005736733 Starting From 5K to...VIP Model Call Girls Lohegaon ( Pune ) Call ON 8005736733 Starting From 5K to...
VIP Model Call Girls Lohegaon ( Pune ) Call ON 8005736733 Starting From 5K to...SUHANI PANDEY
 
Financing strategies for adaptation. Presentation for CANCC
Financing strategies for adaptation. Presentation for CANCCFinancing strategies for adaptation. Presentation for CANCC
Financing strategies for adaptation. Presentation for CANCCNAP Global Network
 
Top Rated Pune Call Girls Dapodi ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Dapodi ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Dapodi ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Dapodi ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Call Girls in Nagpur High Profile
 
Call On 6297143586 Viman Nagar Call Girls In All Pune 24/7 Provide Call With...
Call On 6297143586  Viman Nagar Call Girls In All Pune 24/7 Provide Call With...Call On 6297143586  Viman Nagar Call Girls In All Pune 24/7 Provide Call With...
Call On 6297143586 Viman Nagar Call Girls In All Pune 24/7 Provide Call With...tanu pandey
 
Finance strategies for adaptation. Presentation for CANCC
Finance strategies for adaptation. Presentation for CANCCFinance strategies for adaptation. Presentation for CANCC
Finance strategies for adaptation. Presentation for CANCCNAP Global Network
 
The U.S. Budget and Economic Outlook (Presentation)
The U.S. Budget and Economic Outlook (Presentation)The U.S. Budget and Economic Outlook (Presentation)
The U.S. Budget and Economic Outlook (Presentation)Congressional Budget Office
 
The NAP process & South-South peer learning
The NAP process & South-South peer learningThe NAP process & South-South peer learning
The NAP process & South-South peer learningNAP Global Network
 
Booking open Available Pune Call Girls Shukrawar Peth 6297143586 Call Hot In...
Booking open Available Pune Call Girls Shukrawar Peth  6297143586 Call Hot In...Booking open Available Pune Call Girls Shukrawar Peth  6297143586 Call Hot In...
Booking open Available Pune Call Girls Shukrawar Peth 6297143586 Call Hot In...tanu pandey
 
Top Rated Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...
Top Rated  Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...Top Rated  Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...
Top Rated Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...Call Girls in Nagpur High Profile
 
2024: The FAR, Federal Acquisition Regulations - Part 29
2024: The FAR, Federal Acquisition Regulations - Part 292024: The FAR, Federal Acquisition Regulations - Part 29
2024: The FAR, Federal Acquisition Regulations - Part 29JSchaus & Associates
 
An Atoll Futures Research Institute? Presentation for CANCC
An Atoll Futures Research Institute? Presentation for CANCCAn Atoll Futures Research Institute? Presentation for CANCC
An Atoll Futures Research Institute? Presentation for CANCCNAP Global Network
 
Antisemitism Awareness Act: pénaliser la critique de l'Etat d'Israël
Antisemitism Awareness Act: pénaliser la critique de l'Etat d'IsraëlAntisemitism Awareness Act: pénaliser la critique de l'Etat d'Israël
Antisemitism Awareness Act: pénaliser la critique de l'Etat d'IsraëlEdouardHusson
 
Call Girls Sangamwadi Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Sangamwadi Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Sangamwadi Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Sangamwadi Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Get Premium Budhwar Peth Call Girls (8005736733) 24x7 Rate 15999 with A/c Roo...
Get Premium Budhwar Peth Call Girls (8005736733) 24x7 Rate 15999 with A/c Roo...Get Premium Budhwar Peth Call Girls (8005736733) 24x7 Rate 15999 with A/c Roo...
Get Premium Budhwar Peth Call Girls (8005736733) 24x7 Rate 15999 with A/c Roo...MOHANI PANDEY
 
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hour
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hourcelebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hour
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hourCall Girls in Nagpur High Profile
 
Coastal Protection Measures in Hulhumale'
Coastal Protection Measures in Hulhumale'Coastal Protection Measures in Hulhumale'
Coastal Protection Measures in Hulhumale'NAP Global Network
 
Top Rated Pune Call Girls Bhosari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Bhosari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Bhosari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Bhosari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

Regional Snapshot Atlanta Aging Trends 2024
Regional Snapshot Atlanta Aging Trends 2024Regional Snapshot Atlanta Aging Trends 2024
Regional Snapshot Atlanta Aging Trends 2024
 
Human-AI Collaboration for Virtual Capacity in Emergency Operation Centers (E...
Human-AI Collaborationfor Virtual Capacity in Emergency Operation Centers (E...Human-AI Collaborationfor Virtual Capacity in Emergency Operation Centers (E...
Human-AI Collaboration for Virtual Capacity in Emergency Operation Centers (E...
 
VIP Model Call Girls Lohegaon ( Pune ) Call ON 8005736733 Starting From 5K to...
VIP Model Call Girls Lohegaon ( Pune ) Call ON 8005736733 Starting From 5K to...VIP Model Call Girls Lohegaon ( Pune ) Call ON 8005736733 Starting From 5K to...
VIP Model Call Girls Lohegaon ( Pune ) Call ON 8005736733 Starting From 5K to...
 
Financing strategies for adaptation. Presentation for CANCC
Financing strategies for adaptation. Presentation for CANCCFinancing strategies for adaptation. Presentation for CANCC
Financing strategies for adaptation. Presentation for CANCC
 
(NEHA) Call Girls Nagpur Call Now 8250077686 Nagpur Escorts 24x7
(NEHA) Call Girls Nagpur Call Now 8250077686 Nagpur Escorts 24x7(NEHA) Call Girls Nagpur Call Now 8250077686 Nagpur Escorts 24x7
(NEHA) Call Girls Nagpur Call Now 8250077686 Nagpur Escorts 24x7
 
Top Rated Pune Call Girls Dapodi ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Dapodi ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Dapodi ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Dapodi ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
Call On 6297143586 Viman Nagar Call Girls In All Pune 24/7 Provide Call With...
Call On 6297143586  Viman Nagar Call Girls In All Pune 24/7 Provide Call With...Call On 6297143586  Viman Nagar Call Girls In All Pune 24/7 Provide Call With...
Call On 6297143586 Viman Nagar Call Girls In All Pune 24/7 Provide Call With...
 
Finance strategies for adaptation. Presentation for CANCC
Finance strategies for adaptation. Presentation for CANCCFinance strategies for adaptation. Presentation for CANCC
Finance strategies for adaptation. Presentation for CANCC
 
The U.S. Budget and Economic Outlook (Presentation)
The U.S. Budget and Economic Outlook (Presentation)The U.S. Budget and Economic Outlook (Presentation)
The U.S. Budget and Economic Outlook (Presentation)
 
The NAP process & South-South peer learning
The NAP process & South-South peer learningThe NAP process & South-South peer learning
The NAP process & South-South peer learning
 
Booking open Available Pune Call Girls Shukrawar Peth 6297143586 Call Hot In...
Booking open Available Pune Call Girls Shukrawar Peth  6297143586 Call Hot In...Booking open Available Pune Call Girls Shukrawar Peth  6297143586 Call Hot In...
Booking open Available Pune Call Girls Shukrawar Peth 6297143586 Call Hot In...
 
Top Rated Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...
Top Rated  Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...Top Rated  Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...
Top Rated Pune Call Girls Hadapsar ⟟ 6297143586 ⟟ Call Me For Genuine Sex Se...
 
2024: The FAR, Federal Acquisition Regulations - Part 29
2024: The FAR, Federal Acquisition Regulations - Part 292024: The FAR, Federal Acquisition Regulations - Part 29
2024: The FAR, Federal Acquisition Regulations - Part 29
 
An Atoll Futures Research Institute? Presentation for CANCC
An Atoll Futures Research Institute? Presentation for CANCCAn Atoll Futures Research Institute? Presentation for CANCC
An Atoll Futures Research Institute? Presentation for CANCC
 
Antisemitism Awareness Act: pénaliser la critique de l'Etat d'Israël
Antisemitism Awareness Act: pénaliser la critique de l'Etat d'IsraëlAntisemitism Awareness Act: pénaliser la critique de l'Etat d'Israël
Antisemitism Awareness Act: pénaliser la critique de l'Etat d'Israël
 
Call Girls Sangamwadi Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Sangamwadi Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Sangamwadi Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Sangamwadi Call Me 7737669865 Budget Friendly No Advance Booking
 
Get Premium Budhwar Peth Call Girls (8005736733) 24x7 Rate 15999 with A/c Roo...
Get Premium Budhwar Peth Call Girls (8005736733) 24x7 Rate 15999 with A/c Roo...Get Premium Budhwar Peth Call Girls (8005736733) 24x7 Rate 15999 with A/c Roo...
Get Premium Budhwar Peth Call Girls (8005736733) 24x7 Rate 15999 with A/c Roo...
 
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hour
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hourcelebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hour
celebrity 💋 Agra Escorts Just Dail 8250092165 service available anytime 24 hour
 
Coastal Protection Measures in Hulhumale'
Coastal Protection Measures in Hulhumale'Coastal Protection Measures in Hulhumale'
Coastal Protection Measures in Hulhumale'
 
Top Rated Pune Call Girls Bhosari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated  Pune Call Girls Bhosari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...Top Rated  Pune Call Girls Bhosari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
Top Rated Pune Call Girls Bhosari ⟟ 6297143586 ⟟ Call Me For Genuine Sex Ser...
 

sqltutorialbasiccommands-130310014513-phpapp01.pdf

  • 1. © 2013 1keydata.com All Rights Reserved SQL Tutorial Basic SQL Commands
  • 2. Agenda • Database Basics • SQL Commands – SELECT … FROM – WHERE – ORDER BY – GROUP BY – HAVING © 2013 1keydata.com All Rights Reserved
  • 3. Database Basics Database Tables © 2013 1keydata.com All Rights Reserved In a relational database, data is stored in tables.
  • 4. Database Basics Database Tables Columns © 2013 1keydata.com All Rights Reserved Each table consists of columns and rows. Each column is a field in a record, and there is a column name associated with each column.
  • 5. Database Basics Database Tables Columns Rows © 2013 1keydata.com All Rights Reserved Each row represents one record. When we say how many records we have, we are referring to the number of rows.
  • 6. SELECT … FROM © 2013 1keydata.com All Rights Reserved SELECT “COLUMN_NAME” FROM “TABLE_NAME” SQL is structured similar to the English language. The basic command for retrieving data from a database table is to SELECT data FROM a table. Not surprisingly, the keywords "SELECT" and "FROM" make up the core of a SQL statement. The syntax for “SELECT… FROM” is:
  • 7. SELECT … FROM © 2013 1keydata.com All Rights Reserved Select more than 1 column: SELECT “COLUMN_NAME_1”, “COLUMN_NAME_2” FROM “TABLE_NAME” Select all columns: SELECT * FROM “TABLE_NAME” Different ways of selecting data: Select unique values: SELECT DISTINCT “Column_Name” FROM “TABLE_NAME”
  • 8. WHERE SELECT “COLUMN_NAME” FROM “TABLE_NAME” WHERE “CONDITION” © 2013 1keydata.com All Rights Reserved Sometimes we want to retrieve only a subset of the data. In those cases, we use the “WHERE” keyword. The syntax for “WHERE” is: CONDITION represents how we want the data to be filtered.
  • 9. ORDER BY SELECT “COLUMN_NAME” FROM “TABLE_NAME” WHERE “CONDITION” ORDER BY “COLUMN_NAME” [ASC | DESC] © 2013 1keydata.com All Rights Reserved When we want to list the results in a particular order (ascending or descending), we use the ORDER BY keyword at the end of the SQL statement. The syntax for “ORDER BY” is:
  • 10. MATHEMATICAL FUNCTIONS © 2013 1keydata.com All Rights Reserved SQL has built-in mathematical functions to allow us to perform mathematical operations on the data. Common mathematical functions include: • SUM • AVG • COUNT • MAX • MIN
  • 11. GROUP BY © 2013 1keydata.com All Rights Reserved Date Store Sales_Amount SELECT MAX(Sales_Amount) FROM SALES_HISTORY; To find the highest Sales_Amount across all stores, we use the MAX( ) function in the following SQL: SALES_HISTORY
  • 12. GROUP BY © 2013 1keydata.com All Rights Reserved Date Store Sales_Amount SELECT Store, MAX(Sales_Amount) FROM SALES_HISTORY; To find the highest Sales_Amount for each store, we change the SELECT portion to include “Store”: SALES_HISTORY
  • 13. GROUP BY © 2013 1keydata.com All Rights Reserved Date Store Sales_Amount SELECT Store, MAX(Sales_Amount) FROM SALES_HISTORY GROUP BY Store; However, this SELECT statement by itself is not enough. To allow SQL to correctly calculate what we want, we need to use the GROUP BY keyword. In the following example, the Store column after GROUP BY tells SQL to apply the MAX function for each Store. SALES_HISTORY
  • 14. SELECT “COLUMN_NAME_1”, FUNCTION(“COLUMN_NAME_2”) FROM “TABLE_NAME” WHERE “CONDITION” GROUP BY “COLUMN_NAME_1” GROUP BY © 2013 1keydata.com All Rights Reserved To summarize, the syntax for GROUP BY is as follows:
  • 15. HAVING © 2013 1keydata.com All Rights Reserved Previously we had talked about using the WHERE keyword to filter results. We cannot use WHERE to filter based on the result of a function, because we need to specify the filtering condition after SQL has calculated the function, and consequently any filtering condition based on the function needs to be specified after the GROUP BY phrase. So we cannot use the WHERE keyword because it is always used before GROUP BY. HAVING is used to filter based on the result of a function.
  • 16. HAVING SELECT “COLUMN_NAME_1”, FUNCTION(“COLUMN_NAME_2”) FROM “TABLE_NAME” GROUP BY “COLUMN_NAME_1” HAVING (CONDITION based on FUNCTION) © 2013 1keydata.com All Rights Reserved The syntax for HAVING is as follows:
  • 17. HAVING © 2013 1keydata.com All Rights Reserved Date Store Sales_Amount SELECT Store, SUM(Sales_Amount) FROM SALES_HISTORY GROUP BY Store HAVING SUM(Sales_Amount) > 100; Using the SALES_HISTORY table we had earlier. If we want to sum the sales amount for each store, but only want to see results for stores with total sales amount greater than 100, we use the following SQL: SALES_HISTORY
  • 18. Order of SQL Commands A SELECT statement has the following order: • SELECT … FROM • WHERE • GROUP BY • HAVING • ORDER BY © 2013 1keydata.com All Rights Reserved
  • 19. 1Keydata SQL Tutorial http://www.1keydata.com/sql/sql.html © 2013 1keydata.com All Rights Reserved