SlideShare a Scribd company logo
• Select Statement
• Select command is used to fetch the data in a set of records from a
table, view or a group of tables, views by making use of SQL joins.
• Retrieval of data using SQL statements can be done by using
different predicates like −
• Where
• Group By
• Having
• Order By
Basic select :
Select * from student;
Name Regno Branch Age
Hari 100 CSE 15
Pinky 101 CSE 17
Bob 102 CSE 14
Bhanu 103 CSE 18
Where clause
• Where clause is used with the data manipulation language (DML)
statement to check for a condition being met in row.
• Example 1
• The query given below displays the students’ records whose age is
in between 15 and 20.
SELECT * FROM student where age>15 and age<20;
(OR)
SELECT * FROM student where age between 15 and 20;
Name Regno Branch Age
Pinky 101 CSE 17
Bhanu 103 CSE 18
Example 2
• Consider another example to know more about where clause −
• Like operator is used to search for a specified pattern in a column.
The percent sign (%) represents zero, one, or multiple characters
The underscore sign (_) represents one, single character
The following SQL statement selects all students with a student Name that have
"or" in any position:
SELECT *FROM student where name LIKE '%or%’;
The following SQL statement selects all students with a student name that have "r"
in the second position
SELECT *FROM student where name LIKE '_r%’;
The following SQL statement selects all customers with a ContactName that starts
with "a" and ends with "o":
SELECT * FROM Customers
WHERE ContactName LIKE 'a%o';
The following SQL statement selects all customers with a CustomerName that does
NOT start with "a":
SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'a%’;
Example: SELECT *FROM student where name like B%;
Name Regno Branch Age
Bob 102 CSE 14
Bhanu 103 CSE 18
SQL group by
In SQL, The Group By statement is used for organizing similar data into groups. The data is further
organized with the help of equivalent function. It means, if different rows in a precise column have the
same values, it will arrange those rows in a group.
•The SELECT statement is used with the GROUP BY clause in the SQL query.
•WHERE clause is placed before the GROUP BY clause in SQL.
•ORDER BY clause is placed after the GROUP BY clause in SQL.
S.no Name AGE Salary
1 John 24 25000
2 Nick 22 22000
3 Amara 25 15000
4 Nick 22 22000
5 John 24 25000
SUBJECT YEAR NAME
C language 2 John
C language 2 Ginny
C language 2 Jasmeen
C language 3 Nick
C language 3 Amara
Java 1 Sifa
Java 1 dolly
1.SELECT NAME, SUM (SALARY) FROM Employee
2.GROUP BY NAME;
S.no Name AGE Salary
1 John 24 25000
2 Nick 22 22000
3 Amara 25 15000
4 Nick 22 22000
5 John 24 25000
NAME SALARY
John 50000
Nick 44000
Amara 15000
SUBJECT YEAR NAME
C language 2 John
C language 2 Ginny
C language 2 Jasmeen
C language 3 Nick
C language 3 Amara
Java 1 Sifa
Java 1 dolly
1.SELECT SUBJECT, YEAR, Count (*)
2.FROM Student
3.Group BY SUBJECT, YEAR;
SUBJECT YEAR Count
C language 2 3
C language 3 2
Java 1 2
HAVING Clause
WHERE clause is used for deciding purpose. It is used to place conditions on the
columns to determine the part of the last result-set of the group. Here, we are not
required to use the combined functions like COUNT (), SUM (), etc. with the WHERE
clause. After that, we need to use a HAVING clause.
Syntax:
SELECT column1, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2
HAVING condition
ORDER BY column1, column2;
function_name: Mainly used for name of the function, SUM(), AVG().
table_name: Used for name of the table.
condition: Condition used.
HAVING Clause
SELECT NAME, SUM(SALARY) FROM Employee
GROUP BY NAME
HAVING SUM(SALARY)>=50000;
Name SUM(SALARY)
John 50000
Aggregate Functions in SQL
• SQL Aggregate Functions
• SQL aggregation function is used to perform the calculations on
multiple rows of a single column of a table. It returns a single
value.
• It is also used to summarize the data.
Types of SQL Aggregation Function
COUNT FUNCTION
• COUNT function is used to Count the number of rows in a
database table. It can work on both numeric and non-numeric
data types.
• COUNT function uses the COUNT(*) that returns the count of
all the rows in a specified table. COUNT(*) considers duplicate
and Null.
Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
• Sample table:
• PRODUCT_MAST
Output: 10
PRODUCT COMPANY QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
Example: COUNT()
SELECT COUNT(*) FROM PRODUCT_MAST
Example: COUNT with WHERE
SELECT COUNT(*)
FROM PRODUCT_MAST;
WHERE RATE>=20;
Output:
7
• Sample table:
• PRODUCT_MAST
PRODUC
T
COMPAN
Y
QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
Example: COUNT() with DISTINCT
SELECT COUNT(DISTINCT COMPAN
Y)
FROM PRODUCT_MAST;
Output: 3
Example: COUNT() with GROUP BY
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY;
Output:
Com1 5
Com2 3
• Sample table:
• PRODUCT_MAST
PRODUC
T
COMPAN
Y
QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
Example: COUNT() with HAVING
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
Output:
Com1 5
Com2 3
2. SUM Function
• Sum function is used to calculate the sum of all selected
columns. It works on numeric fields only.
Syntax
SUM()
or
SUM( [ALL|DISTINCT] expression )
Example: SUM()
SELECT SUM(COST)
FROM PRODUCT_MAST;
Output: 670
PROD
UCT
COM
PANY
QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
Example: SUM() with WHERE
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;
Output:320
Example: SUM() with GROUP BY
SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3
GROUP BY COMPANY;
Output:
Com1 150
Com2 170
PROD
UCT
COM
PANY
QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
• Example: SUM() with HAVING
SELECT COMPANY, SUM(COST)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING SUM(COST)>=170;
Output:
Com1 335
Com3 170
PROD
UCT
COM
PANY
QTY RATE COST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
• 3. AVG function
• The AVG function is used to calculate the average value of the
numeric type. AVG function returns the average of all non-Null
values.
• Syntax
AVG()
or
AVG( [ALL|DISTINCT] expression )
Example:
SELECT AVG(COST)
FROM PRODUCT_MAST;
Output: 67.00
4. MAX Function
• The MAX function is used to find the maximum value of a certain
column. This function determines the largest value of all selected
values of a column.
• Syntax
MAX()
or
MAX( [ALL|DISTINCT] expression )
Example:
SELECT MAX(RATE)
FROM PRODUCT_MAST;
Output:30
5. MIN Function
• MIN function is used to find the minimum value of a certain column.
This function determines the smallest value of all selected values of
a column.
• Syntax
MIN()
or
MIN( [ALL|DISTINCT] expression )
• Example:
SELECT MIN(RATE)
FROM PRODUCT_MAST;
Output: 10

