What is an Inner Join?
The world of database management is full of techniques and concepts that allow you to retrieve and manipulate data efficiently. One of the most known and used methods is the Inner Join. This type of join is the default in relational database management systems, and its main function is to cross or combine rows from two or more tables based on a condition that must be met in both.
When we say 'cross tables', we refer to the process by which data from different tables are linked thanks to a common key (such as an ID, for example). This most common join is an 'implicit join': it is possible to use it without explicitly mentioning it in the SQL query as shown here:
SELECT book.name, book.title, a.nameFROM authors AS a, books AS bWHERE a.authorId = b.authorId;
However, it is often preferred to use the explicit form of the Inner Join, which provides better readability and maintainability of the SQL code:
SELECT book.name, book.title, a.nameFROM books AS bINNER JOIN authors AS a ON a.authorId = b.authorId;
The difference between these two approaches is purely stylistic, since both queries are designed to return the same data set, i.e., the books along with their respective authors.
When to use a Left Join?
Where the Inner Join only returns rows where there are matches in both tables, the Left Join overcomes this limitation. When using a Left Join, the query will return all rows from the left table (left, from the SQL clause perspective), and rows from the right table only if there are matches. If there are no matches, the result will be NULL for the columns of the table on the right.
Let's see a practical example. Suppose we want to list all authors, including those who have no books:
SELECT a.authorId, a.name, b.titleFROM authors AS aLEFT JOIN books AS b ON a.authorId = b.authorId;
This query will fetch all authors, and if any have no books, the field corresponding to the book title will be NULL.
A Left Join is particularly useful when we are interested in finding all instances of a primary record, even when there is no associated record in the secondary table.
How to use grouping functions?
With a solid understanding of Inner and Left Joins, we can begin to explore more advanced functions such as COUNT. This function is crucial for counting records or groupings within a database. For example, if we want to know how many books each author has written, we could use the following:
SELECT a.name, COUNT(b.bookId) AS book_countFROM authors AS aLEFT JOIN books AS b ON a.authorId = b.authorIdGROUP BY a.authorId;
By using GROUP BY
, we are specifying the point on which we are going to perform the aggregation, ensuring that each author receives his or her correct book count.
Experimenting with different combinations of Inner and Left join, in addition to the grouping functions, will allow us to create more robust and functional queries. Understanding how data distributions impact the availability and visibility within our SQL queries gives us a strategic advantage when designing robust and well-founded applications.
Want to see more contributions, questions and answers from the community?