RIGHT JOIN in SQL

Level 4130 keystrokes

RIGHT JOIN returns all rows from the right table and matched rows from the left.

SQL code

-- Return order columns and the customer name, with readable aliases
1SELECT
2 o.id AS order_id,
3 o.total,
4 c.name AS customer
-- orders (alias o) is the left table
5FROM
6 orders o
-- RIGHT JOIN keeps every row of the right table, customers (alias c)
7RIGHT JOIN
-- A customer with no order (Carol) gets NULL in order_id and total
-- Same result as customers LEFT JOIN orders
8 customers c ON o.customer_id = c.id
-- Sort by customer name
9ORDER BY
10 c.name;

Output

+----------+-------+----------+
| order_id | total | customer |
+----------+-------+----------+
|        1 | 99.50 | Alice    |
|        2 | 45.00 | Bob      |
|     NULL | NULL  | Carol    |
+----------+-------+----------+
3 rows in set
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this SQL code and how often they appear.

  • .× 6
  • _× 2
  • ,× 2
  • ;× 1
  • =× 1

More SQL examples at level 4

← SQL typing practice