SlideShare a Scribd company logo
Aggregate functions in SQL
Sherin Rappai
Department of Computer Science
III SEM BCA
What is aggregate function?
• An aggregate function in SQL performs a calculation on multiple values and
returns a single value. SQL provides many aggregate functions that include avg,
count, sum, min, max, etc. An aggregate function ignores NULL values when it
performs the calculation, except for the count function.
• What is Aggregate Function in SQL?
• An aggregate function in SQL returns one value after calculating multiple values
of a column. We often use aggregate functions with the GROUP BY and HAVING
clauses of the SELECT statement.
• Various types of SQL aggregate functions are:
• Count()
• Sum()
• Avg()
• Min()
• Max()
COUNT FUNCTION
• The COUNT() function returns the number of rows in a database table.
• Syntax:
• COUNT(*)
• or
• COUNT( [ALL|DISTINCT] expression )
Sum Function
• The SUM() function returns the total sum of a numeric column.
• Syntax:
• SUM()
• or
• SUM( [ALL|DISTINCT] expression )
AVG Function
• The AVG() function calculates the average of a set of values.
• Syntax:
• AVG()
• or
• AVG( [ALL|DISTINCT] expression )
MIN Function
• The MIN() aggregate function returns the lowest value (minimum) in a set of non-
NULL values.
• Syntax:
• MIN()
• or
• MIN( [ALL|DISTINCT] expression )
MAX Function
• The MAX() aggregate function returns the highest value (maximum) in a set of
non-NULL values.
• Syntax:
• MAX()
• or
• MAX( [ALL|DISTINCT] expression )
VIEWS IN SQL
• In SQL, a view is a virtual table based on the result-set of an SQL statement.
• A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
• You can add SQL statements and functions to a view and present the data as if the
data were coming from one single table.
• A view is created with the CREATE VIEW statement.
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
DELETING VIEWS
UPDATING VIEWS
• There are certain conditions needed to be satisfied to update a view. If any
one of these conditions is not met, then we will not be allowed to update the
view.
• The SELECT statement which is used to create the view should not include
GROUP BY clause or ORDER BY clause.
• The SELECT statement should not have the DISTINCT keyword.
• The View should have all NOT NULL values.
• The view should not be created using nested queries or complex queries.
• The view should be created from a single table. If the view is created using
multiple tables then we will not be allowed to update the view.
CREATE OR REPLACE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS, StudentMarks.AGE
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
For example, if we want to update the view MarksView and add the field AGE to this View
from StudentMarks Table, we can do this as:
INSERTING VALUES IN VIEWS
DELETING A ROW FROM VIEW
WITH CHECK OPTION
• The WITH CHECK OPTION clause in SQL is a very useful clause for
views. It is applicable to a updatable view. If the view is not updatable, then
there is no meaning of including this clause in the CREATE VIEW
statement.
• The WITH CHECK OPTION clause is used to prevent the insertion of rows
in the view where the condition in the WHERE clause in CREATE VIEW
statement is not satisfied.
• If we have used the WITH CHECK OPTION clause in the CREATE VIEW
statement, and if the UPDATE or INSERT clause does not satisfy the
conditions then they will return an error.
Aggregate functions in SQL.pptx
JOINS IN SQL
• A JOIN clause is used to combine rows from two or more tables, based on a
related column between them.
SELECT Orders.OrderID, Customers.CustomerName,
Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Custo
mers.CustomerID;
• Different Types of SQL JOINs:
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched
records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a match in either left or
right table
SQL INNER JOIN Keyword
• The INNER JOIN keyword selects records that have matching values in both
tables.
• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
• Example:
• SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
SQL LEFT JOIN Keyword
• The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records from the right table (table2). The result is 0 records from the
right side, if there is no match.
• Syntax:
• SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
• Example:
• SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL RIGHT JOIN Keyword
• The RIGHT JOIN keyword returns all records from the right table (table2), and
the matching records from the left table (table1). The result is 0 records from the
left side, if there is no match.
• Syntax:
• SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
• Example:
• SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
SQL FULL OUTER JOIN Keyword
• The FULL OUTER JOIN keyword returns all records when there is a match in left
(table1) or right (table2) table records.
• Syntax:
• SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
• Example:
• SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL SELF JOIN
• A self join is a regular join, but the table is joined with itself.
• Syntax
• SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
Constraint
• Integrity Constraints are the rules enforced on the data columns of a table. These
are used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the database.
• Constraints could be either on a column level or a table level. The column level
constraints are applied only to one column, whereas the table level constraints are
applied to the whole table.
• Following are some of the most commonly used constraints available in SQL.
• NOT NULL Constraint − Ensures that a column cannot have NULL value.
• DEFAULT Constraint − Provides a default value for a column when none is specified.
• UNIQUE Constraint − Ensures that all values in a column are different.
• PRIMARY Key − Uniquely identifies each row/record in a database table.
• FOREIGN Key − Uniquely identifies a row/record in any of the given database table.
• CHECK Constraint − The CHECK constraint ensures that all the values in a column satisfies certain
conditions.
• INDEX − Used to create and retrieve data from the database very quickly.
• Constraints can be specified when a table is created with the CREATE TABLE statement or you can use the
ALTER TABLE statement to create constraints even after the table is created.
• A primary key is a field in a database table that uniquely identifies each row/record. This
is also one type of Integrity Constraint.
• Primary keys must have distinct values. Null values are not allowed in a primary key
column. A table can only have one primary key, which can be made up of one or more
fields. It creates a composite key when several fields are used as a primary key.
• Foreign keys help ensure the consistency of your data while providing some ease. This is
also a type of integrity constraint. You are responsible for keeping track of inter-table
dependencies and preserving their consistency from within your applications .
• The not null constraint tells a column that it can't have any null values in it. This is also
a type of integrity constraint. This forces a field to always have a value, meaning you can't
create a new record or change an existing one without adding a value to it.
• A collection of one or more table fields/columns that uniquely identify a record in a
database table is known as a unique key. This is also a type of integrity constraint. It’s
similar to a primary key, but it can only accept one null value and cannot have duplicate
values. A Unique key is generated automatically.
DROPPING A CONSTRAINT
• Any constraint that you have defined can be dropped using the ALTER TABLE command with the
DROP CONSTRAINT option.
• For example, to drop the primary key constraint in the EMPLOYEES table, you can use the
following command.
• ALTER TABLE EMPLOYEES DROP CONSTRAINT EMPLOYEES_PK;
• Some implementations may provide shortcuts for dropping certain constraints. For example, to
drop the primary key constraint for a table in Oracle, you can use the following command.
• ALTER TABLE EMPLOYEES DROP PRIMARY KEY;
Ad

