UNION
There is no such
FULL JOIN
in MySQL but we can get the same result using
UNION
SELECT 
    students.*, 
    notes.*
FROM `students` 
LEFT JOIN notes ON notes.student_id = students.id
UNION
SELECT 
    students.*, 
    notes.*
FROM `students` 
RIGHT JOIN notes ON notes.student_id = students.id
As default,
UNION
removes duplicate rows. If we want to get all the rows including duplicates, we can use
UNION ALL
SELECT 
    students.*, 
    notes.*
FROM `students` 
LEFT JOIN notes ON notes.student_id = students.id
UNION ALL
SELECT 
    students.*, 
    notes.*
FROM `students` 
RIGHT JOIN notes ON notes.student_id = students.id
Divides all the students into different classes
SELECT *, 'Brilliant' AS class
FROM `students`
WHERE points >= 90
UNION
SELECT *, 'Gold'
FROM `students`
WHERE points >= 80 
    AND points < 90
UNION
SELECT *, 'Silver'
FROM `students`
WHERE points >= 60 
    AND points < 80
UNION
SELECT *, 'Lazy'
FROM `students`
WHERE points < 60
ORDER BY points DESC
by Valeri Tandilashvili
4 years ago
MySQL
UNION
1
Pro tip: use ```triple backticks around text``` to write in code fences