2. The SQL Aggregate Functions:
MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
Min , MAX() Syntax
SELECT Aggregate function (MAX/ Min) (column_name)
FROM table_name
WHERE condition;
4. SQL Aggregate Functions :COUNT(), AVG()
and SUM() Functions
The COUNT() function returns the number of rows that matches a specified criterion.
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.
Syntax
SELECT Aggregate Function (column_name)
FROM table_name
WHERE condition;
6. SQL GROUP BY Statement
The GROUP BY statement groups rows that have the same values into
summary rows, like "find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT,
MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
9. SQL HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not
be used with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
11. Examples
For each department display a list of deptno that have more than 3 employees
SELECT deptno, COUNT(*)
FROM Emp
GROUP BY deptno
Having COUNT(*)>3;
For each department display a list of deptno that have at lest 3 employees working as
SALESMAN
SELECT deptno, COUNT(*)
FROM Emp
WHERE Job=‘SALESMAN’
GROUP BY deptno
Having COUNT(*)>3;
12. SQL Joins
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
Different Types of SQL JOINs
Here are the different types of the JOINs in SQL:
(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
13.
14. SQL INNER JOIN Keyword
The INNER JOIN keyword selects records that have matching values in both
tables.
INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
16. SQL FULL OUTER JOIN Keyword
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.
Note: FULL OUTER JOIN can potentially return very large result-sets!
Tip: FULL OUTER JOIN and FULL JOIN are the same.
FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
18. SQL JOIN
Used to joining more than 2 tables
JOIN Syntax
SELECT column_name(s)
FROM table1 , table2,table3
WHERE condition;
19.
20. Examples
Display list of projects names and name of employees assigned to it
SELECT pname,fname
FROM Employee, project, works_on
WHERE ssn=Essn and pno=pnumber;
For each department display a list of department name and maximum salary of
this department.
SELECT dname,max(Salary)
FROM Department , Employee
WHERE Dno=Dnumber
Group By dname;
21. Examples
Customer(Customer ID, Name, Address, Job)
Transaction (Trans ID,Customer ID, Account#, Trans Type, Date, Amount )
Get the total amount of each transaction type
SELECT Transaction_Type, SUM(Amount)
FROM Transaction
GROUP BY (Transaction_Type)
Get the total amount of each transaction type for customer ID = 20
SELECT Transaction_Type, SUM(Amount)
FROM Transaction
GROUP BY (Transaction_Type)
HAVING Customer_ID=20
22. Examples
Get the number of accounts for each Customer
SELECT Customer_Id, COUNT(Account#)
FROM Transaction
GROUP BY (Customer_Id)
Get the number of accounts for each Customer Only include customers
with more than or equal 2 accounts
SELECT Customer_Id, COUNT(Account#)
FROM Transaction
GROUP BY (Customer_Id)
HAVING COUNT(Account#)>=2
23. Examples
Get the Customer ID of all customers who performed transaction type
‘Deposit’ but never performed ‘withdraw’?
SELECR Customer_ID
FROM Transaction
WHERE transaction_Type = ‘Deposit'
EXCEPT
SELECT Customer_ID
FROM Transaction
WHERE Transcation_Type =‘Withdraw'