More Related Content

Similar to Unit 3-Select Options and Aggregate Functions in SQL (1).pptx

Sql intro
Sql introSql intro
Sql introglubox
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
Madhusha15
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
Oraclesql
OraclesqlOraclesql
Oraclesql
Priya Goyal
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
ANSHVAJPAI
 
Sql clauses by Manan Pasricha
Sql clauses by Manan PasrichaSql clauses by Manan Pasricha
Sql clauses by Manan Pasricha
MananPasricha
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
guest2160992
 
Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2Belal Arfa
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
Sql
SqlSql
DB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptxDB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptx
NermeenKamel7
 
Sql functions
Sql functionsSql functions
Sql functions
G C Reddy Technologies
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
DataminingTools Inc
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
oracle content
 
Dbms presentationnnnn
Dbms presentationnnnnDbms presentationnnnn
Dbms presentationnnnn
sumi haque
 

Similar to Unit 3-Select Options and Aggregate Functions in SQL (1).pptx (20)

Sql intro
Sql introSql intro
Sql intro
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Chapter5.ppt
Chapter5.pptChapter5.ppt
Chapter5.ppt
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
01 basic orders
01   basic orders01   basic orders
01 basic orders
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Sql clauses by Manan Pasricha
Sql clauses by Manan PasrichaSql clauses by Manan Pasricha
Sql clauses by Manan Pasricha
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
 
Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Sql
SqlSql
Sql
 
DB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptxDB_lecturs8 27 11.pptx
DB_lecturs8 27 11.pptx
 
Sql functions
Sql functionsSql functions
Sql functions
 
Les18
Les18Les18
Les18
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Dbms presentationnnnn
Dbms presentationnnnnDbms presentationnnnn
Dbms presentationnnnn
 
Query
QueryQuery
Query
 

Recently uploaded

一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
Tiktokethiodaily
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
MaleehaSheikh2
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 

Recently uploaded (20)

一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 

