SlideShare ist ein Scribd-Unternehmen logo
1 von 28
Aggregate functions in SQL
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;

Weitere ähnliche Inhalte

Was ist angesagt?

Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
Anurag
 
Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statements
Vivek Singh
 

Was ist angesagt? (20)

Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 
SQL
SQLSQL
SQL
 
SQL
SQLSQL
SQL
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
Triggers
TriggersTriggers
Triggers
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statements
 

Ähnlich wie Aggregate functions in SQL.pptx

Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 
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.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 -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdfSQL -Beginner To Intermediate Level.pdf
SQL -Beginner To Intermediate Level.pdf
DraguClaudiu
 

Ähnlich wie Aggregate functions in SQL.pptx (20)

Sql Tutorials
Sql TutorialsSql Tutorials
Sql Tutorials
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Views, Triggers, Functions, Stored Procedures,  Indexing and JoinsViews, Triggers, Functions, Stored Procedures,  Indexing and Joins
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
2..basic queries.pptx
2..basic queries.pptx2..basic queries.pptx
2..basic queries.pptx
 
Assignment 3
Assignment 3Assignment 3
Assignment 3
 
MS SQL Server
MS SQL ServerMS SQL Server
MS SQL Server
 
UNIT2.ppt
UNIT2.pptUNIT2.ppt
UNIT2.ppt
 
Sql
SqlSql
Sql
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
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
 
3. ddl create
3. ddl create3. ddl create
3. ddl create
 
Joins & constraints
Joins & constraintsJoins & constraints
Joins & constraints
 
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
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database QueryingTech Jam 01 - Database Querying
Tech Jam 01 - Database Querying
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
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
 
Manipulating data
Manipulating dataManipulating data
Manipulating data
 
SignalR & SQL Dependency
SignalR & SQL DependencySignalR & SQL Dependency
SignalR & SQL Dependency
 

Mehr von SherinRappai

A* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA studentsA* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA students
SherinRappai
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
SherinRappai
 

Mehr von SherinRappai (20)

Extensible markup language ppt as part of Internet Technology
Extensible markup language ppt as part of Internet TechnologyExtensible markup language ppt as part of Internet Technology
Extensible markup language ppt as part of Internet Technology
 
Java script ppt from students in internet technology
Java script ppt from students in internet technologyJava script ppt from students in internet technology
Java script ppt from students in internet technology
 
Building Competency and Career in the Open Source World
Building Competency and Career in the Open Source WorldBuilding Competency and Career in the Open Source World
Building Competency and Career in the Open Source World
 
How to Build a Career in Open Source.pptx
How to Build a Career in Open Source.pptxHow to Build a Career in Open Source.pptx
How to Build a Career in Open Source.pptx
 
Issues in Knowledge representation for students
Issues in Knowledge representation for studentsIssues in Knowledge representation for students
Issues in Knowledge representation for students
 
A* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA studentsA* algorithm of Artificial Intelligence for BCA students
A* algorithm of Artificial Intelligence for BCA students
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptxCOMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
COMPUTING AND PROGRAMMING FUNDAMENTAL.pptx
 
Clustering.pptx
Clustering.pptxClustering.pptx
Clustering.pptx
 
neuralnetwork.pptx
neuralnetwork.pptxneuralnetwork.pptx
neuralnetwork.pptx
 
Rendering Algorithms.pptx
Rendering Algorithms.pptxRendering Algorithms.pptx
Rendering Algorithms.pptx
 
Introduction to Multimedia.pptx
Introduction to Multimedia.pptxIntroduction to Multimedia.pptx
Introduction to Multimedia.pptx
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
system model.pptx
system model.pptxsystem model.pptx
system model.pptx
 
SE UNIT-1.pptx
SE UNIT-1.pptxSE UNIT-1.pptx
SE UNIT-1.pptx
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptx
 
Introduction to DBMS.pptx
Introduction to DBMS.pptxIntroduction to DBMS.pptx
Introduction to DBMS.pptx
 
Input_Output_Organization.pptx
Input_Output_Organization.pptxInput_Output_Organization.pptx
Input_Output_Organization.pptx
 

KĂźrzlich hochgeladen

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

KĂźrzlich hochgeladen (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Aggregate functions in SQL.pptx

  • 1. Aggregate functions in SQL 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;