Below statement could be used to get data from multiple tables, so, we need to use join to get data from multiple tables.
Syntax :
SELECT tablenmae1.colunmname, tablename2.columnnmae FROM tablenmae1 JOIN tablename2 ON tablenmae1.colunmnam = tablename2.columnnmae ORDER BY columnname;
Let us take three tables, two tables of customers named Geeks1, Geeks2 and Geeks3.
Geeks1 table :
| ID | FirstName |
|---|---|
| 1 | Nisha |
| 2 | Manoj |
| 3 | Pooja |
Geeks2 table :
| ID | LastName |
|---|---|
| 1 | Gupta |
| 2 | Desai |
| 3 | Kumari |
Geeks3 table :
| GID | PID | Asset |
|---|---|---|
| 1 | P1 | Laptop |
| 2 | P2 | Desktop |
| 3 | P3 | Laptop |
| 4 | P4 | None |
Example to select from multiple tables :
SELECT Geeks3.GID, Geeks3.PID,
Geeks3.Asset, Geeks1.FirstName,
Geeks2.LastName
FROM Geeks3
LEFT JOIN Geeks1
ON Geeks3.GID = Geeks1.ID
LEFT JOIN Geeks2
ON Geeks3.GID = Geeks2.ID
Output :
| GID | PID | Asset | FirstName | LastName |
|---|---|---|---|---|
| 1 | P1 | Laptop | Nisha | Gupta |
| 2 | P2 | Desktop | Manoj | Desai |
| 3 | P3 | Laptop | Pooja | Kumari |
| 4 | P4 | None | NULL | NULL |
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- Select into and temporary tables in MS SQL Server
- Joining three or more tables in SQL
- Copy tables between databases in SQL Server using Import-and-Export Wizard
- Copy tables between databases in SQL Server
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL | SELECT Query
- SQL | SELECT TOP Clause
- Performing Database Operations in Java | SQL CREATE, INSERT, UPDATE, DELETE and SELECT
- SELECT INTO statement in SQL
- Select statement in MS SQL Server
- Select top in MS SQL Server
- Insert Into Select statement in MS SQL Server
- SQL | Query to select NAME from table using different options
- SQL | Procedures in PL/SQL
- SQL | Difference between functions and stored procedures in PL/SQL
- Difference between T-SQL and PL-SQL
- Difference between SQL and T-SQL
- SQL | Checking Existing Constraints on a Table using Data Dictionaries
- Manipulate R Data Frames Using SQL
- Numeric and Date-time data types in SQL Server
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

