How to Query Multiple Tables in SQL
Last Updated :
16 Dec, 2024
SQL (Structured Query Language) is a powerful tool for managing and querying relational databases. One of its most valuable features is the ability to query multiple tables simultaneously, allowing us to retrieve and integrate related data efficiently.
In this article, we will explain how to query multiple tables in SQL with examples, using a step-by-step approach. By the end, we will be able to write queries to combine data from different tables in a well-structured database.
How to Query Multiple Tables in SQL
For this example, we will create a database named geeks, define two tables (department and employee), populate the tables with sample data, and execute queries to extract data from both tables using different methods. Each step will be explained in detail.
Department Table
The department
table will store department-related information, such as ID, salary, name, and department ID. Use the following SQL statement to create it:
Create Table department(
ID int,
SALARY int,
NAME Varchar(20),
DEPT_ID Varchar(255));
INSERT INTO department VALUES (1,'Neha','F','1994-06-03');
INSERT INTO department VALUES (2,'Harsh','M','1996-03-12');
INSERT INTO department VALUES (3,'Harsh','M','1995-05-01');
INSERT INTO department VALUES (4,'Rupali','F',1996-11-11');
INSERT INTO department VALUES (5,'Rohan','M','1992-03-08');
Output

Departments Table
Employee Table
Now create another table called employee that will store employee-specific details, including ID, email, and city. This table will relate to the department
table via the ID
column. Create the table using below query.
CREATE TABLE employee(
ID int,
Email Varchar(255),
City Varchar(20) );
INSERT INTO employee VALUES (1, "[email protected]", "Noida");
INSERT INTO employee VALUES (2, "[email protected]", "Jaipur");
INSERT INTO employee VALUES (3, "[email protected]", "Noida");
INSERT INTO employee VALUES (4, "[email protected]", "Jaipur");
INSERT INTO employee VALUES (5, "[email protected]", "Noida");
Output

employee table
Example 1: Using a Simple SELECT Statement
The most common way to query multiple tables is with a simple SELECT expression. To integrate results from different tables, use the FROM clause to name more than one table. Here’s how it works in practice:
Syntax
SELECT table1name.column1name, table2name.column2name
FROM table1name, table2name
WHERE table1name.column1name = table2name.column1name;
This query retrieves combined data from both tables based on the matching ID
column. It is simple yet effective for basic requirements.
Query:
SELECT department.ID, department.NAME, employee.Email, employee.City
FROM department, employee
WHERE department.ID = employee.ID;
Output

Querying Multiple Tables
Example 2: Using JOINs
SQL JOINs provide a more structured and readable way to query multiple tables. They are particularly useful for complex queries.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,….
FROM table1 JOIN table2
ON table1.matching_column = table2.matching_column;
Join the department
and employee
tables to fetch combined data: This query retrieves the same result as the previous example but uses the JOIN
clause, which is more efficient and easier to read in complex queries.
Query:
SELECT department.ID, department.NAME, employee.Email, employee.City
FROM department JOIN employee ON department.ID = employee.ID;
Output

Using JOINS
Understanding the Use Case
When working with relational databases, data is often stored across multiple tables to maintain normalization and reduce redundancy. Querying multiple tables allows us to:
- Combine related data stored in separate tables, providing a unified view.
- Generate meaningful reports by correlating data from different entities.
- Maintain scalability and structure in large databases by avoiding data duplication.
Conclusion
Querying multiple tables in SQL is a fundamental skill for working with relational databases. Whether we use a simple SELECT
statement or structured JOIN
operations, understanding these techniques is essential for extracting meaningful insights from our data. By mastering these methods, we can effectively combine and analyze data across complex datasets. With practice, we’ll be able to create robust queries that enhance data driven decision making and streamline database management tasks.
Similar Reads
Querying Multiple Tables in SQL
SQL (Structured Query Language) is a powerful tool for managing and querying relational databases. One of its most valuable features is the ability to query multiple tables simultaneously, allowing us to retrieve and integrate related data efficiently. In this article, we will explain how to query m
4 min read
How to Retrieve Data from Multiple Tables in SQL?
In SQL, retrieving data from multiple tables is a common requirement in database operations. Efficiently combining data from different tables allows developers to create complex queries and extract valuable insights from interconnected datasets. In this article, we will explore multiple approaches t
5 min read
How to Retrieve Data From Multiple Tables in PL/SQL?
Retrieving data from multiple tables is a common task in PL/SQL and It is a skill that can significantly enhance our data manipulation capabilities. Whether we are joining tables to fetch related data or using subqueries to extract specific information, knowing how to navigate multiple tables is ess
4 min read
How to Retrieve Data from Multiple Tables in PL/SQL
PL/SQL is âProcedural Language extensions to the Structured Query Languageâ. SQL is a popular language for both querying and updating data in relational database management systems (RDBMS). PL/SQL adds many procedural constructs to SQL language to overcome some limitations of SQL. In addition, PL/SQ
5 min read
SELECT data from Multiple Tables in SQL
In SQL (Structured Query Language), it is a common requirement to retrieve data from more than one table at once. When you work with relational databases, you often have to combine data from multiple tables to get meaningful results. SQL provides many methods for selecting data from multiple tables,
4 min read
SQL Query to Exclude Multiple Values
To exclude multiple values to be fetched from a table we can use multiple OR statements but when we want to exclude a lot of values it becomes lengthy to write multiple AND statements, To avoid this we can use the NOT IN clause with the array of values that need to be excluded with the WHERE stateme
2 min read
How to Limit Query Results in SQL?
SQL provides powerful features like the LIMIT clause to control the number of rows returned, making it an essential tool for optimizing queries and retrieving precise results. Whether weâre managing large datasets or working on performance tuning, understanding how to limit query results is key. In
4 min read
How to Perform SQL Join on Multiple Columns in Same Table?
To perform a SQL JOIN on multiple columns in the same table, we use the Self Join. This technique allows us to create connections between different columns of the same table by comparing them directly. We can implement a Self Join using various types of joins such as âinner,â âleft,â âright,â âfull,
4 min read
How to Get Multiple Counts With Single Query in SQLite?
In data analysis, obtaining multiple counts for different categories is a common requirement. SQLite, a lightweight and versatile database management system, offers a powerful feature that allows us to achieve this efficiently. In this article, we'll explore how to use SQLite to retrieve multiple co
3 min read
How to List All Tables in Oracle?
In this article, we will discuss all the methods to list all tables in the oracle SQL Database. We have three types of a subset of tables available to use as identifiers which in turn help us to sort the required table names. Here, are the following types of table identifiers in the Oracle SQL Datab
2 min read