SlideShare a Scribd company logo
1 of 28
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 3SneaK3
 
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 knowPavithSingh
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database QueryingTech Jam 01 - Database Querying
Tech Jam 01 - Database QueryingRodger Oates
 
SignalR & SQL Dependency
SignalR & SQL DependencySignalR & SQL Dependency
SignalR & SQL DependencyNarato
 
SQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfDraguClaudiu
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL FundamentalsBrian Foote
 
Joins & constraints
Joins & constraintsJoins & constraints
Joins & constraintsVENNILAV6
 
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.pptxYashaswiniSrinivasan1
 
Java class 8
Java class 8Java class 8
Java class 8Edureka!
 
SQL Assessment Command Statements
SQL Assessment Command StatementsSQL Assessment Command Statements
SQL Assessment Command StatementsShaun Wilson
 
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
 
Introduction to Structured Query Language (SQL) (1).ppt
Introduction to Structured Query Language (SQL) (1).pptIntroduction to Structured Query Language (SQL) (1).ppt
Introduction to Structured Query Language (SQL) (1).pptComputerScienceDepar6
 

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
 
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)
 
Introduction to Structured Query Language (SQL) (1).ppt
Introduction to Structured Query Language (SQL) (1).pptIntroduction to Structured Query Language (SQL) (1).ppt
Introduction to Structured Query Language (SQL) (1).ppt
 

Recently uploaded

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

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;