SlideShare a Scribd company logo
Introduction to SQL server
Grouping, Scalar and aggregate functions, Joins
SQL Functions
• SQL has many built-in functions for
performing calculations on data. It can be
broadly classified in to two
–Aggregate Functions
– Scalar functions
SQL Functions
• Aggregate Functions
– SQL aggregate functions return a single value, calculated
from all the values in a column.
• Select AVG(int_price) from tbl_stock; // returns 8
• Select SUM(int_price) from tbl_stock; // returns 33
• Select MIN(int_price) from tbl_stock; // returns 2
• Select COUNT(vchr_product) from tbl_stock; // returns 4
Pk_int_id Vchr_product Int_price
1 Pen 10
2 Book 15
3 Eraser 2
4 Pencil 6
Tbl_stock Select SUM(int_price) from tbl_stock;
Returns a single value
SQL Functions
• Scalar Functions
– SQL scalar functions return a single value for each values of a
particular column given as input.
➢ Select UPPER(vchr_product) from tbl_stock;
➢ Select LOWER(vchr_product) from tbl_stock;
➢ Select ROUND(int_price) from tbl_stock;
Pk_int_id Vchr_product Int_price
1 Pen 10
2 Book 15
3 Eraser 2
4 Pencil 6
Tbl_product
Returns a value
Returns a value
Returns a value
Returns a value
Grouping Data
• GROUP BY allows you to take your result set, group it into
logical groups and then run aggregate queries on the groups.
• You could for instance select all employees, group them by
their workplace location and calculate the average salary. This
would give you the average salary of an employee at a given
location in your database.
Avg = 12500
Avg = 11500
Tbl_employee
Emp_id Emp_name Emp_age Emp_email int_salary vchr_place
1000 Deepak 24 dk@gmail.com 10000 Cochin
1001 Aneesh 23 an@gmail.com 20000 Calicut
1002 Naveen 25 nn@gmail.com 15000 Cochin
1003 Jacob 25 jb@gmail.com 13000 Calicut
Select vchr_place, avg(int_salary) from tbl_employee
group by vchr_place;
Example
Result :
Emp_id Emp_name Emp_age Emp_email int_salary vchr_place
1000 Deepak 24 dk@gmail.com 10000 Cochin
1001 Aneesh 23 an@gmail.com 20000 Calicut
1002 Naveen 25 nn@gmail.com 15000 Cochin
1003 Jacob 25 jb@gmail.com 13000 Calicut
Tbl_employee
Vchr_place Avg(int_alary)
Cochin 12500
Calicut 11500
The HAVING Clause
• The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.
• An Sql statement can have both ‘where’ clause and ‘having’
clause. Where filters data before grouping.Having filters data
after grouping
• Syntax:
• SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator
value;
Example
Select vchr_place, avg(int_salary) from tbl_employee group by
vchr_place having avg(int_salary)>12000;
Vchr_place Avg(int_alary)
Cochin 12500
Calicut 11500
Vchr_place Avg(int_alary)
Cochin 12500
JOINS
Where do we use joins?
• An SQL JOIN clause is used to combine rows from two or more
tables, based on a common field between them.
• Join helps to write single query to fetch data from multiple
tables so as to meet the business requirement/generate reports
Types Of joins
• Inner join
• Outer Join
– Left Outer Join
– Right Outer Join
Inner Join
• The INNER JOIN keyword selects all rows from both tables as
long as there is a match between the columns in both tables.
• Syntax
– SELECT column_name(s) FROM table1 INNER JOIN table2 ON
table1.column_name=table2.column_name;
Pk_int_id Vchr_place
1 Mumbai
2 Kolkata
3 Bangalore
4 Cochin
Emp_id Emp_name fk_int_place_id
1000 Deepak 1
1001 Aneesh 3
1002 Naveen 2
1003 Jacob 5
Emp_id Emp_name Vchr_place
1000 Deepak Mumbai
1001 Aneesh Banglore
1002 Naveen Kolkatta
InnerJoin
Inner Join
SELECT emp_id,emp_name, vchr_place from tbl_employee
Join tbl_place on fk_int_place_id=pk_int_id
Left Outer Join
• The LEFT JOIN keyword returns all rows from the left table
(table1), with the matching rows in the right table (table2).
The result is NULL in the right side when there is no match.
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON
table1.column_name=table2.column_name;
Left Outer Join
Pk_int_id Vchr_place
1 Mumbai
2 Kolkata
3 Bangalore
4 Cochin
Emp_id Emp_name fk_int_place_id
1000 Deepak 1
1001 Aneesh 4
1002 Naveen 2
1003 Jacob 5
1004 Alex 6
Emp_id Emp_name Vchr_place
1000 Deepak Mumbai
1001 Aneesh Cochin
1002 Naveen Kolkatta
1003 Jacob NULL
1004 Alex NULL
LeftJoin
SELECT emp_id,emp_name, vchr_place from tbl_employee
Left Join tbl_place on fk_int_place_id=pk_int_id
Right Outer Join
• The RIGHT JOIN keyword returns all rows from the right table
(table2), with the matching rows in the left table (table1). The
result is NULL in the left side when there is no match.
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON
table1.column_name=table2.column_name;
Right Outer Join
Pk_int_id Vchr_place
1 Mumbai
2 Kolkata
3 Bangalore
4 Cochin
5 Trivandrum
7 Delhi
Emp_id Emp_name fk_int_place_id
1000 Deepak 1
1001 Aneesh 4
1002 Naveen 2
1003 Jacob 5
1004 Alex 6
Emp_id Emp_name Vchr_place
1000 Deepak Mumbai
1001 Aneesh Cochin
1002 Naveen Kolkatta
1003 Jacob Trivandrum
NULL NULL Banglore
NULL NULL Delhi
RightJoin
SELECT emp_id,emp_name, vchr_place fromtbl_employee Right
Join tbl_place on fk_int_place_id=pk_int_id
Questions?
“A good question deserve a good
grade…”
Self Check !!
Self Check !!
• Scalar function will return 0 or more
values
–True
–False
Self Check !!
• Scalar function will return 0 or more
values
–True
–False
Self Check !!
• Round() is an aggregate function
–True
–False
Self Check !!
• Round() is an aggregate function
–True
–False
Self Check !!
• Spot out the errors
Select dept,sum(salary), place from tbl_employee group by dept
where sum(salary)>10000
Correct Answer:
Select dept,sum(salary) from tbl_employee group by
dept having sum(salary)>10000
Self Check !!
• Spot out the errors
Select dept,sum(salary), place from tbl_employee group by dept
where sum(salary)>10000
Correct Answer:
Result will be unexpected if display any columns
without aggregate functions other than that
given in group by
Where must be given before group by We cannot use aggregate functions with where
condition
Select dept,sum(salary) from tbl_employee group by dept having
sum(salary)>10000
Select id,student,dept from tbl_student join tbl_dept on
fk_dept_id =pk_int_id
Pk_int_id dept
1 Computer science
2 Electronics
3 Commerce
4 Art
id Student fk_dept_id
1000 Ram 1
1001 Raju 4
1002 Mary 2
1003 Dona 5
1004 Lal 6
id Student dept
1000 Ram Computer science
1001 Raju Art
1002 Mary Electronics
1003 Dona NULL
1004 Lal NULL
Query :
Select id,student,dept from tbl_student left join tbl_dept on
fk_dept_id =pk_int_id
Pk_int_id dept
1 Computer science
2 Electronics
3 Commerce
4 Art
id Student fk_dept_id
1000 Ram 1
1001 Raju 4
1002 Mary 2
1003 Dona 5
1004 Lal 6
id Student dept
1000 Ram Computer science
1001 Raju Art
1002 Mary Electronics
1003 Dona NULL
1004 Lal NULL
Query :
Self Check !!
• When to use inner join
– I want to display all the places and students from that
places
– I want to display all the students and their places
– I want to display only the students of given places
–I want to display the only students from places given in
another table
Self Check !!
• When to use inner join
– I want to display all the places and students from that
places
– I want to display all the students and their places
– I want to display only the students of given places
– I want to display the only students from places given in
another table
Self Check !!
• When to use Right join
A. Matching records from right table needs to be displayed
B. Mismatching records from right table needs to be
displayed
C. Matching records from left table needs to be displayed
D. Mismatching records from left table needs to be
displayed
E. Both A & B
F. Both B & C
Self Check !!
• When to use Right join
A. Matching records from right table needs to be displayed
B. Mismatching records from right table needs to be
displayed
C. Matching records from left table needs to be displayed
D. Mismatching records from left table needs to be
displayed
E. Both A & B
F. Both B & C
End of day 2
THANK YOU..

