2. What is aggregate function?
• An aggregate function in SQL performs a calculation on multiple values and
returns a single value. SQL provides many aggregate functions that include avg,
count, sum, min, max, etc. An aggregate function ignores NULL values when it
performs the calculation, except for the count function.
• What is Aggregate Function in SQL?
• An aggregate function in SQL returns one value after calculating multiple values
of a column. We often use aggregate functions with the GROUP BY and HAVING
clauses of the SELECT statement.
• Various types of SQL aggregate functions are:
• Count()
• Sum()
• Avg()
• Min()
• Max()
3. COUNT FUNCTION
• The COUNT() function returns the number of rows in a database table.
• Syntax:
• COUNT(*)
• or
• COUNT( [ALL|DISTINCT] expression )
4. Sum Function
• The SUM() function returns the total sum of a numeric column.
• Syntax:
• SUM()
• or
• SUM( [ALL|DISTINCT] expression )
5. AVG Function
• The AVG() function calculates the average of a set of values.
• Syntax:
• AVG()
• or
• AVG( [ALL|DISTINCT] expression )
6. MIN Function
• The MIN() aggregate function returns the lowest value (minimum) in a set of non-
NULL values.
• Syntax:
• MIN()
• or
• MIN( [ALL|DISTINCT] expression )
7. MAX Function
• The MAX() aggregate function returns the highest value (maximum) in a set of
non-NULL values.
• Syntax:
• MAX()
• or
• MAX( [ALL|DISTINCT] expression )
8. VIEWS IN SQL
• In SQL, a view is a virtual table based on the result-set of an SQL statement.
• A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
• You can add SQL statements and functions to a view and present the data as if the
data were coming from one single table.
• A view is created with the CREATE VIEW statement.
12. UPDATING VIEWS
• There are certain conditions needed to be satisfied to update a view. If any
one of these conditions is not met, then we will not be allowed to update the
view.
• The SELECT statement which is used to create the view should not include
GROUP BY clause or ORDER BY clause.
• The SELECT statement should not have the DISTINCT keyword.
• The View should have all NOT NULL values.
• The view should not be created using nested queries or complex queries.
• The view should be created from a single table. If the view is created using
multiple tables then we will not be allowed to update the view.
13. CREATE OR REPLACE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS, StudentMarks.MARKS, StudentMarks.AGE
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
For example, if we want to update the view MarksView and add the field AGE to this View
from StudentMarks Table, we can do this as:
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.
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;