SlideShare a Scribd company logo
© 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

Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
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
Emrah METE
 
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
PavithSingh
 
Chapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdfChapter – 6 SQL Lab Tutorial.pdf
Chapter – 6 SQL Lab Tutorial.pdf
TamiratDejene1
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
Balqees Al.Mubarak
 
SQL Tunning
SQL TunningSQL Tunning
SQL Tunning
Dhananjay Goel
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
Tony Wong
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
Abrar ali
 
Sql
SqlSql
Beg sql
Beg sqlBeg sql
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
Mahir Haque
 
SQL Query
SQL QuerySQL Query
SQL Query
Imam340267
 

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

如何办理(uoit毕业证书)加拿大安大略理工大学毕业证文凭证书录取通知原版一模一样
如何办理(uoit毕业证书)加拿大安大略理工大学毕业证文凭证书录取通知原版一模一样如何办理(uoit毕业证书)加拿大安大略理工大学毕业证文凭证书录取通知原版一模一样
如何办理(uoit毕业证书)加拿大安大略理工大学毕业证文凭证书录取通知原版一模一样
850fcj96
 
PD-1602-as-amended-by-RA-9287-Anti-Illegal-Gambling-Law.pptx
PD-1602-as-amended-by-RA-9287-Anti-Illegal-Gambling-Law.pptxPD-1602-as-amended-by-RA-9287-Anti-Illegal-Gambling-Law.pptx
PD-1602-as-amended-by-RA-9287-Anti-Illegal-Gambling-Law.pptx
RIDPRO11
 
2024: The FAR - Federal Acquisition Regulations, Part 37
2024: The FAR - Federal Acquisition Regulations, Part 372024: The FAR - Federal Acquisition Regulations, Part 37
2024: The FAR - Federal Acquisition Regulations, Part 37
JSchaus & Associates
 
Understanding the Challenges of Street Children
Understanding the Challenges of Street ChildrenUnderstanding the Challenges of Street Children
Understanding the Challenges of Street Children
SERUDS INDIA
 
State crafting: Changes and challenges for managing the public finances
State crafting: Changes and challenges for managing the public financesState crafting: Changes and challenges for managing the public finances
State crafting: Changes and challenges for managing the public finances
ResolutionFoundation
 
kupon sample qurban masjid indonesia terbaru.pptx
kupon sample qurban masjid indonesia terbaru.pptxkupon sample qurban masjid indonesia terbaru.pptx
kupon sample qurban masjid indonesia terbaru.pptx
viderakai
 
The Role of a Process Server in real estate
The Role of a Process Server in real estateThe Role of a Process Server in real estate
The Role of a Process Server in real estate
oklahomajudicialproc1
 
Russian anarchist and anti-war movement in the third year of full-scale war
Russian anarchist and anti-war movement in the third year of full-scale warRussian anarchist and anti-war movement in the third year of full-scale war
Russian anarchist and anti-war movement in the third year of full-scale war
Antti Rautiainen
 
NHAI_Under_Implementation_01-05-2024.pdf
NHAI_Under_Implementation_01-05-2024.pdfNHAI_Under_Implementation_01-05-2024.pdf
NHAI_Under_Implementation_01-05-2024.pdf
AjayVejendla3
 
MHM Roundtable Slide Deck WHA Side-event May 28 2024.pptx
MHM Roundtable Slide Deck WHA Side-event May 28 2024.pptxMHM Roundtable Slide Deck WHA Side-event May 28 2024.pptx
MHM Roundtable Slide Deck WHA Side-event May 28 2024.pptx
ILC- UK
 
快速制作(ocad毕业证书)加拿大安大略艺术设计学院毕业证本科学历雅思成绩单原版一模一样
快速制作(ocad毕业证书)加拿大安大略艺术设计学院毕业证本科学历雅思成绩单原版一模一样快速制作(ocad毕业证书)加拿大安大略艺术设计学院毕业证本科学历雅思成绩单原版一模一样
快速制作(ocad毕业证书)加拿大安大略艺术设计学院毕业证本科学历雅思成绩单原版一模一样
850fcj96
 