Unit 3-Select Options and Aggregate Functions in SQL (1).pptx

  • 1. • Select Statement • Select command is used to fetch the data in a set of records from a table, view or a group of tables, views by making use of SQL joins. • Retrieval of data using SQL statements can be done by using different predicates like − • Where • Group By • Having • Order By Basic select : Select * from student; Name Regno Branch Age Hari 100 CSE 15 Pinky 101 CSE 17 Bob 102 CSE 14 Bhanu 103 CSE 18
  • 2. Where clause • Where clause is used with the data manipulation language (DML) statement to check for a condition being met in row. • Example 1 • The query given below displays the students’ records whose age is in between 15 and 20. SELECT * FROM student where age>15 and age<20; (OR) SELECT * FROM student where age between 15 and 20; Name Regno Branch Age Pinky 101 CSE 17 Bhanu 103 CSE 18
  • 3. Example 2 • Consider another example to know more about where clause − • Like operator is used to search for a specified pattern in a column. The percent sign (%) represents zero, one, or multiple characters The underscore sign (_) represents one, single character The following SQL statement selects all students with a student Name that have "or" in any position: SELECT *FROM student where name LIKE '%or%’; The following SQL statement selects all students with a student name that have "r" in the second position SELECT *FROM student where name LIKE '_r%’;
  • 4. The following SQL statement selects all customers with a ContactName that starts with "a" and ends with "o": SELECT * FROM Customers WHERE ContactName LIKE 'a%o'; The following SQL statement selects all customers with a CustomerName that does NOT start with "a": SELECT * FROM Customers WHERE CustomerName NOT LIKE 'a%’; Example: SELECT *FROM student where name like B%; Name Regno Branch Age Bob 102 CSE 14 Bhanu 103 CSE 18
  • 5. SQL group by In SQL, The Group By statement is used for organizing similar data into groups. The data is further organized with the help of equivalent function. It means, if different rows in a precise column have the same values, it will arrange those rows in a group. •The SELECT statement is used with the GROUP BY clause in the SQL query. •WHERE clause is placed before the GROUP BY clause in SQL. •ORDER BY clause is placed after the GROUP BY clause in SQL. S.no Name AGE Salary 1 John 24 25000 2 Nick 22 22000 3 Amara 25 15000 4 Nick 22 22000 5 John 24 25000 SUBJECT YEAR NAME C language 2 John C language 2 Ginny C language 2 Jasmeen C language 3 Nick C language 3 Amara Java 1 Sifa Java 1 dolly
  • 6. 1.SELECT NAME, SUM (SALARY) FROM Employee 2.GROUP BY NAME; S.no Name AGE Salary 1 John 24 25000 2 Nick 22 22000 3 Amara 25 15000 4 Nick 22 22000 5 John 24 25000 NAME SALARY John 50000 Nick 44000 Amara 15000 SUBJECT YEAR NAME C language 2 John C language 2 Ginny C language 2 Jasmeen C language 3 Nick C language 3 Amara Java 1 Sifa Java 1 dolly 1.SELECT SUBJECT, YEAR, Count (*) 2.FROM Student 3.Group BY SUBJECT, YEAR; SUBJECT YEAR Count C language 2 3 C language 3 2 Java 1 2
  • 7. HAVING Clause WHERE clause is used for deciding purpose. It is used to place conditions on the columns to determine the part of the last result-set of the group. Here, we are not required to use the combined functions like COUNT (), SUM (), etc. with the WHERE clause. After that, we need to use a HAVING clause. Syntax: SELECT column1, function_name(column2) FROM table_name WHERE condition GROUP BY column1, column2 HAVING condition ORDER BY column1, column2; function_name: Mainly used for name of the function, SUM(), AVG(). table_name: Used for name of the table. condition: Condition used.
  • 8. HAVING Clause SELECT NAME, SUM(SALARY) FROM Employee GROUP BY NAME HAVING SUM(SALARY)>=50000; Name SUM(SALARY) John 50000
  • 10. • SQL Aggregate Functions • SQL aggregation function is used to perform the calculations on multiple rows of a single column of a table. It returns a single value. • It is also used to summarize the data. Types of SQL Aggregation Function
  • 11. COUNT FUNCTION • COUNT function is used to Count the number of rows in a database table. It can work on both numeric and non-numeric data types. • COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table. COUNT(*) considers duplicate and Null. Syntax: COUNT(*) or COUNT( [ALL|DISTINCT] expression )
  • 12. • Sample table: • PRODUCT_MAST Output: 10 PRODUCT COMPANY QTY RATE COST Item1 Com1 2 10 20 Item2 Com2 3 25 75 Item3 Com1 2 30 60 Item4 Com3 5 10 50 Item5 Com2 2 20 40 Item6 Cpm1 3 25 75 Item7 Com1 5 30 150 Item8 Com1 3 10 30 Item9 Com2 2 25 50 Item10 Com3 4 30 120 Example: COUNT() SELECT COUNT(*) FROM PRODUCT_MAST Example: COUNT with WHERE SELECT COUNT(*) FROM PRODUCT_MAST; WHERE RATE>=20; Output: 7
  • 13. • Sample table: • PRODUCT_MAST PRODUC T COMPAN Y QTY RATE COST Item1 Com1 2 10 20 Item2 Com2 3 25 75 Item3 Com1 2 30 60 Item4 Com3 5 10 50 Item5 Com2 2 20 40 Item6 Cpm1 3 25 75 Item7 Com1 5 30 150 Item8 Com1 3 10 30 Item9 Com2 2 25 50 Item10 Com3 4 30 120 Example: COUNT() with DISTINCT SELECT COUNT(DISTINCT COMPAN Y) FROM PRODUCT_MAST; Output: 3 Example: COUNT() with GROUP BY SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY; Output: Com1 5 Com2 3
  • 14. • Sample table: • PRODUCT_MAST PRODUC T COMPAN Y QTY RATE COST Item1 Com1 2 10 20 Item2 Com2 3 25 75 Item3 Com1 2 30 60 Item4 Com3 5 10 50 Item5 Com2 2 20 40 Item6 Cpm1 3 25 75 Item7 Com1 5 30 150 Item8 Com1 3 10 30 Item9 Com2 2 25 50 Item10 Com3 4 30 120 Example: COUNT() with HAVING SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY HAVING COUNT(*)>2; Output: Com1 5 Com2 3
  • 15. 2. SUM Function • Sum function is used to calculate the sum of all selected columns. It works on numeric fields only. Syntax SUM() or SUM( [ALL|DISTINCT] expression ) Example: SUM() SELECT SUM(COST) FROM PRODUCT_MAST; Output: 670 PROD UCT COM PANY QTY RATE COST Item1 Com1 2 10 20 Item2 Com2 3 25 75 Item3 Com1 2 30 60 Item4 Com3 5 10 50 Item5 Com2 2 20 40 Item6 Cpm1 3 25 75 Item7 Com1 5 30 150 Item8 Com1 3 10 30 Item9 Com2 2 25 50 Item10 Com3 4 30 120
  • 16. Example: SUM() with WHERE SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3; Output:320 Example: SUM() with GROUP BY SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3 GROUP BY COMPANY; Output: Com1 150 Com2 170 PROD UCT COM PANY QTY RATE COST Item1 Com1 2 10 20 Item2 Com2 3 25 75 Item3 Com1 2 30 60 Item4 Com3 5 10 50 Item5 Com2 2 20 40 Item6 Cpm1 3 25 75 Item7 Com1 5 30 150 Item8 Com1 3 10 30 Item9 Com2 2 25 50 Item10 Com3 4 30 120
  • 17. • Example: SUM() with HAVING SELECT COMPANY, SUM(COST) FROM PRODUCT_MAST GROUP BY COMPANY HAVING SUM(COST)>=170; Output: Com1 335 Com3 170 PROD UCT COM PANY QTY RATE COST Item1 Com1 2 10 20 Item2 Com2 3 25 75 Item3 Com1 2 30 60 Item4 Com3 5 10 50 Item5 Com2 2 20 40 Item6 Cpm1 3 25 75 Item7 Com1 5 30 150 Item8 Com1 3 10 30 Item9 Com2 2 25 50 Item10 Com3 4 30 120
  • 18. • 3. AVG function • The AVG function is used to calculate the average value of the numeric type. AVG function returns the average of all non-Null values. • Syntax AVG() or AVG( [ALL|DISTINCT] expression ) Example: SELECT AVG(COST) FROM PRODUCT_MAST; Output: 67.00
  • 19. 4. MAX Function • The MAX function is used to find the maximum value of a certain column. This function determines the largest value of all selected values of a column. • Syntax MAX() or MAX( [ALL|DISTINCT] expression ) Example: SELECT MAX(RATE) FROM PRODUCT_MAST; Output:30
  • 20. 5. MIN Function • MIN function is used to find the minimum value of a certain column. This function determines the smallest value of all selected values of a column. • Syntax MIN() or MIN( [ALL|DISTINCT] expression ) • Example: SELECT MIN(RATE) FROM PRODUCT_MAST; Output: 10