More Related Content

What's hot

Stored procedure in sql server
Stored procedure in sql serverStored procedure in sql server
Stored procedure in sql server
baabtra.com - No. 1 supplier of quality freshers
 
Sql views
Sql viewsSql views
Sql views
arshid045
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
Salman Memon
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functionsVikas Gupta
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
Ritwik Das
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
Ankit Dubey
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
Raveena Thakur
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
NITISH KUMAR
 
Oracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step GuideOracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step Guide
Srinimf-Slides
 
Triggers in plsql
Triggers in plsqlTriggers in plsql
Triggers in plsql
Arun Sial
 
SQL commands
SQL commandsSQL commands
SQL commands
GirdharRatne
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
Sharad Dubey
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
BG Java EE Course
 

What's hot (20)

Stored procedure in sql server
Stored procedure in sql serverStored procedure in sql server
Stored procedure in sql server
 
Sql views
Sql viewsSql views
Sql views
 
Joins and unions
Joins and unionsJoins and unions
Joins and unions
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
Joins And Its Types
Joins And Its TypesJoins And Its Types
Joins And Its Types
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
sql function(ppt)
sql function(ppt)sql function(ppt)
sql function(ppt)
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Oracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step GuideOracle PLSQL Step By Step Guide
Oracle PLSQL Step By Step Guide
 
