Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andy Brown
In this tutorial
If your head spins when you try to remember the syntax of joins in SQL, use this method instead! You first need to make sure you have a new query on screen in SQL Server Management Studio:
Create a new query ... | ... then choose the current database. |
Let's assume that you now want to create a query showing the author's name for each book in our database:
The query should show the 7 books with corresponding authors.
To start joining two tables together, right-click in a new query:
Right-click in a new query and choose to design your query.
Now choose the tables you want to include in your query:
Double-click on each table that you want to include in your query.
In our case, Management Studio will create a link between the tables:
SQL Server automatically creates a join between the two tables.
Management Studio puts this link in because each table has a column with the same name (AuthorId) and the same data type (bigint) with one of the two tables (tblAuthor) having this column as its primary key.
You can now choose which columns you want to include, and add any sorting, filtering (and much more besides):
Here we've chosen to include 3 columns, and we're about to sort the query by the LastName column.
When you've finished you can choose OK to insert the corresponding SQL into your query (although initially it won't be prettty):
SELECT tblAuthor.FirstName, tblAuthor.LastName, tblBook.BookName
FROM tblAuthor INNER JOIN
tblBook ON tblAuthor.AuthorId = tblBook.AuthorId
Once you've got the SQL (and join syntax) that you want, you can make it look neater. It won't affect the SQL, but it may make you feel better!
-- show authors and books
SELECT
tblAuthor.FirstName,
tblAuthor.LastName,
tblBook.BookName
FROM
tblAuthor
INNER JOIN tblBook
ON tblAuthor.AuthorId = tblBook.AuthorId
The above SQL is exactly the same as the code generated, give or take a bit of indentation and a comment - but it's MUCH easier to read.
This is all very well, but what happens if SSMS doesn't create a join for you?
Suppose that the tables weren't joined automatically?
In the above case, you could manually create a join by dragging one of the AuthorId columns onto the other:
Drag the AuthorId column (1) onto column (2) in the tblBook table to create a join.
You can also change the nature of joins by right-clicking on them:
Right-click on any join to see the options shown above (and explained below).
The option shown selected above (Select All Rows from tblAuthor) would create a left outer join:
What a left outer join looks like in query editor.
Here's the SQL generated for this diagram:
SELECT
tblAuthor.FirstName,
tblAuthor.LastName,
tblBook.BookName
FROM
tblAuthor
LEFT OUTER JOIN tblBook
ON tblAuthor.AuthorId = tblBook.AuthorId
ORDER BY
tblAuthor.LastName
However, we haven't yet seen what an inner join or outer join actually are, so it's time to step back a bit and have a look at how to create an inner join the hard way - by writing SQL.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.