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

More Related Content

PPTX
Php verses my sql
PDF
Php verses MySQL
PPT
Lecture6 display data by okello erick
PPTX
MySql:Basics
PPTX
MySql:Introduction
DOCX
Connectivity coding for java and mysql
PPTX
Learn PHP Lacture2
Php verses my sql
Php verses MySQL
Lecture6 display data by okello erick
MySql:Basics
MySql:Introduction
Connectivity coding for java and mysql
Learn PHP Lacture2

What's hot (14)

PDF
Web app development_crud_13
PDF
MySQL for beginners
DOCX
Ruby on Rails Developer - Allerin
PDF
Backups with Exported Resources - Zach Leslie, Puppet Labs
DOCX
Cara membuka workbook yang terproteksi
PPT
Mysql DBI
PDF
Introduction to php database connectivity
KEY
第三回月次セミナー(公開版)
DOCX
Conexion php
PPTX
Database Connectivity in PHP
PPTX
Config BuildConfig
PPTX
Beginner guide to mysql command line
DOCX
Multiple instance on windows
Web app development_crud_13
MySQL for beginners
Ruby on Rails Developer - Allerin
Backups with Exported Resources - Zach Leslie, Puppet Labs
Cara membuka workbook yang terproteksi
Mysql DBI
Introduction to php database connectivity
第三回月次セミナー(公開版)
Conexion php
Database Connectivity in PHP
Config BuildConfig
Beginner guide to mysql command line
Multiple instance on windows
Ad

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

PDF
Dynamic websites lec2
PDF
Sql ch 5
PPTX
Subqueries, Backups, Users and Privileges
PDF
Assignment#05
PPTX
Introduction to SQL
PPTX
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
PPTX
Server Query Language – Getting Started.pptx
PPT
SQL querys in detail || Sql query slides
PDF
SQL Queries related to DDL and DML and DCL
PPT
Module 3 Part I - Bk1 Chapter 07.ppt
PDF
CS121Lec05.pdf
PDF
SQL Queries .pdf
PPT
Intro to tsql unit 2
PPT
Intro To TSQL - Unit 2
DOC
PPT
Introduction to SQL brief notes for academic.ppt
PPT
Transact SQL (T-SQL) for Beginners (A New Hope)
PDF
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
PDF
Tech Jam 01 - Database Querying
PDF
dbmshkxjgzigyih you ucifu Yoyxoych5.pdf
Dynamic websites lec2
Sql ch 5
Subqueries, Backups, Users and Privileges
Assignment#05
Introduction to SQL
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Server Query Language – Getting Started.pptx
SQL querys in detail || Sql query slides
SQL Queries related to DDL and DML and DCL
Module 3 Part I - Bk1 Chapter 07.ppt
CS121Lec05.pdf
SQL Queries .pdf
Intro to tsql unit 2
Intro To TSQL - Unit 2
Introduction to SQL brief notes for academic.ppt
Transact SQL (T-SQL) for Beginners (A New Hope)
FOUNDATION OF DATA SCIENCE SQL QUESTIONS
Tech Jam 01 - Database Querying
dbmshkxjgzigyih you ucifu Yoyxoych5.pdf
Ad

More from sqlserver content (20)

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

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
cuic standard and advanced reporting.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
sap open course for s4hana steps from ECC to s4
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
cuic standard and advanced reporting.pdf
Spectral efficient network and resource selection model in 5G networks
Encapsulation_ Review paper, used for researhc scholars
NewMind AI Weekly Chronicles - August'25-Week II
A comparative analysis of optical character recognition models for extracting...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
sap open course for s4hana steps from ECC to s4

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