SQL - SELECT from Multiple Tables with MS SQL Server
Last Updated :
14 Jun, 2021
In SQL we can retrieve data from multiple tables also by using SELECT with multiple tables which actually results in CROSS JOIN of all the tables. The resulting table occurring from CROSS JOIN of two contains all the row combinations of the 2nd table which is a Cartesian product of tables. If we consider table1 contains m rows and table2 contains n rows then the resulting table after selecting two tables i.e cross join of two tables contains m*n rows.
Let us see how to select multiple tables using the MSSQL server:
Creating a database :
Creating a database GeeksForGeeks by using the following SQL query as follows.
CREATE DATABASE GeeksForGeeks;

Using the database :
Using the database student using the following SQL query as follows.
USE GeeksForGeeks;

Creating Tables:
Creating three tables student, branch_details, credit_details with SQL query as follows:
CREATE TABLE student
(
stu_id varchar(10),
stu_name varchar(20),
branch varchar(20)
);

CREATE TABLE branch_details
(
branch_name varchar(10),
subjects INT
);

CREATE TABLE credit_details
(
branch varchar(20),
max_credits INT,
min_credits_required INT
);

Verifying the database :
To view the description of the three tables in the database GeeksForGeeks using the following SQL query as follows.
EXEC sp_columns student;
EXEC sp_columns branch_details;
EXEC sp_columns credit_details;

Inserting data into the three tables:
Inserting rows into tables using the following SQL query as follows:
INSERT INTO student VALUES
('1901401','DEVA','C.S'),
('1901402','HARSH','C.S'),
('1901403','DAVID','E.C'),
('1901404','GAURAV','E.C');
INSERT INTO branch_details VALUES
('C.S',8),
('E.C',7),
('M.E',7),
('I.C.E',9),
('E.E.E',8);
INSERT INTO credit_details VALUES
('C.S',24, 12),
('E.C',21, 11),
('M.E',21, 11),
('I.C.E',27,14),
('E.E.E',24,12);

Verifying the inserted data :
Viewing the three tables after inserting rows by using the following SQL query as follows.
SELECT * FROM student;
SELECT * FROM branch_details;
SELECT * FROM credit_details;

Using SELECT statement for n tables:
SYNTAX:
SELECT columns
FROM table_1, table_2,...table_n
WHERE condition;
Using SELECT statements for 2 tables student, branch_details:
SELECT * FROM student, branch_details;
This table contains 4*5 = 20 rows.
Using SELECT statements for 3 tables student, branch_details, credit_details:
SELECT * FROM student, branch_details, credit_details;
The resulting table contains 5*4*5 = 100 rows.
We can obtain other types of join by selecting multiple tables and mentioning appropriate condition in the WHERE clause but instead of using the SELECT with multiple tables and adding conditions using the keywords of joins is more optimal.
Example:
Query to display students who have enrolled in a course and their particulars.
SELECT student.stu_id, student.stu_name,
student.branch, subjects, max_credits
FROM student, branch_details, credit_details
WHERE student.branch = branch_details.branch_name AND
branch_details.branch_name = credit_details.branch;
Note: When there are columns with the same name in different tables it is good to dot operator to point to the particular tables data.
Similar Reads
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
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
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
How to SQL Select from Stored Procedure using SQL Server?
There may be situations in SQL Server where you need to use a stored procedure to get data from a SQL query. For direct data selection from a stored procedure within a query, SQL Server offers options like OPENQUERY and OPENROWSET. The usual way is running the stored procedure independently and then
3 min read
Select Statement in MS SQL Server
The SELECT statement in SQL Server is a foundational SQL command used for querying and retrieving data from one or more tables within a database. This command allows users to specify which columns and rows to retrieve and apply filters to focus on specific data and perform various operations to mani
4 min read
Retrieve Records from Multiple Tables in MySQL
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 t
5 min read
What is Nested Select Statement in SQL Server
SQL Server is a powerful relational database management system. Sql Server is very good with its robustness and scalability. SQL Server operates as a client-server structure, imparting centralized management and scalability for huge-scale applications and enterprise-stage solutions. It offers advanc
3 min read
How to Query 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
Select into and temporary tables in MS SQL Server
In SQL Server, the SELECT INTO TEMP TABLE statement is used to select data from one or more source tables and insert it into a temporary table. Temporary tables are extremely useful when dealing with intermediate results, or when working with subsets of data within a session without modifying or aff
4 min read
How to Get Multiple Counts With Single Query in SQL Server
In SQL Server, obtaining multiple counts with a single query is a common requirement, especially when we are analyzing data across different conditions. Whether we are tallying the number of active and inactive users or counting orders based on their status by using a single query can speed our data
4 min read