Triggers in plsql
Triggers in plsqlTriggers in plsql
Triggers in plsql
 
SQL commands
SQL commandsSQL commands
SQL commands
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Mysql
MysqlMysql
Mysql
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 

Similar to Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join

Integrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdfIntegrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdf
Saikrishna492522
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
Vidyasagar Mundroy
 
Interacting with Oracle Database
Interacting with Oracle DatabaseInteracting with Oracle Database
Interacting with Oracle Database
Chhom Karath
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
bixxman
 
sql language
sql languagesql language
sql language
moman abde
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
Brian Foote
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
Madhusha15
 
Les06
Les06Les06
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
Geetha Kannan
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
EllenGracePorras
 
Statements,joins and operators in sql by thanveer danish melayi(1)
Statements,joins and operators in sql by thanveer danish melayi(1)Statements,joins and operators in sql by thanveer danish melayi(1)
Statements,joins and operators in sql by thanveer danish melayi(1)
Muhammed Thanveer M
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
TechandMate
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
Achmad Solichin
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
Amrit Kaur
 

Similar to Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join (20)

Introduction to mysql part 2
Introduction to mysql part 2Introduction to mysql part 2
Introduction to mysql part 2
 
Integrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdfIntegrity constraint fundamentals of dbms.pdf
Integrity constraint fundamentals of dbms.pdf
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
Interacting with Oracle Database
Interacting with Oracle DatabaseInteracting with Oracle Database
Interacting with Oracle Database
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
sql language
sql languagesql language
sql language
 
Sql
SqlSql
Sql
 
SQL Fundamentals
SQL FundamentalsSQL Fundamentals
SQL Fundamentals
 
Sql statements function join
Sql statements function joinSql statements function join
Sql statements function join
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
Les06
Les06Les06
Les06
 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Statements,joins and operators in sql by thanveer danish melayi(1)
Statements,joins and operators in sql by thanveer danish melayi(1)Statements,joins and operators in sql by thanveer danish melayi(1)
Statements,joins and operators in sql by thanveer danish melayi(1)
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 
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
 

More from baabtra.com - No. 1 supplier of quality freshers

Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
baabtra.com - No. 1 supplier of quality freshers
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
baabtra.com - No. 1 supplier of quality freshers
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
baabtra.com - No. 1 supplier of quality freshers
 
