11<br /> SQL SERVER: ADVANCEDQUERY CONCEPTS<br />
Advanced Queries<br />Having seen the basic concepts of SQL, let us divulge into the advanced query concepts.<br />The Pre...
  Any
  In
  Having</li></li></ul><li>Nested Queries<br />Nested Queries literally mean ‘a query within another query’. This is exact...
Nested Select Queries<br />Consider the following tables of the Fisherman database:<br />Fisherman<br />FishCatch Table:<b...
Nested Select Queries<br />The previous query can be rewritten using in as follows:<br />Using ANY:<br />select name from ...
Usefulness of having ‘Having’<br />We have seen that there exists a restriction that the aggregate functions cannot be use...
Having<br />Find the Fisherman who has caught 2 fishes:<br />Solving with having:<br />Select fishermanID from fishtable g...
Upcoming SlideShare
Loading in …5
×

MS SQLSERVER:Advanced Query Concepts Copy

448 views
416 views

Published on

MS SQLSERVER:Advanced Query Concepts Copy

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

  • Be the first to like this

No Downloads
Views
Total views
448
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MS SQLSERVER:Advanced Query Concepts Copy

  1. 1. 11<br /> SQL SERVER: ADVANCEDQUERY CONCEPTS<br />
  2. 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. 3. Any
  4. 4. In
  5. 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. 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. 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. 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. 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. 10. Summary<br />11. Advanced Query Concepts:<br /><ul><li> Nested Select SQL
  11. 11. In
  12. 12. Any
  13. 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 />

×