SlideShare a Scribd company logo
11 SQL SERVER: ADVANCEDQUERY CONCEPTS
Advanced QueriesHaving seen the basic concepts of SQL, let us divulge into the advanced query concepts.The Presentation consists of the following portions:  Nested Queries
  Any
  In
  HavingNested QueriesNested Queries literally mean ‘a query within another query’. This is exactly what we are about to see. In some cases, the data derived using a select statement becomes inadequate. Here, we pipe the output of this statement into another select statement, till we get the results that we needed. Illustration:Select x1Select x2Select x3t2t3t1Select x4 from t1,t2,t3
Nested Select QueriesConsider the following tables of the Fisherman database:FishermanFishCatch Table:Now, I wish to find the names of the fishermen who caught a bass (any type). The SQL query would be:select name from fisherman where fishermanid = any(selectfishermanID from FishCatch where fish like ‘%Bass%’)As you can see, any is used to compare a particular record’s field to that of multiple records.
Nested Select QueriesThe previous query can be rewritten using in as follows:Using ANY:select name from fisherman where fishermanid= any(selectfishermanID from FishCatch where fish like ‘%Bass%’)Using IN:select name from fisherman where fishermanidin(selectfishermanID from FishCatch where fish like ‘%Bass%’)NOTE: When using in remember NOT TO PUT THE ‘=‘ (Equal to ) operator
Usefulness of having ‘Having’We have seen that there exists a restriction that the aggregate functions cannot be used in the right side of where clause. This syntactical restriction may lead to several inconveniences while programming.To understand this, see the following problem:Find the Fisherman who has caught 2 fishes:We might think of this solution:Select FishermanID from fishTable where count(fish) =2But this doesn’t work as aggregate functions are not allowed on the right-side of where clause. So, how do we solve the problem? ‘Having’ is our answer.
HavingFind the Fisherman who has caught 2 fishes:Solving with having:Select fishermanID from fishtable group by fishermanidhaving count(fish)=2
Ad

Recommended

Php verses my sql
Php verses my sql
SEO Training in Chandigarh
 
Php verses MySQL
Php verses MySQL
CBitss Technologies
 
Lecture6 display data by okello erick
Lecture6 display data by okello erick
okelloerick
 
MySql:Basics
MySql:Basics
DataminingTools Inc
 
MySql:Introduction
MySql:Introduction
DataminingTools Inc
 
Php database connectivity
Php database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
Connectivity coding for java and mysql
Connectivity coding for java and mysql
Fahad Ali Khan
 
Learn PHP Lacture2
Learn PHP Lacture2
ADARSH BHATT
 
Web app development_crud_13
Web app development_crud_13
Hassen Poreya
 
MySQL for beginners
MySQL for beginners
Saeid Zebardast
 
Ruby on Rails Developer - Allerin
Ruby on Rails Developer - Allerin
Lauree R
 
Backups with Exported Resources - Zach Leslie, Puppet Labs
Backups with Exported Resources - Zach Leslie, Puppet Labs
Puppet
 
Cara membuka workbook yang terproteksi
Cara membuka workbook yang terproteksi
Irmaoly
 
Bootstrap
Bootstrap
NexThoughts Technologies
 
Mysql DBI
Mysql DBI
Joe Christensen
 
Introduction to php database connectivity
Introduction to php database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
第三回月次セミナー(公開版)
第三回月次セミナー(公開版)
moai kids
 
Conexion php
Conexion php
Luis Reategui Vargas
 
Database Connectivity in PHP
Database Connectivity in PHP
Taha Malampatti
 
Config BuildConfig
Config BuildConfig
NexThoughts Technologies
 
Beginner guide to mysql command line
Beginner guide to mysql command line
Priti Solanki
 
Multiple instance on windows
Multiple instance on windows
Vasudeva Rao
 
Dynamic websites lec2
Dynamic websites lec2
Belal Arfa
 
Sql ch 5
Sql ch 5
Mukesh Tekwani
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Assignment#05
Assignment#05
Sunita Milind Dol
 
Introduction to SQL
Introduction to SQL
Mahir Haque
 
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
ssuser9dddf7
 
Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
auzee32
 
SQL querys in detail || Sql query slides
SQL querys in detail || Sql query slides
gourav kottawar
 

More Related Content

What's hot (14)

Web app development_crud_13
Web app development_crud_13
Hassen Poreya
 
MySQL for beginners
MySQL for beginners
Saeid Zebardast
 
Ruby on Rails Developer - Allerin
Ruby on Rails Developer - Allerin
Lauree R
 
Backups with Exported Resources - Zach Leslie, Puppet Labs
Backups with Exported Resources - Zach Leslie, Puppet Labs
Puppet
 
Cara membuka workbook yang terproteksi
Cara membuka workbook yang terproteksi
Irmaoly
 
Bootstrap
Bootstrap
NexThoughts Technologies
 
Mysql DBI
Mysql DBI
Joe Christensen
 
Introduction to php database connectivity
Introduction to php database connectivity
baabtra.com - No. 1 supplier of quality freshers
 
第三回月次セミナー(公開版)
第三回月次セミナー(公開版)
moai kids
 