More Related Content

Similar to Aggregate functions in SQL.pptx (20)

2..basic queries.pptx
2..basic queries.pptx2..basic queries.pptx
2..basic queries.pptx
MalaikaRahatQurashi
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
Simplilearn
 
UNIT2.ppt
UNIT2.pptUNIT2.ppt
UNIT2.ppt
SaurabhLokare1
 
SQL Views
SQL ViewsSQL Views
SQL Views
baabtra.com - No. 1 supplier of quality freshers
 
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldfView od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
talhahakeem295
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
SneaK3
 
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
 
Sql
SqlSql
Sql
RittikaBaksi
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database QueryingTech Jam 01 - Database Querying
Tech Jam 01 - Database Querying
Rodger Oates
 
3. ddl create
3. ddl create3. ddl create
3. ddl create
Amrit Kaur
 
SignalR & SQL Dependency
SignalR & SQL DependencySignalR & SQL Dependency
SignalR & SQL Dependency
Narato
 
INTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEFINTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
Gurpreet singh
 
apply Integrity constraints on database table
apply Integrity constraints on database tableapply Integrity constraints on database table
apply Integrity constraints on database table
prachi gat
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
DraguClaudiu
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
nimsarabuwaa2002
 
Structure query language, database course
Structure query language, database courseStructure query language, database course
Structure query language, database course
yunussufyan2024
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
Brian Foote
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
uncleRhyme
 
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
SQL INterview Questions .pTop 45 SQL Interview Questions And Answers In 2025 ...
Simplilearn
 
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldfView od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
View od dffmfmfmm,dm,f,dm,dfm,dddfdfsd,sd,sddf,df,ldf
talhahakeem295
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
SneaK3
 
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
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database QueryingTech Jam 01 - Database Querying
Tech Jam 01 - Database Querying
Rodger Oates
 
