def test_all_joins(persons):
    "Tests the four different types of joins in SQL."
    # Create and populate the Orders table.
    orders = Table(('O_Id', int), ('OrderNo', int), ('P_Id', int))
    orders.insert(1, 77895, 3)
    orders.insert(2, 44678, 3)
    orders.insert(3, 22456, 1)
    orders.insert(4, 24562, 1)
    orders.insert(5, 34764, 15)
    # Test the inner join function.
    inner_join(ROW.Persons.P_Id == ROW.Orders.P_Id,
               Persons=persons, Orders=orders) \
               .select('Persons.LastName',
                       'Persons.FirstName',
                       'Orders.OrderNo') \
                       .order_by('Persons.LastName').table().print()
    # Test inner join with alias.
    inner_join(ROW.p.P_Id == ROW.po.P_Id,
               p=persons, po=orders) \
               .select('po.OrderNo', 'p.LastName', 'p.FirstName') \
               .where((ROW.p.LastName == 'Hansen') &
                      (ROW.p.FirstName == 'Ola')).print()
    # Test left join with and without alias.
    left_join((persons, 'Persons'), (orders, 'Orders'),
              ROW.Persons.P_Id == ROW.Orders.P_Id) \
              .select('Persons.LastName',
                      'Persons.FirstName',
                      'Orders.OrderNo') \
                      .order_by('Persons.LastName').table().print()
    left_join((persons, 'p'), (orders, 'o'), ROW.p.P_Id == ROW.o.P_Id) \
              .select('p.LastName',
                      'p.FirstName',
                      'o.OrderNo') \
                      .order_by('p.LastName').table().print()
    # Test right join with and without alias.
    right_join((persons, 'Persons'), (orders, 'Orders'),
               ROW.Persons.P_Id == ROW.Orders.P_Id) \
               .select('Persons.LastName',
                       'Persons.FirstName',
                       'Orders.OrderNo') \
                       .order_by('Persons.LastName').table().print()
    right_join((persons, 'p'), (orders, 'o'), ROW.p.P_Id == ROW.o.P_Id) \
               .select('p.LastName', 'p.FirstName', 'o.OrderNo') \
               .order_by('p.LastName').table().print()
    # Test full join with and without alias.
    full_join(ROW.Persons.P_Id == ROW.Orders.P_Id,
              Persons=persons, Orders=orders) \
              .order_by('Persons.LastName').table().print()
    full_join(ROW.p.P_Id == ROW.o.P_Id,
              p=persons, o=orders) \
              .select('p.LastName', 'p.FirstName', 'o.OrderNo') \
              .order_by('p.LastName').table().print()
    return orders
Tests the four different types of joins in SQL.
recipes/Python/577825_Rudimentary_Database_Engine/recipe-577825.py