Conexion php
Conexion php
Luis Reategui Vargas
 
Database Connectivity in PHP
Database Connectivity in PHP
Taha Malampatti
 
Config BuildConfig
Config BuildConfig
NexThoughts Technologies
 
Beginner guide to mysql command line
Beginner guide to mysql command line
Priti Solanki
 
Multiple instance on windows
Multiple instance on windows
Vasudeva Rao
 
Web app development_crud_13
Web app development_crud_13
Hassen Poreya
 
Ruby on Rails Developer - Allerin
Ruby on Rails Developer - Allerin
Lauree R
 
Backups with Exported Resources - Zach Leslie, Puppet Labs
Backups with Exported Resources - Zach Leslie, Puppet Labs
Puppet
 
Cara membuka workbook yang terproteksi
Cara membuka workbook yang terproteksi
Irmaoly
 
第三回月次セミナー(公開版)
第三回月次セミナー(公開版)
moai kids
 
Database Connectivity in PHP
Database Connectivity in PHP
Taha Malampatti
 
Beginner guide to mysql command line
Beginner guide to mysql command line
Priti Solanki
 
Multiple instance on windows
Multiple instance on windows
Vasudeva Rao
 

Similar to MS SQLSERVER:Advanced Query Concepts Copy (20)

Dynamic websites lec2
Dynamic websites lec2
Belal Arfa
 
Sql ch 5
Sql ch 5
Mukesh Tekwani
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Assignment#05
Assignment#05
Sunita Milind Dol
 
Introduction to SQL
Introduction to SQL
Mahir Haque
 
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
ssuser9dddf7
 
Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
auzee32
 
SQL querys in detail || Sql query slides
SQL querys in detail || Sql query slides
gourav kottawar
 
SQL Queries related to DDL and DML and DCL
SQL Queries related to DDL and DML and DCL
Rajendra Prasad
 
Module 3 Part I - Bk1 Chapter 07.ppt
Module 3 Part I - Bk1 Chapter 07.ppt
KusumaS36
 
CS121Lec05.pdf
CS121Lec05.pdf
georgejustymirobi1
 
SQL Queries .pdf
SQL Queries .pdf
srinathpurushotham
 
Intro to tsql unit 2
Intro to tsql unit 2
Syed Asrarali
 
Intro To TSQL - Unit 2
Intro To TSQL - Unit 2
iccma
 
SQL
SQL
Shunya Ram
 
Transact SQL (T-SQL) for Beginners (A New Hope)
Transact SQL (T-SQL) for Beginners (A New Hope)
Andrea Allred
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
HITIKAJAIN4
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database Querying
Rodger Oates
 
dbmshkxjgzigyih you ucifu Yoyxoych5.pdf
dbmshkxjgzigyih you ucifu Yoyxoych5.pdf
sahilurrahemankhan
 
dbmoyyohhhhhhhuhcyo Yxi hi uYes usch5.pdf
dbmoyyohhhhhhhuhcyo Yxi hi uYes usch5.pdf
sahilurrahemankhan
 
Dynamic websites lec2
Dynamic websites lec2
Belal Arfa
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 
Introduction to SQL
Introduction to SQL
Mahir Haque
 
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
ssuser9dddf7
 
Server Query Language – Getting Started.pptx
Server Query Language – Getting Started.pptx
auzee32
 
SQL querys in detail || Sql query slides
SQL querys in detail || Sql query slides
gourav kottawar
 
SQL Queries related to DDL and DML and DCL
SQL Queries related to DDL and DML and DCL
Rajendra Prasad
 
Module 3 Part I - Bk1 Chapter 07.ppt
Module 3 Part I - Bk1 Chapter 07.ppt
KusumaS36
 
Intro to tsql unit 2
Intro to tsql unit 2
Syed Asrarali
 
Intro To TSQL - Unit 2
Intro To TSQL - Unit 2
iccma
 
Transact SQL (T-SQL) for Beginners (A New Hope)
Transact SQL (T-SQL) for Beginners (A New Hope)
Andrea Allred
 
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
HITIKAJAIN4
 
Tech Jam 01 - Database Querying
Tech Jam 01 - Database Querying
Rodger Oates
 
dbmshkxjgzigyih you ucifu Yoyxoych5.pdf
dbmshkxjgzigyih you ucifu Yoyxoych5.pdf
sahilurrahemankhan
 
dbmoyyohhhhhhhuhcyo Yxi hi uYes usch5.pdf
dbmoyyohhhhhhhuhcyo Yxi hi uYes usch5.pdf
sahilurrahemankhan
 
Ad

More from sqlserver content (20)

MS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining tools
sqlserver content
 
MS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data mining
sqlserver content
 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
sqlserver content
 
MS SQL SERVER: Olap cubes and data mining
MS SQL SERVER: Olap cubes and data mining
sqlserver content
 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithm
sqlserver content
 
MS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rules
sqlserver content
 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regression
sqlserver content
 
MS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithm
sqlserver content
 
MS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithm
sqlserver content
 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmx
sqlserver content
 
MS Sql Server: Reporting models
MS Sql Server: Reporting models
sqlserver content
 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating data
