So, let’s see what happens if some of the records are not matched. In our example, all books have a corresponding author and all authors have at least one corresponding book. INNER JOIN only displays records that are available in both tables. The result set would be exactly the same if we put the authors table in the FROM clause and the books table in the INNER JOIN clause. Note that the order of the tables doesn’t matter with INNER JOIN, or simple JOIN. It then pulls the corresponding first name and last name. The table aliases reduce typing and make the query more readable.įor each record in the left table (i.e., books), the query checks the author_id, then looks for the same id in the first column of the authors table. We assign the aliases in the FROM and INNER JOIN clauses and use them throughout the query. Note also that we are using aliases for table names (i.e., b for books and a for authors). Then, we use the ON keyword to tell the database which columns should be used for matching the records (i.e., the author_id column from the books table and the id column from the authors table). In the INNER JOIN clause, we specify the second table to join (also referred to as the right table). In the FROM clause, we specify the first table to join (also referred to as the left table). In the SELECT statement, we list the columns to be displayed: book id, book title, author’s first name, and author’s last name. SELECT b.id, b.title, a.first_name, a.last_name In our SQL query, we’ll join these two tables by matching the author_id column from the books table and the id column from the authors table: The book titles are stored in the books table, and the author names are stored in the authors table. Let’s say we want to show book titles along with their authors (i.e., the author’s first name and last name). This join type is used when we want to display matching records from two tables. We’ll start with a basic INNER JOIN, or simply, JOIN. It includes 93 coding challenges! INNER JOIN If you want to practice joining tables in SQL with many examples, I recommend taking the SQL JOINs course. Our database contains four tables: books, authors, editors, and translators. For the examples, we will use information about a publishing house that publishes original and translated books.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |