Using SQL JOIN: Guide to Joining Tables
Meta description: Learn how to join multiple tables into a single query using SQL JOIN, INNER-OUTER JOIN differences, and the use of aliases.
🎯 What Will You Learn in This Guide?
In this comprehensive guide, you will learn how to join tables in SQL, step by step.
You will see INNER JOIN and OUTER JOIN types with examples, and learn to simplify your queries by using alias (alias).
We will also show you how you can test these examples on the cloud infrastructure offered by GenixNode.
🧠 Stage 1 – Technical Analysis
- Main topic: JOIN operations (INNER, OUTER, LEFT, RIGHT) in SQL.
- Purpose: To create meaningful result sets by combining related data from different tables into a single query.
- Summary: In this guide, you will set up a sample database and learn JOIN syntax, ON / USING, alias usage and performance tips.
⚙️ 1. Preparing the Environment
Connect to MySQL Server
ssh admin@tr1-node01.ornek.com
mysql -u admin -p
💬 These commands allow you to connect to MySQL.
Create Sample Database
CREATE DATABASE genixnode_db;
USE genixnode_db;
💬 Creates and activates the new database.
🧱 2. Setup Sample Tables
Table Purpose Key products Keeps product information productID team Sales personnel information employeeID, productExpertise sales Sales records salesID, productID, salesperson
Products Table
CREATE TABLE urunler (
urunID INT PRIMARY KEY,
urunAdi VARCHAR(20),
fiyat DECIMAL(5,2)
);
💬 Product ID, name and price information.
Team Table
CREATE TABLE ekip (
calisanID INT PRIMARY KEY,
calisanAdi VARCHAR(20),
urunUzmanlik INT,
FOREIGN KEY (urunUzmanlik) REFERENCES urunler(urunID)
);
💬 Associates the product in which employees are experts.
Sales Table
CREATE TABLE satislar (
satisID INT PRIMARY KEY,
adet INT,
urunID INT,
satisElemani INT,
FOREIGN KEY (urunID) REFERENCES urunler(urunID),
FOREIGN KEY (satisElemani) REFERENCES ekip(calisanID)
);
💬 Associates each sales record with the product and employee.
💾 3. Add Data
INSERT INTO urunler VALUES
(1, 'Gecitci', 18.99),
(2, 'Aletcik', 14.49),
(3, 'Cihazcik', 39.99),
(4, 'Merdane', 11.50),
(5, 'NeOlaKi', 29.99);
💬 Creates the product list.
INSERT INTO ekip VALUES
(1, 'Elif', 1),
(2, 'Ali', 4),
(3, 'Deniz', 3),
(4, 'Burak', 2);
💬 Sales team information.
INSERT INTO satislar VALUES
(1, 7, 1, 1),
(2, 10, 5, 4),
(3, 8, 2, 4),
(4, 1, 3, 3),
(5, 5, 1, 3);
💬 Sample sales data.
🔗 4. Using JOIN
INNER JOIN (Default)
SELECT ekip.calisanAdi, urunler.urunAdi, urunler.fiyat
FROM urunler JOIN ekip
ON urunler.urunID = ekip.urunUzmanlik;
➡️ Returns matching records in both tables.
Easy Assembly with USING
SELECT satislar.satisID, satislar.adet, urunler.urunAdi
FROM satislar JOIN urunler USING (urunID)
ORDER BY satisID;
➡️ USING can be used instead of ON in columns with the same name.
LEFT JOIN (Show All Sales)
SELECT s.satisID, s.adet, e.calisanAdi
FROM satislar AS s
LEFT JOIN ekip AS e
ON s.satisElemani = e.calisanID;
➡️ NULL is returned for unmatched sales.
RIGHT JOIN (Show All Employees)
SELECT s.satisID, s.adet, e.calisanAdi
FROM satislar AS s
RIGHT JOIN ekip AS e
ON s.satisElemani = e.calisanID;
➡️ Lists all employees, those who do not make sales will return NULL.
FULL OUTER JOIN (Alternative in MySQL)
There is no direct FULL JOIN in MySQL, it can be emulated with UNION:
SELECT * FROM satislar LEFT JOIN ekip ON satislar.satisElemani = ekip.calisanID
UNION
SELECT * FROM satislar RIGHT JOIN ekip ON satislar.satisElemani = ekip.calisanID;
🧮 5. Use of Alias
SELECT S.satisID, S.adet, P.urunAdi, (P.fiyat * S.adet) AS gelir
FROM satislar AS S JOIN urunler AS P USING (urunID);
💬 Alias shortens the query and increases readability.
⚡ 6. Performance Tips to speed up JOIN operations:
Add INDEX to the merged columns.
Do not call unnecessary columns in the SELECT section.
For complex JOINs, test the query first.
❓ Frequently Asked Questions (FAQ)
- What is the difference between JOIN and WHERE?
JOIN joins the tables, WHERE filters the result.
- When to use LEFT JOIN?
When you want to keep all data in the left table.
- How to do FULL JOIN in MySQL?
By combining the results of both left and right JOIN using UNION.
- What is the best way to improve JOIN performance?
Adding index to JOIN columns.
☁️ Conclusion and Recommendation
In this guide, you learned how to use SQL JOIN to combine related tables into a single query. JOIN operations are indispensable for data analysis, reporting and data integrity. 💼 To take your database experience to the next level, you can immediately create and practice MySQL examples on the GenixNode infrastructure.