Php database connectivity
Php database connectivityPhp database connectivity
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
baabtra.com - No. 1 supplier of quality freshers
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Blue brain
Blue brainBlue brain
5g
5g5g
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills
Baabtra soft skills
 

Recently uploaded

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 

Recently uploaded (20)

AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 

Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join

  • 1. Introduction to SQL server Grouping, Scalar and aggregate functions, Joins
  • 2. SQL Functions • SQL has many built-in functions for performing calculations on data. It can be broadly classified in to two –Aggregate Functions – Scalar functions
  • 3. SQL Functions • Aggregate Functions – SQL aggregate functions return a single value, calculated from all the values in a column. • Select AVG(int_price) from tbl_stock; // returns 8 • Select SUM(int_price) from tbl_stock; // returns 33 • Select MIN(int_price) from tbl_stock; // returns 2 • Select COUNT(vchr_product) from tbl_stock; // returns 4 Pk_int_id Vchr_product Int_price 1 Pen 10 2 Book 15 3 Eraser 2 4 Pencil 6 Tbl_stock Select SUM(int_price) from tbl_stock; Returns a single value
  • 4. SQL Functions • Scalar Functions – SQL scalar functions return a single value for each values of a particular column given as input. ➢ Select UPPER(vchr_product) from tbl_stock; ➢ Select LOWER(vchr_product) from tbl_stock; ➢ Select ROUND(int_price) from tbl_stock; Pk_int_id Vchr_product Int_price 1 Pen 10 2 Book 15 3 Eraser 2 4 Pencil 6 Tbl_product Returns a value Returns a value Returns a value Returns a value
  • 5. Grouping Data • GROUP BY allows you to take your result set, group it into logical groups and then run aggregate queries on the groups. • You could for instance select all employees, group them by their workplace location and calculate the average salary. This would give you the average salary of an employee at a given location in your database. Avg = 12500 Avg = 11500 Tbl_employee Emp_id Emp_name Emp_age Emp_email int_salary vchr_place 1000 Deepak 24 dk@gmail.com 10000 Cochin 1001 Aneesh 23 an@gmail.com 20000 Calicut 1002 Naveen 25 nn@gmail.com 15000 Cochin 1003 Jacob 25 jb@gmail.com 13000 Calicut
  • 6. Select vchr_place, avg(int_salary) from tbl_employee group by vchr_place; Example Result : Emp_id Emp_name Emp_age Emp_email int_salary vchr_place 1000 Deepak 24 dk@gmail.com 10000 Cochin 1001 Aneesh 23 an@gmail.com 20000 Calicut 1002 Naveen 25 nn@gmail.com 15000 Cochin 1003 Jacob 25 jb@gmail.com 13000 Calicut Tbl_employee Vchr_place Avg(int_alary) Cochin 12500 Calicut 11500
  • 7. The HAVING Clause • The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. • An Sql statement can have both ‘where’ clause and ‘having’ clause. Where filters data before grouping.Having filters data after grouping • Syntax: • SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value;
  • 8. Example Select vchr_place, avg(int_salary) from tbl_employee group by vchr_place having avg(int_salary)>12000; Vchr_place Avg(int_alary) Cochin 12500 Calicut 11500 Vchr_place Avg(int_alary) Cochin 12500
  • 10. Where do we use joins? • An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them. • Join helps to write single query to fetch data from multiple tables so as to meet the business requirement/generate reports
  • 11. Types Of joins • Inner join • Outer Join – Left Outer Join – Right Outer Join
  • 12. Inner Join • The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables. • Syntax – SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name;
  • 13. Pk_int_id Vchr_place 1 Mumbai 2 Kolkata 3 Bangalore 4 Cochin Emp_id Emp_name fk_int_place_id 1000 Deepak 1 1001 Aneesh 3 1002 Naveen 2 1003 Jacob 5 Emp_id Emp_name Vchr_place 1000 Deepak Mumbai 1001 Aneesh Banglore 1002 Naveen Kolkatta InnerJoin Inner Join SELECT emp_id,emp_name, vchr_place from tbl_employee Join tbl_place on fk_int_place_id=pk_int_id
  • 14. Left Outer Join • The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match. SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;
  • 15. Left Outer Join Pk_int_id Vchr_place 1 Mumbai 2 Kolkata 3 Bangalore 4 Cochin Emp_id Emp_name fk_int_place_id 1000 Deepak 1 1001 Aneesh 4 1002 Naveen 2 1003 Jacob 5 1004 Alex 6 Emp_id Emp_name Vchr_place 1000 Deepak Mumbai 1001 Aneesh Cochin 1002 Naveen Kolkatta 1003 Jacob NULL 1004 Alex NULL LeftJoin SELECT emp_id,emp_name, vchr_place from tbl_employee Left Join tbl_place on fk_int_place_id=pk_int_id
  • 16. Right Outer Join • The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match. SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;
  • 17. Right Outer Join Pk_int_id Vchr_place 1 Mumbai 2 Kolkata 3 Bangalore 4 Cochin 5 Trivandrum 7 Delhi Emp_id Emp_name fk_int_place_id 1000 Deepak 1 1001 Aneesh 4 1002 Naveen 2 1003 Jacob 5 1004 Alex 6 Emp_id Emp_name Vchr_place 1000 Deepak Mumbai 1001 Aneesh Cochin 1002 Naveen Kolkatta 1003 Jacob Trivandrum NULL NULL Banglore NULL NULL Delhi RightJoin SELECT emp_id,emp_name, vchr_place fromtbl_employee Right Join tbl_place on fk_int_place_id=pk_int_id
  • 18. Questions? “A good question deserve a good grade…”
  • 20. Self Check !! • Scalar function will return 0 or more values –True –False
  • 21. Self Check !! • Scalar function will return 0 or more values –True –False
  • 22. Self Check !! • Round() is an aggregate function –True –False
  • 23. Self Check !! • Round() is an aggregate function –True –False
  • 24. Self Check !! • Spot out the errors Select dept,sum(salary), place from tbl_employee group by dept where sum(salary)>10000 Correct Answer: Select dept,sum(salary) from tbl_employee group by dept having sum(salary)>10000
  • 25. Self Check !! • Spot out the errors Select dept,sum(salary), place from tbl_employee group by dept where sum(salary)>10000 Correct Answer: Result will be unexpected if display any columns without aggregate functions other than that given in group by Where must be given before group by We cannot use aggregate functions with where condition Select dept,sum(salary) from tbl_employee group by dept having sum(salary)>10000
  • 26. Select id,student,dept from tbl_student join tbl_dept on fk_dept_id =pk_int_id Pk_int_id dept 1 Computer science 2 Electronics 3 Commerce 4 Art id Student fk_dept_id 1000 Ram 1 1001 Raju 4 1002 Mary 2 1003 Dona 5 1004 Lal 6 id Student dept 1000 Ram Computer science 1001 Raju Art 1002 Mary Electronics 1003 Dona NULL 1004 Lal NULL Query :
  • 27. Select id,student,dept from tbl_student left join tbl_dept on fk_dept_id =pk_int_id Pk_int_id dept 1 Computer science 2 Electronics 3 Commerce 4 Art id Student fk_dept_id 1000 Ram 1 1001 Raju 4 1002 Mary 2 1003 Dona 5 1004 Lal 6 id Student dept 1000 Ram Computer science 1001 Raju Art 1002 Mary Electronics 1003 Dona NULL 1004 Lal NULL Query :
  • 28. Self Check !! • When to use inner join – I want to display all the places and students from that places – I want to display all the students and their places – I want to display only the students of given places –I want to display the only students from places given in another table
  • 29. Self Check !! • When to use inner join – I want to display all the places and students from that places – I want to display all the students and their places – I want to display only the students of given places – I want to display the only students from places given in another table
  • 30. Self Check !! • When to use Right join A. Matching records from right table needs to be displayed B. Mismatching records from right table needs to be displayed C. Matching records from left table needs to be displayed D. Mismatching records from left table needs to be displayed E. Both A & B F. Both B & C
  • 31. Self Check !! • When to use Right join A. Matching records from right table needs to be displayed B. Mismatching records from right table needs to be displayed C. Matching records from left table needs to be displayed D. Mismatching records from left table needs to be displayed E. Both A & B F. Both B & C