You're going to crush this! Let's go ๐ช
๐ผ Why is this useful?
โจ Target Result
Select columns from a table
SELECT name FROM users
SELECT * FROM products
-- * means all columns
Filter rows with conditions
WHERE price > 100
WHERE name = 'Alice'
WHERE name LIKE 'A%'
Sort results
ORDER BY name ASC
ORDER BY price DESC
Combine data from multiple tables
INNER JOIN
Returns only matching rows from both tables
SELECT * FROM A INNER JOIN B ON A.id = B.a_id
LEFT JOIN
All rows from left table + matching from right
SELECT * FROM A LEFT JOIN B ON A.id = B.a_id
RIGHT JOIN
All rows from right table + matching from left
SELECT * FROM A RIGHT JOIN B ON A.id = B.a_id
FULL OUTER JOIN
All rows from both tables
SELECT * FROM A FULL OUTER JOIN B ON A.id = B.a_id
Group for aggregation
GROUP BY department
HAVING COUNT(*) > 2
Calculate values
COUNT(*), SUM(col)
AVG(col), MIN(col)
Strings need single quotes
WHERE name = Alice
WHERE name = 'Alice'
Use HAVING, not WHERE for groups
WHERE COUNT(*) > 2
HAVING COUNT(*) > 2
Only select grouped or aggregate cols
SELECT name, dept GROUP BY dept
SELECT dept, COUNT(*) GROUP BY dept
JOIN needs ON to connect tables
SELECT * FROM orders JOIN customers
JOIN customers ON orders.cust_id = customers.id
FROM needs a table name
SELECT * FROM
SELECT * FROM employees
Check spelling carefully
SELEC * FROM table
SELECT * FROM table
ORDER BY comes after GROUP BY
ORDER BY col GROUP BY col2
GROUP BY col2 ORDER BY col
Use LIKE for patterns
WHERE name = 'A%'
WHERE name LIKE 'A%'