Published: Monday 4 September 2023
With a SQL statement, we can use the JOIN statement to join multiple tables to a database query.
Two commonly used JOIN types that are used in SQL Server are INNER JOIN and LEFT JOIN.
Take these users and roles data:
User
Id | Name | RoleId |
---|---|---|
1 | Dave | 1 |
2 | Roger | 2 |
3 | Pavan | 2 |
4 | Thierry | NULL |
Role
Id | Name |
---|---|
1 | Administrator |
2 | Editor |
Write a SQL statement that will return all users and their role. For table names, use User for the user table and Role for the role table. You'll need to use one of the JOIN methods to JOIN the User table with the Role table. Remember to only include the columns you need in the statement.