Skip to main content
Log inGet a demo
Back to SQL Dictionary
Joins

SQL JOIN WHERE

What is SQL JOIN with WHERE Clause?

The SQL JOIN operation, combined with the WHERE clause, is used to retrieve data from two or more tables based on specified conditions. It allows you to filter the joined data and create a result set that includes only the rows that meet the specified criteria.

When you would use it

You would use the SQL JOIN with WHERE clause when you want to:

  1. Combine data: Join tables to retrieve data from multiple sources and create a unified result set.
  2. Apply conditions: Filter the data based on specific criteria to include only the relevant rows in the result set.
  3. Create complex queries: Build advanced queries that involve multiple tables and filtering conditions.

Syntax

The syntax for using SQL JOIN with WHERE clause is as follows:

SELECT column_list
FROM table1
JOIN table2 ON table1.column_name = table2.column_name
WHERE condition;
  • column_list: The columns you want to retrieve from the joined tables.
  • table1 and table2: The names of the tables to be joined.
  • column_name: The common column(s) or key(s) that are used to establish the relationship between the tables.
  • condition: The condition that specifies the filtering criteria for the joined data.

Parameter values

  • column_list: A list of column names you want to select from the joined tables.
  • table1 and table2: The names of the tables that you want to join.
  • column_name: The column(s) that create the relationship between the tables. This can be one or more columns.
  • condition: The condition that specifies the criteria for filtering the joined data.

Example query

Suppose you have two tables, "orders" and "customers," and you want to retrieve a list of orders placed by customers who reside in a specific city (e.g., New York). You can use a SQL JOIN with a WHERE clause like this:

SELECT orders.order_id, customers.customer_name
FROM orders
JOIN customers ON orders.customer_id = customers.customer_id
WHERE customers.city = 'New York';

Example table response

Assuming the "orders" and "customers" tables contain the following data:

orders:

| order_id | customer_id |
| -------- | ----------- |
| 1        | 101         |
| 2        | 102         |
| 3        | 103         |

customers:

| customer_id | customer_name | city       |
| ---------- | ------------- | ---------- |
| 101        | John Smith    | New York   |
| 102        | Mary Johnson  | Los Angeles|
| 103        | Sam Brown     | New York   |

The query mentioned earlier would return the following result:

| order_id | customer_name |
| -------- | ------------- |
| 1        | John Smith    |
| 3        | Sam Brown     |

This result combines data from both tables but includes only the rows where the customers' city is "New York."

Use cases

  • Combining data from multiple tables while applying specific filtering criteria.
  • Retrieving data from related tables while including only the relevant rows in the result set.
  • Creating complex queries that involve joining tables and applying additional conditions.

SQL languages this is available for

The SQL JOIN with WHERE clause is a standard SQL feature and is available in most relational database management systems (RDBMS), including but not limited to:

  • MySQL
  • PostgreSQL
  • Oracle Database
  • SQL Server
  • SQLite
  • IBM Db2
  • MariaDB

The syntax and behavior of the JOIN with WHERE clause are consistent across these database systems.

Related

SQL JOIN ON

SQL Common Table Expression (CTE)

SQL Stored Procedures

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.