Skip to main content

Joins

A join combines data from two or more related tables based on a common column. In SQLAlchemy, joins are performed using the join() and outerjoin() methods.

Types of Joins

SQL supports two categories of joins:
  • Inner Join
  • Outer Join
The Outer Join category includes:
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
Note: In SQL, the OUTER keyword is optional.
  • LEFT JOIN = LEFT OUTER JOIN
  • RIGHT JOIN = RIGHT OUTER JOIN
  • FULL JOIN = FULL OUTER JOIN

Sample Tables

courses

students

Inner Join

Returns only the rows that have matching records in both tables.

Task

Retrieve each student’s name along with the course they enrolled in.

SQL

SQLAlchemy

Expected Output

Left Outer Join

Returns all rows from the left table and matching rows from the right table. If no matching record exists, the right-side columns contain NULL.

Task

Retrieve all students along with their course names, including students who are not enrolled in any course.

SQL

SQLAlchemy

Expected Output

Right Outer Join

Returns all rows from the right table and matching rows from the left table. If no matching record exists, the left-side columns contain NULL.
Note: SQLite does not support RIGHT OUTER JOIN.

Task

Retrieve all courses along with their enrolled students, including courses that have no students.

SQL

SQLAlchemy

Expected Output

Full Outer Join

Returns all rows from both tables. Matching rows are combined, while non-matching rows contain NULL.
Note: SQLite does not support FULL OUTER JOIN.

Task

Retrieve every student and every course, even if they are not related.

SQL

SQLAlchemy

Expected Output

Multiple Join

A multiple join combines more than two tables.

Sample Tables

departments

courses

students

Relationship

Task

Retrieve each student’s name, course name, and department name.

SQL

SQLAlchemy

Expected Output

Summary

Aliases

An alias is a temporary name given to a table or a column within a query. Aliases improve readability and are useful when:
  • The same table is used multiple times.
  • Table names are long.
  • Column names become ambiguous after joins.
In SQLAlchemy:
  • aliased() creates a table alias.
  • label() creates a column alias.

Table Alias

Task

Retrieve the student names using a table alias.
SQL
SQLAlchemy

Column Alias

Task

Display the student’s name as Student Name.
SQL
SQLAlchemy

Self Join

Aliases are required when the same table appears multiple times.

Task

Retrieve every employee along with their manager’s name.
SQL
SQLAlchemy

Subqueries

A subquery is a query written inside another query. The result of the inner query is used by the outer query.

Task

Retrieve students enrolled in courses having a duration greater than 6 months.
SQL
SQLAlchemy

Scalar Subquery

A scalar subquery returns a single value.

Task

Retrieve students whose marks are greater than the average marks.
SQL
SQLAlchemy

EXISTS

EXISTS checks whether the inner query returns at least one matching row. It is commonly used to determine whether related records exist.

Task

Retrieve all courses having at least one enrolled student.
SQL
SQLAlchemy

Raw SQL

Sometimes writing SQL directly is simpler than using ORM methods. SQLAlchemy allows executing raw SQL using text().

Task

Retrieve students from Hyderabad.
SQL
SQLAlchemy

Window Functions

Window functions perform calculations across a set of related rows while preserving every row in the result. Unlike GROUP BY, which combines multiple rows into a single row, window functions keep every row and add calculated values. They are called Window Functions because the calculation is performed over a window (subset) of rows.
Window functions are commonly used for:
  • Ranking
  • Row numbering
  • Running totals
  • Moving averages
  • Previous and next row comparison

ROW_NUMBER()

Assign a unique row number based on descending marks.
SQL
SQLAlchemy

RANK()

Students with equal marks receive the same rank, and the next rank is skipped.
SQL
SQLAlchemy

DENSE_RANK()

Students with equal marks receive the same rank, and the next rank is not skipped.
SQL
SQLAlchemy

Running Total

Calculate the cumulative marks of students.
SQL
SQLAlchemy

Summary