SignalR & SQL Dependency
SignalR & SQL DependencySignalR & SQL Dependency
SignalR & SQL Dependency
Narato
 
INTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEFINTRODUCTION TO SQL QUERIES REALTED BRIEF
INTRODUCTION TO SQL QUERIES REALTED BRIEF
VADAPALLYPRAVEENKUMA1
 
apply Integrity constraints on database table
apply Integrity constraints on database tableapply Integrity constraints on database table
apply Integrity constraints on database table
prachi gat
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
DraguClaudiu
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
4 SQL DML.pptx ASHEN WANNIARACHCHI USESS
nimsarabuwaa2002
 
Structure query language, database course
Structure query language, database courseStructure query language, database course
Structure query language, database course
yunussufyan2024
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
Brian Foote
 

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Ad

Aggregate functions in SQL.pptx

  • 1. Aggregate functions in SQL Sherin Rappai Department of Computer Science III SEM BCA
  • 2. What is aggregate function? • An aggregate function in SQL performs a calculation on multiple values and returns a single value. SQL provides many aggregate functions that include avg, count, sum, min, max, etc. An aggregate function ignores NULL values when it performs the calculation, except for the count function. • What is Aggregate Function in SQL? • An aggregate function in SQL returns one value after calculating multiple values of a column. We often use aggregate functions with the GROUP BY and HAVING clauses of the SELECT statement. • Various types of SQL aggregate functions are: • Count() • Sum() • Avg() • Min() • Max()
  • 3. COUNT FUNCTION • The COUNT() function returns the number of rows in a database table. • Syntax: • COUNT(*) • or • COUNT( [ALL|DISTINCT] expression )
  • 4. Sum Function • The SUM() function returns the total sum of a numeric column. • Syntax: • SUM() • or • SUM( [ALL|DISTINCT] expression )
  • 5. AVG Function • The AVG() function calculates the average of a set of values. • Syntax: • AVG() • or • AVG( [ALL|DISTINCT] expression )
  • 6. MIN Function • The MIN() aggregate function returns the lowest value (minimum) in a set of non- NULL values. • Syntax: • MIN() • or • MIN( [ALL|DISTINCT] expression )
  • 7. MAX Function • The MAX() aggregate function returns the highest value (maximum) in a set of non-NULL values. • Syntax: • MAX() • or • MAX( [ALL|DISTINCT] expression )
  • 8. VIEWS IN SQL • In SQL, a view is a virtual table based on the result-set of an SQL statement. • A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. • You can add SQL statements and functions to a view and present the data as if the data were coming from one single table. • A view is created with the CREATE VIEW statement.
  • 12. UPDATING VIEWS • There are certain conditions needed to be satisfied to update a view. If any one of these conditions is not met, then we will not be allowed to update the view. • The SELECT statement which is used to create the view should not include GROUP BY clause or ORDER BY clause. • The SELECT statement should not have the DISTINCT keyword. • The View should have all NOT NULL values. • The view should not be created using nested queries or complex queries. • The view should be created from a single table. If the view is created using multiple tables then we will not be allowed to update the view.
  • 13. CREATE OR REPLACE VIEW MarksView AS SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS, StudentMarks.AGE FROM StudentDetails, StudentMarks WHERE StudentDetails.NAME = StudentMarks.NAME; For example, if we want to update the view MarksView and add the field AGE to this View from StudentMarks Table, we can do this as:
  • 15. DELETING A ROW FROM VIEW
  • 16. WITH CHECK OPTION • The WITH CHECK OPTION clause in SQL is a very useful clause for views. It is applicable to a updatable view. If the view is not updatable, then there is no meaning of including this clause in the CREATE VIEW statement. • The WITH CHECK OPTION clause is used to prevent the insertion of rows in the view where the condition in the WHERE clause in CREATE VIEW statement is not satisfied. • If we have used the WITH CHECK OPTION clause in the CREATE VIEW statement, and if the UPDATE or INSERT clause does not satisfy the conditions then they will return an error.
  • 18. JOINS IN SQL • A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
  • 19. SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate FROM Orders INNER JOIN Customers ON Orders.CustomerID=Custo mers.CustomerID;
  • 20. • Different Types of SQL JOINs: • (INNER) JOIN: Returns records that have matching values in both tables • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
  • 21. SQL INNER JOIN Keyword • The INNER JOIN keyword selects records that have matching values in both tables. • SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; • Example: • SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
  • 22. SQL LEFT JOIN Keyword • The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match. • Syntax: • SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name; • Example: • SELECT Customers.CustomerName, Orders.OrderID FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID ORDER BY Customers.CustomerName;
  • 23. SQL RIGHT JOIN Keyword • The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the left table (table1). The result is 0 records from the left side, if there is no match. • Syntax: • SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name; • Example: • SELECT Orders.OrderID, Employees.LastName, Employees.FirstName FROM Orders RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID ORDER BY Orders.OrderID;
  • 24. SQL FULL OUTER JOIN Keyword • The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records. • Syntax: • SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name = table2.column_name WHERE condition; • Example: • SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY Customers.CustomerName;
  • 25. SQL SELF JOIN • A self join is a regular join, but the table is joined with itself. • Syntax • SELECT column_name(s) FROM table1 T1, table1 T2 WHERE condition;
  • 26. Constraint • Integrity Constraints are the rules enforced on the data columns of a table. These are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the database. • Constraints could be either on a column level or a table level. The column level constraints are applied only to one column, whereas the table level constraints are applied to the whole table. • Following are some of the most commonly used constraints available in SQL. • NOT NULL Constraint − Ensures that a column cannot have NULL value. • DEFAULT Constraint − Provides a default value for a column when none is specified. • UNIQUE Constraint − Ensures that all values in a column are different. • PRIMARY Key − Uniquely identifies each row/record in a database table. • FOREIGN Key − Uniquely identifies a row/record in any of the given database table. • CHECK Constraint − The CHECK constraint ensures that all the values in a column satisfies certain conditions. • INDEX − Used to create and retrieve data from the database very quickly. • Constraints can be specified when a table is created with the CREATE TABLE statement or you can use the ALTER TABLE statement to create constraints even after the table is created.
  • 27. • A primary key is a field in a database table that uniquely identifies each row/record. This is also one type of Integrity Constraint. • Primary keys must have distinct values. Null values are not allowed in a primary key column. A table can only have one primary key, which can be made up of one or more fields. It creates a composite key when several fields are used as a primary key. • Foreign keys help ensure the consistency of your data while providing some ease. This is also a type of integrity constraint. You are responsible for keeping track of inter-table dependencies and preserving their consistency from within your applications . • The not null constraint tells a column that it can't have any null values in it. This is also a type of integrity constraint. This forces a field to always have a value, meaning you can't create a new record or change an existing one without adding a value to it. • A collection of one or more table fields/columns that uniquely identify a record in a database table is known as a unique key. This is also a type of integrity constraint. It’s similar to a primary key, but it can only accept one null value and cannot have duplicate values. A Unique key is generated automatically.
  • 28. DROPPING A CONSTRAINT • Any constraint that you have defined can be dropped using the ALTER TABLE command with the DROP CONSTRAINT option. • For example, to drop the primary key constraint in the EMPLOYEES table, you can use the following command. • ALTER TABLE EMPLOYEES DROP CONSTRAINT EMPLOYEES_PK; • Some implementations may provide shortcuts for dropping certain constraints. For example, to drop the primary key constraint for a table in Oracle, you can use the following command. • ALTER TABLE EMPLOYEES DROP PRIMARY KEY;