![]() |
In relational databases like MySQL, data is often spread across multiple tables to maintain normalization and avoid redundancy. To effectively work with such data, you need to combine and retrieve records from these tables using various types of joins and other methods. This article will guide you through different ways to retrieve records from multiple tables in MySQL. How to Retrieve Records from Multiple Tables in MySQLThe JOIN keyword connects two or more tables based on a specific column. This can be used to access records of both tables and retrieve them in a single SELECT statement. The syntax for utilizing JOINs in MySQL hinges on the JOIN keyword followed by the table names and the JOIN type, along with the ON clause to define the joining condition. Syntax:
Parameters:
Retrieve Records From Multiple Tables ExamplesLet’s look at some examples of how to retrieve records from multiple tables in MySQL. First, let’s create a demo MySQL database on which we will perform the MySQL queries. CREATE TABLE customers ( Output: customers table:
orders table:
After creating the demo tables, let’s look at MySQL queries to retrieve records from multiple tables. Using INNER JOIN to Retrieve Records From Multiple TablesIn this example, we have created a database as Record and Consider two tables customers and orders. We want to retrieve customer names and their corresponding order details with the help of foreign keys as written here. SELECT customers.name, orders.order_id, orders.product_name Output:This query will only return records where a customer in the customers table has a matching entry in the orders table, based on the customer_id column. +------------+----------+--------------+ Explanation: The SELECT query retrieves the names of customers along with order details such as order_id and product_name by performing an INNER JOIN on the ‘customers‘ and ‘orders‘ tables using the common key ‘id’ and ‘customer_id‘, respectively. Using LEFT JOIN to Retrieve Records From Multiple TablesRetrieve customer names and their corresponding order details with the help of a foreign key. SELECT customers.name, orders.order_id, orders.product_name Output:This query will include all customers from the customers table, with matching order details from the orders table if available. If no order exists for a customer, the order_id and product_name columns will be populated with NULL values. +------------+----------+--------------+ Explanation: The SELECT query fetches customer names along with order details such as order_id and product_name. It uses a LEFT JOIN to include all customers, even those without orders, linking them based on the common key ‘id‘ and ‘customer_id‘. Using UNION-- Retrieve customer names from the customers table Output: +------------+ Explanation: This UNION operation combines the results of two queries into a single result set. It retrieves customer names from the ‘customers’ table and product names from the ‘orders‘ table, ensuring there are no duplicate rows. ConclusionRetrieving records from multiple tables in MySQL is a common requirement and can be accomplished using various types of joins, subqueries, and UNION operations. Understanding these techniques allows you to efficiently query your database and extract the necessary data for your applications. Use INNER JOIN for matching records, LEFT JOIN or RIGHT JOIN when you need all records from one table regardless of matches, and UNION or subqueries for more complex retrieval scenarios. FAQs: Retrieving Records from Multiple Tables in MySQLWhat is a JOIN in MySQL?
How can I retrieve records from multiple tables without matching rows?
What is the UNION operator in MySQL?
|
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |