SlideShare a Scribd company logo
1 of 23
Download to read offline
Structured Query
Language(SQL)
Assistant Lecturer Huda A. Alameen
hudaa.alameen@uokufa.edu.iq
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;
Examples
SELECT MIN(Price) AS SmallestPrice
FROM Products;
SELECT MAX(Price) AS LargestPrice
FROM Products;
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;
Examples
SELECT COUNT(ProductID)
FROM Products;
SELECT AVG(Price)
FROM Products;
SELECT SUM(Quantity)
FROM OrderDetails;
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);
Examples
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
Examples
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
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);
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
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;
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
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;
Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
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;
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SQL JOIN
 Used to joining more than 2 tables
 JOIN Syntax
SELECT column_name(s)
FROM table1 , table2,table3
WHERE condition;
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;
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
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
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'

More Related Content

What's hot (20)

MY SQL
MY SQLMY SQL
MY SQL
 
Sql cheat-sheet
Sql cheat-sheetSql cheat-sheet
Sql cheat-sheet
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statement
 
USING VLOOKUP FUNCTION
USING VLOOKUP FUNCTIONUSING VLOOKUP FUNCTION
USING VLOOKUP FUNCTION
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Si0302 20140320131934
Si0302 20140320131934Si0302 20140320131934
Si0302 20140320131934
 
Lab4 join - all types listed
Lab4   join - all types listedLab4   join - all types listed
Lab4 join - all types listed
 
Vlookup In Excel
Vlookup In ExcelVlookup In Excel
Vlookup In Excel
 
Clauses
ClausesClauses
Clauses
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
 
Sql wksht-1
Sql wksht-1Sql wksht-1
Sql wksht-1
 
Sql Tags
Sql TagsSql Tags
Sql Tags
 
SQL Keywords
SQL KeywordsSQL Keywords
SQL Keywords
 
Sql basics v2
Sql basics v2Sql basics v2
Sql basics v2
 
Les06
Les06Les06
Les06
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Sql functions
Sql functionsSql functions
Sql functions
 

Similar to Structured query language(sql) (20)

Module03
Module03Module03
Module03
 
dbms.pdf
dbms.pdfdbms.pdf
dbms.pdf
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
 
Sql
SqlSql
Sql
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptx
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Mysql1
Mysql1Mysql1
Mysql1
 
SQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptxSQLSERVERQUERIES.pptx
SQLSERVERQUERIES.pptx
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Query
QueryQuery
Query
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
 
Excel.useful fns
Excel.useful fnsExcel.useful fns
Excel.useful fns
 
V22 function-1
V22 function-1V22 function-1
V22 function-1
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 

More from Huda Alameen

More from Huda Alameen (20)

Architectural design
Architectural designArchitectural design
Architectural design
 
System Modeling
System ModelingSystem Modeling
System Modeling
 
Requirements Engineering
Requirements EngineeringRequirements Engineering
Requirements Engineering
 
Java Repetiotion Statements
Java Repetiotion StatementsJava Repetiotion Statements
Java Repetiotion Statements
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Java Print method
Java  Print methodJava  Print method
Java Print method
 
Softweare Engieering
Softweare Engieering Softweare Engieering
Softweare Engieering
 
Softweare Engieering
Softweare Engieering Softweare Engieering
Softweare Engieering
 
Sql viwes
Sql viwesSql viwes
Sql viwes
 
Relational algebra
Relational algebraRelational algebra
Relational algebra
 
Normalization
NormalizationNormalization
Normalization
 
Lecture one db
Lecture one dbLecture one db
Lecture one db
 
Introduction to structured query language
Introduction to structured query languageIntroduction to structured query language
Introduction to structured query language
 
Indexing techniques
Indexing techniquesIndexing techniques
Indexing techniques
 
Agg fun
Agg funAgg fun
Agg fun
 
Se lec1 (1)
Se lec1 (1)Se lec1 (1)
Se lec1 (1)
 
Se lec6
Se lec6Se lec6
Se lec6
 
Se lec5
Se lec5Se lec5
Se lec5
 
Se lec 4
Se lec 4Se lec 4
Se lec 4
 
Se lec 3
Se lec 3Se lec 3
Se lec 3
 

Recently uploaded

Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...lizamodels9
 
Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptArshadWarsi13
 
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptxSulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptxnoordubaliya2003
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxSwapnil Therkar
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024AyushiRastogi48
 
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)riyaescorts54
 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPirithiRaju
 
Pests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPirithiRaju
 
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxSTOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxMurugaveni B
 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxBerniceCayabyab1
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxNandakishor Bhaurao Deshmukh
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPirithiRaju
 
Microteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringMicroteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringPrajakta Shinde
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPirithiRaju
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsHajira Mahmood
 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensorsonawaneprad
 
User Guide: Magellan MX™ Weather Station
User Guide: Magellan MX™ Weather StationUser Guide: Magellan MX™ Weather Station
User Guide: Magellan MX™ Weather StationColumbia Weather Systems
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 

Recently uploaded (20)

Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
 
Transposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.pptTransposable elements in prokaryotes.ppt
Transposable elements in prokaryotes.ppt
 
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptxSulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
Sulphur & Phosphrus Cycle PowerPoint Presentation (2) [Autosaved]-3-1.pptx
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptxAnalytical Profile of Coleus Forskohlii | Forskolin .pptx
Analytical Profile of Coleus Forskohlii | Forskolin .pptx
 
Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024Vision and reflection on Mining Software Repositories research in 2024
Vision and reflection on Mining Software Repositories research in 2024
 
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
(9818099198) Call Girls In Noida Sector 14 (NOIDA ESCORTS)
 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
 
Pests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdfPests of Bengal gram_Identification_Dr.UPR.pdf
Pests of Bengal gram_Identification_Dr.UPR.pdf
 
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptxSTOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
STOPPED FLOW METHOD & APPLICATION MURUGAVENI B.pptx
 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
 
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptxTHE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
THE ROLE OF PHARMACOGNOSY IN TRADITIONAL AND MODERN SYSTEM OF MEDICINE.pptx
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
 
Microteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical EngineeringMicroteaching on terms used in filtration .Pharmaceutical Engineering
Microteaching on terms used in filtration .Pharmaceutical Engineering
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutions
 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensor
 
User Guide: Magellan MX™ Weather Station
User Guide: Magellan MX™ Weather StationUser Guide: Magellan MX™ Weather Station
User Guide: Magellan MX™ Weather Station
 
Volatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -IVolatile Oils Pharmacognosy And Phytochemistry -I
Volatile Oils Pharmacognosy And Phytochemistry -I
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 

Structured query language(sql)

  • 1. Structured Query Language(SQL) Assistant Lecturer Huda A. Alameen hudaa.alameen@uokufa.edu.iq
  • 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;
  • 3. Examples SELECT MIN(Price) AS SmallestPrice FROM Products; SELECT MAX(Price) AS LargestPrice FROM Products;
  • 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;
  • 5. Examples SELECT COUNT(ProductID) FROM Products; SELECT AVG(Price) FROM Products; SELECT SUM(Quantity) FROM OrderDetails;
  • 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);
  • 8. Examples SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country ORDER BY COUNT(CustomerID) DESC;
  • 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);
  • 10. Example SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
  • 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;
  • 15. Example SELECT Orders.OrderID, Customers.CustomerName FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
  • 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;
  • 17. Example SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY Customers.CustomerName;
  • 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'