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

Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencyHire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencySheetal Arora
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PPRINCE C P
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfSumit Kumar yadav
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )aarthirajkumar25
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRDelhi Call girls
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticssakshisoni2385
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
fundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyfundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyDrAnita Sharma
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfSumit Kumar yadav
 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPirithiRaju
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfrohankumarsinghrore1
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisDiwakar Mishra
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000Sapana Sha
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)Areesha Ahmad
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡anilsa9823
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...Sérgio Sacani
 

Recently uploaded (20)

Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls AgencyHire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
Hire 💕 9907093804 Hooghly Call Girls Service Call Girls Agency
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C P
 
Zoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdfZoology 4th semester series (krishna).pdf
Zoology 4th semester series (krishna).pdf
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
fundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomologyfundamental of entomology all in one topics of entomology
fundamental of entomology all in one topics of entomology
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdf
 
Pests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdfPests of mustard_Identification_Management_Dr.UPR.pdf
Pests of mustard_Identification_Management_Dr.UPR.pdf
 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdf
 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
 
CELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdfCELL -Structural and Functional unit of life.pdf
CELL -Structural and Functional unit of life.pdf
 
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 60009654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
9654467111 Call Girls In Raj Nagar Delhi Short 1500 Night 6000
 
GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)GBSN - Microbiology (Unit 2)
GBSN - Microbiology (Unit 2)
 
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service  🪡
CALL ON ➥8923113531 🔝Call Girls Kesar Bagh Lucknow best Night Fun service 🪡
 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: “Eg...
 

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'