## SQL Server JOINS

 Author Message Eirikur Eiriksson SSC-Forever Group: General Forum Members Points: 41310 Visits: 19504 Quick analysisFirst a simplified ERD` +--------------+ +--------------+ +-----------------+ | Trial | | Culture | | CultureStep | +--------------+ +--------------+ +-----------------+ | TrialID | ,---|-| CultureID |-|--, | CultureStepID | | TrialCode | | | CultureName | '---|<| CultureID | | CultureID |>|--' | (Culture ) | | ( CultureStep ) | | (Trial ) | | (attributes) | | ( attributes ) | | (attributes) | +--------------+ +-----------------+ +--------------+ `Looking at the ERD, we can tell that:Each Trial has one and only one Culture.One or more Trials can share the same Culture.AndEach Culture has one or more CultureSteps.One or more Cultures can share one or more CultureSteps.Inspecting the Cardinality`Table | Row Count------------|-----------Trial | 6793Culture | 2327CultureStep | 13957`As the query "Select * from Culture inner join Trial on Culture.CultureID = Trial.CultureID" returns 6785 rows, we know that there are 8 (6793 - 6785) Trials sharing the same Culture. We can also tell that each Culture has the average of 6 CultureSteps (13957/2327 = 5.998). Since we do not know the distribution of CultureID in the CultureStep table, the exact numbers cannot be produced but here is an approxymation:Select * from (Culture inner join Trial on Culture.CultureID = Trial.CultureID) inner join CultureStep on Culture.CultureID=CultureStep.CultureIDGiven that each CultureID appears aprox. 6 times in the CultureStep and the set Culture-Trial has 6785 entries, the expected result should have close to 6 x 6785 entries. shaimaa.tarekelshoeiby SSC Rookie Group: General Forum Members Points: 36 Visits: 17 Thank you for the excellent post .Can you please help me become capable of doing such analysis?How were you able to make the simplified ERD and come up with the conclusions under it I want to learn this and be good at it ...Also , need to be good with joins and although I did read out several links with examples ...I still seem to find issues like when my brain cannot digest how an inner join would get more rows than the individual selects and so on. Eirikur Eiriksson SSC-Forever Group: General Forum Members Points: 41310 Visits: 19504 shaimaa.tarekelshoeiby (8/25/2014)Thank you for the excellent post .Can you please help me become capable of doing such analysis?How were you able to make the simplified ERD and come up with the conclusions under it I want to learn this and be good at it ...Also , need to be good with joins and although I did read out several links with examples ...I still seem to find issues like when my brain cannot digest how an inner join would get more rows than the individual selects and so on.You are very welcome. My suggestion is to grab a good book on the subject, i.e. "Database Modeling and Design" or "Data Modeling for the Business" and while reading through it, use only simple tools such as pen/paper or a text editor to work through exercises and examples. Also read up on the "Relational Database Theory", "Naive Set Theory" and "Relational Algebra". This should get you well on the way.Another thing I find helpful, especially when tackling very complex problems, is to practice solving puzzles like Soduku by memory (not using pen and paper), keeps the grey matter functional;-) shaimaa.tarekelshoeiby SSC Rookie Group: General Forum Members Points: 36 Visits: 17 Thank you ...But with so limited time , what can I do to help me go through until I can have more time to read the book(s) you suggested and use pen/paper?Any recommended set of articles or a small daily exercise ; something of the sort? Grant Fritchey SSC Guru Group: General Forum Members Points: 99657 Visits: 33014 There's no instant or magic way to build this kind of knowledge. It takes a long time. I'd add getting a copy of the book T-SQL Querying Fundamentals by Itzik Ben Gan. It's a great way to learn. ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server Query Performance Tuning and SQL Server Execution PlansProduct Evangelist for Red Gate Software shaimaa.tarekelshoeiby SSC Rookie Group: General Forum Members Points: 36 Visits: 17 Oh, an important question regarding the joinI learnt about using CTE instead of a view so that I can do it all as sql queriesHowever , adding the With part doesn't work with Access.Is there anything that can be similarly done in Access (or should I be moving to a new thread?)Thanks a lot in advance! shaimaa.tarekelshoeiby SSC Rookie Group: General Forum Members Points: 36 Visits: 17 Alright thank you both...I should seriously consider Eirikur Eiriksson SSC-Forever Group: General Forum Members Points: 41310 Visits: 19504 shaimaa.tarekelshoeiby (8/25/2014)Oh, an important question regarding the joinI learnt about using CTE instead of a view so that I can do it all as sql queriesHowever , adding the With part doesn't work with Access.Is there anything that can be similarly done in Access (or should I be moving to a new thread?)Thanks a lot in advance!I'm afraid that in Access you will have to stick with views. Grant Fritchey SSC Guru Group: General Forum Members Points: 99657 Visits: 33014 Nope. Access doesn't use Transact SQL and the full T-SQL stack supported by SQL Server. ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server Query Performance Tuning and SQL Server Execution PlansProduct Evangelist for Red Gate Software shaimaa.tarekelshoeiby SSC Rookie Group: General Forum Members Points: 36 Visits: 17 The main problem which made me think of detaching from views is that update cannot be done (getting database exception : modification affects multiple base tables)