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 aliases1SELECT2 o.id AS order_id,3 o.total,4 c.name AS customer-- orders (alias o) is the left table5FROM6 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 orders8 customers c ON o.customer_id = c.id-- Sort by customer name9ORDER BY10 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. 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