SQL RIGHT JOIN – Explained with Syntax and Examples
Introduction
In SQL, text data types are used to store alphanumeric values like names, addresses, emails, and descriptions. Choosing the correct text type — CHAR, VARCHAR, or TEXT — is important for optimizing storage space, query speed, and database performance.
In this section, you'll learn the definitions, differences, and best use cases for each text data type.
1. CHAR (Fixed-Length String)
CHAR is used to store fixed-length strings. If the stored string is shorter than the defined length, SQL automatically pads it with spaces to match the specified size.
Features:
- Fixed length
- Fast and predictable performance
- Uses extra storage if the data is often shorter than the specified length
Syntax:
column_name CHAR(length);
length = number of characters (1 to 255 depending on the database system)
Example:
CREATE TABLE countries (
country_code CHAR(2),
country_name CHAR(50)
);
country_code like 'US', 'IN', 'UK' will always take 2 characters.
When to Use CHAR:
- Data with a constant size, such as country codes, gender ('M', 'F'), state abbreviations
- Fixed-format fields like credit card types ('VISA', 'MC')
- When exact storage size is known and consistent
2. VARCHAR (Variable-Length String)
VARCHAR stands for Variable Character. It stores variable-length strings, meaning only the actual characters are stored without unnecessary padding.
Features:
- Variable length
- More space-efficient than CHAR for varying-length text
- Slightly slower than CHAR when processing large volumes (because of extra calculations for string lengths)
Syntax:
column_name VARCHAR(length);
length = maximum number of characters allowed
Example:
CREATE TABLE employees (
first_name VARCHAR(50),
email VARCHAR(100)
);
Names and emails can vary in length, making VARCHAR ideal.
When to Use VARCHAR:
- Data with unpredictable or variable length
- Names, emails, addresses, and descriptions under 255-65535 characters
- Most general-purpose text fields
3. TEXT (Large Text Field)
TEXT is used to store large amounts of text like long descriptions, blog posts, comments, or articles.
Features:
- Meant for large text storage (up to 65,535 characters for standard TEXT in MySQL)
- Cannot have a default value (in some databases like MySQL)
- TEXT fields are stored outside the main table with a pointer reference
- Different variants exist (TINYTEXT, MEDIUMTEXT, LONGTEXT) for various sizes
Syntax:
column_name TEXT;
Example:
CREATE TABLE articles (
id INT,
title VARCHAR(255),
body TEXT
);
body will store the full article content, which can be very large.
When to Use TEXT:
- Long-form text fields (comments, articles, reviews, reports)
- Data that exceeds normal VARCHAR limits
- When exact storage requirements are unknown or potentially very large
Quick Comparison: CHAR vs VARCHAR vs TEXT
Feature | CHAR | VARCHAR | TEXT |
---|---|---|---|
Storage | Fixed length | Variable length | Variable, large storage |
Max Size | Up to 255 chars | 65,535 bytes (typically) | 65,535+ chars (depends on type) |
Performance | Fast for fixed-size | Efficient for variable text | Slightly slower for queries |
Indexing | Full index support | Full index support | Limited in some DBs |
Best Use Case | Codes, fixed formats | Names, addresses, emails | Articles, long descriptions |
Important Tips
- Use CHAR only when all values will be exactly the same length
- VARCHAR is the best choice for most standard text fields
- Reserve TEXT for content that exceeds VARCHAR limits
- Consider VARCHAR(MAX) in SQL Server for large text that might need indexing
- Be aware that TEXT fields may have limitations on default values and full-text indexing
What is a RIGHT JOIN in SQL?
A RIGHT JOIN (also known as a RIGHT OUTER JOIN) returns all rows from the right table, and the matched rows from the left table. If there is no match from the left table, the result will contain NULL for the left-side columns.
Think of it as: "Give me everything from the right table, and match info from the left table — if available."
RIGHT JOIN Syntax
SELECT table1.column1, table2.column2, ...
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
- table1 is the left table
- table2 is the right table (all its rows are included)
- common_column is the key used to match rows
Example Tables
employees
id | name | department_id |
---|---|---|
1 | Alice | 101 |
2 | Bob | 102 |
departments
id | department_name |
---|---|
101 | Sales |
102 | IT |
103 | HR |
RIGHT JOIN Query
SELECT
employees.name,
departments.department_name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.id;
Result:
name | department_name |
---|---|
Alice | Sales |
Bob | IT |
NULL | HR |
✅ The HR department appears even though no employee is assigned to it.
When to Use RIGHT JOIN
- When you want to get all data from the right table, even if there's no match in the left
- Helpful in reporting missing relationships
- To find orphaned data (like departments without employees)
RIGHT JOIN vs LEFT JOIN
Feature | LEFT JOIN | RIGHT JOIN |
---|---|---|
All rows from | Left table | Right table |
Unmatched rows | NULLs from right table | NULLs from left table |
Common use | Keep all main records | Show missing connections |
Tip:
If your database does not support RIGHT JOIN (e.g., SQLite), you can reverse the tables and use LEFT JOIN instead.
-- Equivalent of RIGHT JOIN using LEFT JOIN
SELECT d.department_name, e.name
FROM departments d
LEFT JOIN employees e
ON d.id = e.department_id;
Conclusion
The SQL RIGHT JOIN is useful for keeping all rows from the right-side table and adding matched data from the left-side table.