You are on page 1of 4

SQL Lab No.

03

SQL Aggregate Functions


SQL aggregate functions return a single value, calculated from values in a column.

Useful aggregate functions:

 AVG() - Returns the average value


 COUNT() - Returns the number of rows
 FIRST() - Returns the first value
 LAST() - Returns the last value
 MAX() - Returns the largest value
 MIN() - Returns the smallest value
 SUM() - Returns the sum

Department Table

Department department department


Dept_id Dept_name Dept_LOC
1 Accounting lahore
2 Research Islamabad
3 sales lahore
4 operations lahore
5 IT Islamabad
6 Finanace Multan

Employee

Emp_I Emp_First Emp_Last_ Phone


Emp_Address Emp_DOB Salary Commission
d _Name Name _No
1 Imran saleem lahore 9/7/2015 123123 500000 100
2 ali zafar lahore 1/1/2000 125125 750000 250
3 irfan ali lahore 1/1/1990 125125 80000 300
4 asad shohail faisalabad 10/15/1995 126126 2000
5 Zeshan ahmed multan 10/10/1998 127127 45000
6 Ayesha ahmed Islamanabad 12/15/1999 128128 90000 10,000
7 aleem akhtar multan 12/15/2001 129129 50000
8 ali hussain islamabad 10/10/1998 130130 50000
9 shahid hussain rawalpindi 1/1/1999 131131 100000 200,00
10 anam ali Lahore 1/5/1997 132132 120000
Example:
SQL AVG () function SELECT AVG(salary) as Averagesalary
SELECT AVG(column_name) FROM table_name FROM employee;

Example:
SQL COUNT(*) Syntax SELECT COUNT(*) as TotalRecords
The COUNT(*) function returns the number FROM employee;
of records in a table SELECT COUNT(*) as TotalRecords
FROM employee
SELECT COUNT(*) FROM table_name; Where salary>=50000;
Example:
SQL COUNT(column_name) Syntax SELECT COUNT(commission) as Total
FROM employee;
The COUNT(column_name) function returns
the number of values (NULL values will
not be counted) of the specified column:
SELECT Count(column_name) FROM table_name

Example:
The MAX() function returns the largest SELECT Max(salary) as MaximumSalary
value of the selected column. FROM employee;
Syntax:
SELECT MAX(column_name) FROM table_name;
Example:
The MIN() function returns the largest 1-SELECT Min(salary) as MinimumSalary
value of the selected column. FROM employee;
Syntax: 2-SELECT Min(salary) as MinimumSalary,
Min(commission) as Minimumcommission
SELECT MIN(column_name) FROM table_name; FROM employee;
1-SELECT SUM(salary) as summationSalary
The SUM() function returns the total sum FROM employee;
of a numeric column. 2-SELECT SUM(commission) as
Syntax: summofCommission
SELECT SUM(column_name) FROM table_name; FROM employee;

select distinct emp_address, sum(salary)


The GROUP BY Statement:
as Totalsalary
The GROUP BY statement is used in
from employee
conjunction with the aggregate functions to
group by emp_address;
group the result-set by one or more columns.
SQL JOIN
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN returns
all rows from multiple tables where the join condition is met.

Different SQL JOINs


Before we continue with examples, we will list the types of the different SQL JOINs you can use:

 INNER JOIN: Returns all rows when there is at least one match in BOTH tables
 LEFT JOIN: Return all rows from the left table, and the matched rows from the right
table
 RIGHT JOIN: Return all rows from the right table, and the matched rows from the left
table

SELECT column_name Example:


FROM table1 SELECT Dept_name, Dept_LOC,
INNER JOIN table2 Emp_First_Name, Salary
ON table1.column_name=table2.column_name; FROM department
INNER JOIN Employee
ON department.dept_id=employee.dept_id;
SELECT column_name(s) Example:
FROM table1 SELECT Dept_name, Dept_LOC,
LEFT JOIN table2 Emp_First_Name, Salary
ON table1.column_name=table2.column_name; FROM department
Left JOIN Employee
ON department.dept_id=employee.dept_id;
SELECT column_name(s) Example:
FROM table1 SELECT Dept_name, Dept_LOC,
RIGHT JOIN table2 Emp_First_Name, Salary
ON table1.column_name=table2.column_name; FROM department
right JOIN Employee
ON department.dept_id=employee.dept_id;

Sub Queries:
In Oracle, a sub query is a query within a query. You can create sub queries
within your SQL statements. These sub queries can reside in the WHERE clause,
the FROM clause, or the SELECT clause.
WHERE clause
Most often, the sub query will be found in the WHERE clause. These sub
queries are also called nested sub queries.

SELECT *
FROM all_tables tabs
WHERE tabs.table_name IN (SELECT cols.table_name
FROM all_tab_columns cols
WHERE cols.column_name = 'SUPPLIER_ID');

Select emp_first_name,emp_last_name

From employee

Where salary= (select max(salary) from employee);

Home Task: Write a query to find out the 2nd highest salary from employee
table.

Note: Quiz will be held in next class relating to SQL. (All topics)

You might also like