GROUP BY With Joins

How to write an inner join query in SQL?

An inner join query uses INNER JOIN and an ON condition to return only rows that match in both tables, with the syntax, an example and common mistakes.

On this page
  1. The basic syntax
  2. An example with authors and books
  3. Aliases and qualified column names
  4. JOIN and INNER JOIN in MySQL
  5. Common mistakes

The basic syntax

An inner join query names the first table after FROM, names the second table after INNER JOIN, and sets the matching rule in an ON clause. Only the rows where the ON condition is true appear in the result.

SELECT a.column_name,
       b.column_name
FROM table_a AS a
INNER JOIN table_b AS b
        ON b.some_column = a.some_column;

Rows with no match in the other table are left out. That is the main difference from an outer join.

An example with authors and books

Two small tables show how this works. A book can have an author, and an author can have many books.

CREATE TABLE authors (
  author_id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

CREATE TABLE books (
  book_id INT PRIMARY KEY,
  title VARCHAR(200) NOT NULL,
  author_id INT NULL,
  FOREIGN KEY (author_id) REFERENCES authors (author_id)
);

INSERT INTO authors (author_id, name) VALUES
  (1, 'Rivera'),
  (2, 'Patel'),
  (3, 'Kim');

INSERT INTO books (book_id, title, author_id) VALUES
  (10, 'River Notes', 1),
  (11, 'Salt and Stone', 1),
  (12, 'Quiet Hours', 2),
  (13, 'Unsigned Draft', NULL);

The query pairs each book with its author:

SELECT a.name AS author,
       b.title
FROM authors AS a
INNER JOIN books AS b
        ON b.author_id = a.author_id
ORDER BY a.name, b.title;

The result has three rows:

author title
Patel Quiet Hours
Rivera River Notes
Rivera Salt and Stone

Kim has no books, so there is nothing to match. “Unsigned Draft” has a NULL author_id. NULL never equals another value, so that book has no match and drops out too.

Aliases and qualified column names

Short aliases such as a and b keep long queries readable. Put the alias in front of each column, as in a.author_id, so the database knows which table you mean. If both tables have a column with the same name, such as id, leaving off the alias causes an error about an ambiguous column. Readers who want to know how to join two tables in SQL with the same column names usually run into this problem.

JOIN and INNER JOIN in MySQL

In MySQL, JOIN on its own means the same thing as INNER JOIN, and the word INNER is optional. Many people keep INNER anyway because it states what the join does. Other databases have their own rules, so check the documentation for the one you use.

Common mistakes

  • Leaving out the ON condition. MySQL then pairs every row from the first table with every row from the second. The result is a Cartesian product, with many rows that mean nothing.
  • Matching the wrong columns. The query still runs, but the rows it returns can be wrong. Check that the columns hold the same kind of value and mean the same thing. Joining a customer ID to an order number, for example, runs without error and gives meaningless matches.
  • Expecting unmatched rows. An inner join drops them. If you need rows without a match, you need an outer join, and “What’s the difference between inner and outer join?” explains when to use one.

The complete guide to group by with joins

Using GROUP BY With INNER JOIN in MySQL
Read the guide

Keep exploring

Guides from other topics

All topics →