sqlserver content
 
MS Sql Server: Reporting introduction
MS Sql Server: Reporting introduction
sqlserver content
 
MS Sql Server: Reporting basics
MS Sql Server: Reporting basics
sqlserver content
 
MS Sql Server: Datamining Introduction
MS Sql Server: Datamining Introduction
sqlserver content
 
MS Sql Server: Business Intelligence
MS Sql Server: Business Intelligence
sqlserver content
 
MS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into Database
sqlserver content
 
MS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With Functions
sqlserver content
 
MS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A Database
sqlserver content
 
MS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base Design
sqlserver content
 
MS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining tools
sqlserver content
 
MS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data mining
sqlserver content
 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
sqlserver content
 
MS SQL SERVER: Olap cubes and data mining
MS SQL SERVER: Olap cubes and data mining
sqlserver content
 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithm
sqlserver content
 
MS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rules
sqlserver content
 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regression
sqlserver content
 
MS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithm
sqlserver content
 
MS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithm
sqlserver content
 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmx
sqlserver content
 
MS Sql Server: Reporting models
MS Sql Server: Reporting models
sqlserver content
 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating data
sqlserver content
 
MS Sql Server: Reporting introduction
MS Sql Server: Reporting introduction
sqlserver content
 
MS Sql Server: Reporting basics
MS Sql Server: Reporting basics
sqlserver content
 
MS Sql Server: Datamining Introduction
MS Sql Server: Datamining Introduction
sqlserver content
 
MS Sql Server: Business Intelligence
MS Sql Server: Business Intelligence
sqlserver content
 
MS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into Database
sqlserver content
 
MS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With Functions
sqlserver content
 
MS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A Database
sqlserver content
 
MS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base Design
sqlserver content
 
Ad

Recently uploaded (20)

Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
Daily Lesson Log MATATAG ICT TEchnology 8
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 
Lessons Learned from Developing Secure AI Workflows.pdf
Lessons Learned from Developing Secure AI Workflows.pdf
Priyanka Aash
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape schützt, was zählt! Und besonders mit dem neust...
Josef Weingand
 
UserCon Belgium: Honey, VMware increased my bill
UserCon Belgium: Honey, VMware increased my bill
stijn40
 
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
GenAI Opportunities and Challenges - Where 370 Enterprises Are Focusing Now.pdf
Priyanka Aash
 
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
EIS-Webinar-Engineering-Retail-Infrastructure-06-16-2025.pdf
Earley Information Science
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Using the SQLExecutor for Data Quality Management: aka One man's love for the...
Safe Software
 
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
" How to survive with 1 billion vectors and not sell a kidney: our low-cost c...
Fwdays
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
 
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
 
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
 
Daily Lesson Log MATATAG ICT TEchnology 8
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
 

MS SQLSERVER:Advanced Query Concepts Copy

  • 1. 11 SQL SERVER: ADVANCEDQUERY CONCEPTS
  • 2. Advanced QueriesHaving seen the basic concepts of SQL, let us divulge into the advanced query concepts.The Presentation consists of the following portions: Nested Queries
  • 5. HavingNested QueriesNested Queries literally mean ‘a query within another query’. This is exactly what we are about to see. In some cases, the data derived using a select statement becomes inadequate. Here, we pipe the output of this statement into another select statement, till we get the results that we needed. Illustration:Select x1Select x2Select x3t2t3t1Select x4 from t1,t2,t3
  • 6. Nested Select QueriesConsider the following tables of the Fisherman database:FishermanFishCatch Table:Now, I wish to find the names of the fishermen who caught a bass (any type). The SQL query would be:select name from fisherman where fishermanid = any(selectfishermanID from FishCatch where fish like ‘%Bass%’)As you can see, any is used to compare a particular record’s field to that of multiple records.
  • 7. Nested Select QueriesThe previous query can be rewritten using in as follows:Using ANY:select name from fisherman where fishermanid= any(selectfishermanID from FishCatch where fish like ‘%Bass%’)Using IN:select name from fisherman where fishermanidin(selectfishermanID from FishCatch where fish like ‘%Bass%’)NOTE: When using in remember NOT TO PUT THE ‘=‘ (Equal to ) operator
  • 8. Usefulness of having ‘Having’We have seen that there exists a restriction that the aggregate functions cannot be used in the right side of where clause. This syntactical restriction may lead to several inconveniences while programming.To understand this, see the following problem:Find the Fisherman who has caught 2 fishes:We might think of this solution:Select FishermanID from fishTable where count(fish) =2But this doesn’t work as aggregate functions are not allowed on the right-side of where clause. So, how do we solve the problem? ‘Having’ is our answer.
  • 9. HavingFind the Fisherman who has caught 2 fishes:Solving with having:Select fishermanID from fishtable group by fishermanidhaving count(fish)=2
  • 10. Summary11. Advanced Query Concepts: Nested Select SQL
  • 11. In
  • 13. HavingVisit more self help tutorialsPick a tutorial of your choice and browse through it at your own pace.The tutorials section is free, self-guiding and will not involve any additional support.Visit us at www.dataminingtools.net