Your SlideShare is downloading. ×
0
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
MS Sql Server: Advanced Query Concepts
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

MS Sql Server: Advanced Query Concepts

4,010

Published on

Advanced Query Concepts

Advanced Query Concepts

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,010
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 11<br /> SQL SERVER: ADVANCEDQUERY CONCEPTS<br />
  • 2. Advanced Queries<br />Having seen the basic concepts of SQL, let us divulge into the advanced query concepts.<br />The Presentation consists of the following portions:<br /><ul><li> Nested Queries
  • 3. Any
  • 4. In
  • 5. Having</li></li></ul><li>Nested Queries<br />Nested Queries literally mean ‘a query within another query’. This is exactly what we are about to see. <br />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. <br />Illustration:<br />Select x1<br />Select x2<br />Select x3<br />t2<br />t3<br />t1<br />Select x4 from t1,t2,t3<br />
  • 6. Nested Select Queries<br />Consider the following tables of the Fisherman database:<br />Fisherman<br />FishCatch Table:<br />Now, I wish to find the names of the fishermen who caught a bass (any type). The SQL query would be:<br />select name from fisherman where fishermanid = any(selectfishermanID from FishCatch where fish like ‘%Bass%’)<br />As you can see, any is used to compare a particular record’s field to that of multiple records.<br />
  • 7. Nested Select Queries<br />The previous query can be rewritten using in as follows:<br />Using ANY:<br />select name from fisherman where fishermanid= any(selectfishermanID from FishCatch where fish like ‘%Bass%’)<br />Using IN:<br />select name from fisherman where fishermanidin(selectfishermanID from FishCatch where fish like ‘%Bass%’)<br />NOTE: When using in remember NOT TO PUT THE ‘=‘ (Equal to ) operator<br />
  • 8. Usefulness of having ‘Having’<br />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.<br />To understand this, see the following problem:<br />Find the Fisherman who has caught 2 fishes:<br />We might think of this solution:<br />Select FishermanID from fishTable where count(fish) =2<br />But 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.<br />
  • 9. Having<br />Find the Fisherman who has caught 2 fishes:<br />Solving with having:<br />Select fishermanID from fishtable group by fishermanidhaving count(fish)=2<br />
  • 10. Summary<br />11. Advanced Query Concepts:<br /><ul><li> Nested Select SQL
  • 11. In
  • 12. Any
  • 13. Having</li></li></ul><li>Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

×