WISE OWL EXERCISES
- Simple Queries (4)
- Setting criteria using WHERE (5)
- Calculations (7)
- Calculations using dates (4)
- Basic joins (8)
- More exotic joins (2)
- Aggregation and grouping (8)
- Views (5)
- Subqueries (5)
- Stored procedures (5)
- Variables (8)
- Parameters and return values (11)
- Testing conditions (1)
- Looping (3)
- Scalar functions (6)
- Transactions (5)
- Creating tables (5)
- Temporary tables and table variables (9)
- Table-valued functions (6)
- Derived tables and CTEs (13)
- Dynamic SQL (4)
- Pivots (2)
- Triggers (2)
- Archived (70)
SQL | Basic joins exercise | Link 3 tables together using inner joins and table aliases
This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.
You can learn how to do this exercise if you attend one of more of the courses listed below!
You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
If you haven't already done so, run the script in the above folder to generate the Doctor Who database.
Write a query using inner joins to show all of the authors who have written episodes featuring the Daleks. You may find the following relationship diagram useful to refer to:
How to get from enemies to authors in 3 easy jumps.
You should find that there are 16 such episodes!
When you've finished, save this as Enough joins for now, then close it down.
select [EpisodeEnemyId], e.[EnemyName],[EpisodeNumber],[AuthorName]
from [dbo].[tblEpisodeEnemy] as epe
inner join [dbo].[tblEnemy] as e
on epe.EnemyId= e.EnemyId
inner join [dbo].[tblEpisode] as e1
on epe.EpisodeId = e1.EpisodeId
inner join [dbo].[tblAuthor] as a
on e1.AuthorId= a.AuthorId
where EnemyName like '%Daleks%'
Use inner joins to link four tables to show Dr Who enemies by author.
from tblAuthor as a
inner join tblEpisode as b on
a.AuthorId = b.AuthorId
inner join tblEpisodeEnemy as c on
c.EpisodeId = b.EpisodeId
inner join tblEnemy as d on
d.EnemyId = c.EnemyId
where d.EnemyName = 'Daleks'
So is this the code?
from tblauthor as a
inner join tblepisode as epi
on a.authorid = epi.authorid
inner join tblepisodeenemy as ene
on epi.episodeid = ene.episodeenemyid
inner join tblenemy as enemy
on enemy.enemyid = ene.episodeenemyid
where title like '%Daleks%'
I just get only four rows ,
From Author As x
INNER JOIN Episode As y
ON x.AuthorId = y.AuthorId
Where Title Like '%Daleks%';
Is this query correct?