Ex - No.: 2 Date:: Aggregate Functions, Joins, Views and Subqueries
Ex - No.: 2 Date:: Aggregate Functions, Joins, Views and Subqueries
: 2
Aggregate functions, Joins, Views and Subqueries
Date:
AGGREGATE FUNCTIONS
These functions are used to do operations from the values of the column and a single value is
returned.
AVG (): It returns average value after calculating from values in a numeric column.
Syntax:
COUNT (): It is used to count the number of rows returned in a SELECT statement.
Syntax:
MAX (): The MAX () function returns the maximum value of the selected column.
Syntax:
MIN (): The MIN () function returns the minimum value of the selected column.
Syntax:
1
SUM (): The SUM () function returns the sum of all the values of the selected column.
Syntax:
JOINS
Step 2: Check for the equality on common attributes for the given tables.
NATURAL JOIN:
It returns the matching rows from the table that are being joined.
Syntax:
Eg:
INNER JOIN:
It returns the matching rows from the tables that are being joined.
Syntax:
2
>select <attribute> from TN1 innerjoin TN2 on TN1.attribute=TN2.attribute.
Eg:
OUTER JOIN:
It returns all the rows from the table1 even when they are unmatched.
Syntax:
Eg:
> select emp.empname,city, salary from emp left outer join empsalary on
emp.empname=empsalary.empname;
It returns all the rows from the table2 even when they are unmatched.
Syntax:
Eg:
3
FULL JOIN:
Syntax:
Eg:
VIEWS
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.
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.
4
SQL UPDATE Syntax
UPDATE view_name SET column_name=values WHERE condition
SUB QUERIES
Subquery or Inner query or Nested query is a query in a query. Subqueries are an alternate way of
returning data from multiple tables.
5
3. WHERE EXISTS (subquery)
CORRELATED SUBQUERY
A query is called correlated subquery when both the inner query and the outer query are
interdependent. For every row processed by the inner query, the outer query is processed as
well. The inner query depends on the outer query before it can be processed.
Result