1.

What are joins in SQL?

Answer»

A join clause is a SQL command used to combine records from multiple tables or retrieve data from these tables based on the existence of a common field (column) between them. A join condition and SELECT statement can be used to join the tables. Using the SQL JOIN clause, records can be fetched from two or more tables in a database and combined. In general, they are used when users need to retrieve data from tables that contain many-to-many or one-to-many relationships between them.

Example: Let's take a look at two tables. Here’s the Employee table.

Emp_IDEmp_NameEmp_No
101Ashish Kaktan9450425345
102Raj Choudhary8462309621
103Vivek Oberoi7512309034
104Shantanu Khandelwal9020330023
105Khanak Desai8451004522

Here's the Employment table. 

Emp_IDEmp_ProfileEmp_CountryEmp_Join_Date
101Content WriterGermany2021-04-20
104Data AnalystIndia2022-12-11
105Software EngineerIndia2022-01-03
108Development ExecutiveEurope2023-02-15
109Marketing ManagerMexico2020-05-23

Let us now join these two tables together using a SELECT statement, as shown below.

SELECT Emp_ID, Emp_Name, Emp_No, Emp_Profile, Emp_Country FROM Employee, Employment WHERE  Employee.Emp_ID = Employment.Emp_ID;

Output:

Emp_IDEmp_NameEmp_NoEmp_ProfileEmp_Country 
101Ashish Kaktan9450425345Content WriterGermany
104Shantanu Khandelwal9020330023Data AnalystIndia
105Khanak Desai8451004522Software EngineerIndia



Discussion

No Comment Found