SQL Advanced Query Operations and Data Analysis
Step Snap 1 [JOIN Syntax Variations]: There are two main syntaxes for table joins in SQL:
SELECT t.column1, t.column2
FROM table1 t, table2 z
WHERE t.id = z.id
SELECT t.column1, t.column2
FROM table1 t
JOIN table2 z ON t.id = z.id
Key differences:
Different JOIN types for various scenarios:
-- INNER JOIN (default): returns only matching rows
FROM table1 t JOIN table2 z ON t.id = z.id
-- LEFT JOIN: keeps all rows from left table
FROM table1 t LEFT JOIN table2 z ON t.id = z.id
-- RIGHT JOIN: keeps all rows from right table
FROM table1 t RIGHT JOIN table2 z ON t.id = z.id
-- FULL OUTER JOIN: keeps all rows from both tables
FROM table1 t FULL OUTER JOIN table2 z ON t.id = z.id
Data integrity check example:
-- Check for missing related records
SELECT *
FROM table1
WHERE id NOT IN (SELECT id FROM table2)