Opinions on EVs: Metro Atlanta Speaks 2023
Opinions on EVs: Metro Atlanta Speaks 2023Opinions on EVs: Metro Atlanta Speaks 2023
Opinions on EVs: Metro Atlanta Speaks 2023
ARCResearch
 
PNRR MADRID GREENTECH FOR BROWN NETWORKS NETWORKS MUR_MUSA_TEBALDI.pdf
PNRR MADRID GREENTECH FOR BROWN NETWORKS NETWORKS MUR_MUSA_TEBALDI.pdfPNRR MADRID GREENTECH FOR BROWN NETWORKS NETWORKS MUR_MUSA_TEBALDI.pdf
PNRR MADRID GREENTECH FOR BROWN NETWORKS NETWORKS MUR_MUSA_TEBALDI.pdf
ClaudioTebaldi2
 
2024: The FAR - Federal Acquisition Regulations, Part 36
2024: The FAR - Federal Acquisition Regulations, Part 362024: The FAR - Federal Acquisition Regulations, Part 36
2024: The FAR - Federal Acquisition Regulations, Part 36
JSchaus & Associates
 
一比一原版(Adelaide毕业证)阿德莱德大学毕业证成绩单
一比一原版(Adelaide毕业证)阿德莱德大学毕业证成绩单一比一原版(Adelaide毕业证)阿德莱德大学毕业证成绩单
一比一原版(Adelaide毕业证)阿德莱德大学毕业证成绩单
ehbuaw
 
Uniform Guidance 3.0 - The New 2 CFR 200
Uniform Guidance 3.0 - The New 2 CFR 200Uniform Guidance 3.0 - The New 2 CFR 200
Uniform Guidance 3.0 - The New 2 CFR 200
GrantManagementInsti
 
2024: The FAR - Federal Acquisition Regulations, Part 38
2024: The FAR - Federal Acquisition Regulations, Part 382024: The FAR - Federal Acquisition Regulations, Part 38
2024: The FAR - Federal Acquisition Regulations, Part 38
JSchaus & Associates
 
ZGB - The Role of Generative AI in Government transformation.pdf
ZGB - The Role of Generative AI in Government transformation.pdfZGB - The Role of Generative AI in Government transformation.pdf
ZGB - The Role of Generative AI in Government transformation.pdf
Saeed Al Dhaheri
 
Get Government Grants and Assistance Program
Get Government Grants and Assistance ProgramGet Government Grants and Assistance Program
Get Government Grants and Assistance Program
Get Government Grants
 
Donate to charity during this holiday season
Donate to charity during this holiday seasonDonate to charity during this holiday season
Donate to charity during this holiday season
SERUDS INDIA
 

Recently uploaded (20)

如何办理(uoit毕业证书)加拿大安大略理工大学毕业证文凭证书录取通知原版一模一样
如何办理(uoit毕业证书)加拿大安大略理工大学毕业证文凭证书录取通知原版一模一样如何办理(uoit毕业证书)加拿大安大略理工大学毕业证文凭证书录取通知原版一模一样
如何办理(uoit毕业证书)加拿大安大略理工大学毕业证文凭证书录取通知原版一模一样
 
PD-1602-as-amended-by-RA-9287-Anti-Illegal-Gambling-Law.pptx
PD-1602-as-amended-by-RA-9287-Anti-Illegal-Gambling-Law.pptxPD-1602-as-amended-by-RA-9287-Anti-Illegal-Gambling-Law.pptx
PD-1602-as-amended-by-RA-9287-Anti-Illegal-Gambling-Law.pptx
 
2024: The FAR - Federal Acquisition Regulations, Part 37
2024: The FAR - Federal Acquisition Regulations, Part 372024: The FAR - Federal Acquisition Regulations, Part 37
2024: The FAR - Federal Acquisition Regulations, Part 37
 
Understanding the Challenges of Street Children
Understanding the Challenges of Street ChildrenUnderstanding the Challenges of Street Children
Understanding the Challenges of Street Children
 
State crafting: Changes and challenges for managing the public finances
State crafting: Changes and challenges for managing the public financesState crafting: Changes and challenges for managing the public finances
State crafting: Changes and challenges for managing the public finances
 
