0% found this document useful (0 votes)
6 views6 pages

Ex - No.: 2 Date:: Aggregate Functions, Joins, Views and Subqueries

The document provides an overview of SQL concepts including aggregate functions, joins, views, and subqueries. It details various aggregate functions like AVG, COUNT, MAX, MIN, and SUM, along with their syntax. Additionally, it explains different types of joins (NATURAL, INNER, OUTER, LEFT, RIGHT, FULL), the creation and management of views, and the use of subqueries in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Ex - No.: 2 Date:: Aggregate Functions, Joins, Views and Subqueries

The document provides an overview of SQL concepts including aggregate functions, joins, views, and subqueries. It details various aggregate functions like AVG, COUNT, MAX, MIN, and SUM, along with their syntax. Additionally, it explains different types of joins (NATURAL, INNER, OUTER, LEFT, RIGHT, FULL), the creation and management of views, and the use of subqueries in SQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex.no.

: 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:

SELECT AVG (column_name) FROM table_name;

COUNT (): It is used to count the number of rows returned in a SELECT statement.

Syntax:

SELECT COUNT (column_name) FROM table_name;

MAX (): The MAX () function returns the maximum value of the selected column.

Syntax:

SELECT MAX (column_name) FROM table_name;

MIN (): The MIN () function returns the minimum value of the selected column.

Syntax:

SELECT MIN (column_name) FROM table_name;

1
SUM (): The SUM () function returns the sum of all the values of the selected column.

Syntax:

SELECT SUM (column_name) FROM table_name;

JOINS

Join is a query in which data is returned from two or more tables.

How the join will be performed:

Step 1: Make the Cartesian product of the given tables.

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:

>select <attribute> from TN where TN1.attribute=TN2.attribute.

Eg:

> select emp.empname,city,salary from emp,empsalary where


emp.empname=empsalary.empname;

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:

>select emp.empname,city, salary from emp inner join empsalary on


emp.empname=empsalary.empname;

OUTER JOIN:

LEFT OUTER JOIN:

It returns all the rows from the table1 even when they are unmatched.

Syntax:

1. select <attribute> from TN1 left outer join TN2 on TN1.attribute=TN2.attribute.


2. select <attribute> from TN where TN1.attribute(+)=TN2.attribute.

Eg:

> select emp.empname,city, salary from emp left outer join empsalary on
emp.empname=empsalary.empname;

RIGHT OUTER JOIN:

It returns all the rows from the table2 even when they are unmatched.

Syntax:

1. select <attribute> from TN1 right outer join TN2 on TN1.attribute=TN2.attribute.


2. select <attribute> from TN where TN1.attribute=(+)TN2.attribute.

Eg:

> select emp.empname,city,salary from emp right outer join empsalary on


emp.empname=empsalary.empname;

3
FULL JOIN:

It is the combination of both left outer and right outer join.

Syntax:

>select <attribute> from TN1 full join TN2 on TN1.attribute=TN2.attribute.

Eg:

>select emp.empname,city,salary from emp full outer join empsalary on


emp.empname=empsalary.empname;

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.

SQL CREATE VIEW Statement

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.

SQL CREATE VIEW Syntax

CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition

SQL UPDATING VIEW Statement

You can update a view by using the following syntax:

4
SQL UPDATE Syntax
UPDATE view_name SET column_name=values WHERE condition

SQL DROPPING VIEW Statement

You can delete a view with the DROP VIEW command.

SQL DROP VIEW Syntax


DROP VIEW view_name

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.

SELECT column_name [, column_name ] FROM table1 [, table2 ] WHERE column_name


OPERATOR (SELECT column_name [, column_name ] FROM table1 [, table2 ] [WHERE])

SQL Subquery; SELECT Statement

1. WHERE column IN (subquery)

SELECT column_name,column_name FROM <tableName> WHERE column_name In

(SELECT column_name,column_name FROM <tableName> WHERE column_name operator value)

2. WHERE column <comparison> (subquery)

SELECT column_name,column_name FROM <tableName> WHERE column_name > or <

(SELECT column_name,column_name FROM <tableName> WHERE column_name operator value)

5
3. WHERE EXISTS (subquery)

SELECT column_name,column_name FROM <tableName> WHERE EXISTS

(SELECT column_name,column_name FROM <tableName> WHERE column_name operator value)

SQL Subquery; INSERT Statement

INSERT into <tableName>values (column_name1,column_name2 ,..)


SELECT column_name1,column_name2… FROM <tableName> WHERE column_name
operator value;

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.

SELECT column_name FROM <tableName1> WHERE column_name operator


(SELECT c1 column_name FROM <tableName2> c1 WHERE c1 column_name operator
column_name);

Result

You might also like