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.
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.
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;

More Related Content

Similar to Aggregate functions in SQL.pptx

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
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
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
Gurpreet singh
 
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
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
Brian Foote
 
Manipulating data
Manipulating dataManipulating data
Manipulating data
Syed Zaid Irshad
 
Joins & constraints
Joins & constraintsJoins & constraints
Joins & constraints
VENNILAV6
 
Integrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdfIntegrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdf
Saikrishna492522
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
Md.Mojibul Hoque
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
YashaswiniSrinivasan1
 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
Geetha Kannan
 
Java class 8
Java class 8Java class 8
Java class 8
Edureka!
 
SQL Assessment Command Statements
SQL Assessment Command StatementsSQL Assessment Command Statements
SQL Assessment Command Statements
Shaun Wilson
 
15925 structured query
15925 structured query15925 structured query
15925 structured query
Universitas Bina Darma Palembang
 
Lec 1 = introduction to structured query language (sql)
Lec 1 = introduction to structured query language (sql)Lec 1 = introduction to structured query language (sql)
Lec 1 = introduction to structured query language (sql)
Faisal Anwar
 

Similar to Aggregate functions in SQL.pptx (20)

Assignment 3
Assignment 3Assignment 3
Assignment 3
 
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
 
Sql
SqlSql
Sql
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database QueryingTech Jam 01 - Database Querying
Tech Jam 01 - Database Querying
 
3. ddl create
3. ddl create3. ddl create
3. ddl create
 
SignalR & SQL Dependency
SignalR & SQL DependencySignalR & SQL Dependency
SignalR & SQL Dependency
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
Manipulating data
Manipulating dataManipulating data
Manipulating data
 
Joins & constraints
Joins & constraintsJoins & constraints
Joins & constraints
 
Integrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdfIntegrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdf
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
ms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptxms-sql-server-150223140402-conversion-gate02.pptx
ms-sql-server-150223140402-conversion-gate02.pptx
 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
 
Java class 8
Java class 8Java class 8
Java class 8
 
SQL Assessment Command Statements
SQL Assessment Command StatementsSQL Assessment Command Statements
SQL Assessment Command Statements
 
15925 structured query
15925 structured query15925 structured query
15925 structured query
 
Lec 1 = introduction to structured query language (sql)
Lec 1 = introduction to structured query language (sql)Lec 1 = introduction to structured query language (sql)
Lec 1 = introduction to structured query language (sql)
 

Recently uploaded

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 

Recently uploaded (20)

ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 

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.
  • 9.
  • 10.
  • 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.
  • 17.
  • 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;