kupon sample qurban masjid indonesia terbaru.pptx
kupon sample qurban masjid indonesia terbaru.pptxkupon sample qurban masjid indonesia terbaru.pptx
kupon sample qurban masjid indonesia terbaru.pptx
 
The Role of a Process Server in real estate
The Role of a Process Server in real estateThe Role of a Process Server in real estate
The Role of a Process Server in real estate
 
Russian anarchist and anti-war movement in the third year of full-scale war
Russian anarchist and anti-war movement in the third year of full-scale warRussian anarchist and anti-war movement in the third year of full-scale war
Russian anarchist and anti-war movement in the third year of full-scale war
 
NHAI_Under_Implementation_01-05-2024.pdf
NHAI_Under_Implementation_01-05-2024.pdfNHAI_Under_Implementation_01-05-2024.pdf
NHAI_Under_Implementation_01-05-2024.pdf
 
MHM Roundtable Slide Deck WHA Side-event May 28 2024.pptx
MHM Roundtable Slide Deck WHA Side-event May 28 2024.pptxMHM Roundtable Slide Deck WHA Side-event May 28 2024.pptx
MHM Roundtable Slide Deck WHA Side-event May 28 2024.pptx
 
快速制作(ocad毕业证书)加拿大安大略艺术设计学院毕业证本科学历雅思成绩单原版一模一样
快速制作(ocad毕业证书)加拿大安大略艺术设计学院毕业证本科学历雅思成绩单原版一模一样快速制作(ocad毕业证书)加拿大安大略艺术设计学院毕业证本科学历雅思成绩单原版一模一样
快速制作(ocad毕业证书)加拿大安大略艺术设计学院毕业证本科学历雅思成绩单原版一模一样
 
Opinions on EVs: Metro Atlanta Speaks 2023
Opinions on EVs: Metro Atlanta Speaks 2023Opinions on EVs: Metro Atlanta Speaks 2023
Opinions on EVs: Metro Atlanta Speaks 2023
 
PNRR MADRID GREENTECH FOR BROWN NETWORKS NETWORKS MUR_MUSA_TEBALDI.pdf
PNRR MADRID GREENTECH FOR BROWN NETWORKS NETWORKS MUR_MUSA_TEBALDI.pdfPNRR MADRID GREENTECH FOR BROWN NETWORKS NETWORKS MUR_MUSA_TEBALDI.pdf
PNRR MADRID GREENTECH FOR BROWN NETWORKS NETWORKS MUR_MUSA_TEBALDI.pdf
 
2024: The FAR - Federal Acquisition Regulations, Part 36
2024: The FAR - Federal Acquisition Regulations, Part 362024: The FAR - Federal Acquisition Regulations, Part 36
2024: The FAR - Federal Acquisition Regulations, Part 36
 
一比一原版(Adelaide毕业证)阿德莱德大学毕业证成绩单
一比一原版(Adelaide毕业证)阿德莱德大学毕业证成绩单一比一原版(Adelaide毕业证)阿德莱德大学毕业证成绩单
一比一原版(Adelaide毕业证)阿德莱德大学毕业证成绩单
 
Uniform Guidance 3.0 - The New 2 CFR 200
Uniform Guidance 3.0 - The New 2 CFR 200Uniform Guidance 3.0 - The New 2 CFR 200
Uniform Guidance 3.0 - The New 2 CFR 200
 
2024: The FAR - Federal Acquisition Regulations, Part 38
2024: The FAR - Federal Acquisition Regulations, Part 382024: The FAR - Federal Acquisition Regulations, Part 38
2024: The FAR - Federal Acquisition Regulations, Part 38
 
ZGB - The Role of Generative AI in Government transformation.pdf
ZGB - The Role of Generative AI in Government transformation.pdfZGB - The Role of Generative AI in Government transformation.pdf
ZGB - The Role of Generative AI in Government transformation.pdf
 
Get Government Grants and Assistance Program
Get Government Grants and Assistance ProgramGet Government Grants and Assistance Program
Get Government Grants and Assistance Program
 
Donate to charity during this holiday season
Donate to charity during this holiday seasonDonate to charity during this holiday season
Donate to charity during this holiday season
 

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