While organizing our technical knowledge base, we revisited the classic “Bro Code” SQL course. It’s a fantastic resource for beginners and a great refresher for pros. To keep these insights handy, I’ve compiled a detailed “scholar’s notebook” based on the complete course structure.
The core logic of this tutorial flows from Setup -> Data Operations (CRUD) -> Advanced Queries.
Phase 1: Building the Structure (DDL - Data Definition)
-
CREATE DATABASE: Where it all begins.CREATE DATABASE myDB; USE myDB; -- Don't forget to switch to it! -
CREATE TABLE: Defining your schema.- Focus on Data Types:
INT,VARCHAR,DECIMAL,DATE. - Example:
CREATE TABLE employees (id INT, name VARCHAR(50));
- Focus on Data Types:
-
ALTER TABLE: Fixing design regrets.ADD(new column),RENAME(table/column),DROP(remove column),MODIFY(change type).
Phase 2: Playing with Data (DML - Data Manipulation)
-
INSERT INTO: Adding data.INSERT INTO employees VALUES (1, 'Spongebob');
-
SELECT: The most important command.SELECT *(everything) vsSELECT name(specific columns).WHERE: The filter (age >= 18).
-
UPDATE: Modifying existing data.- ⚠️ High Risk: Always include a
WHEREclause! UPDATE employees SET hourly_pay = 10.50 WHERE id = 1;
- ⚠️ High Risk: Always include a
-
DELETE: Removing data.- ⚠️ Critical Risk: Missing
WHEREmeans wiping the table—a precursor to “rm -rf /”.
- ⚠️ Critical Risk: Missing
Phase 3: Setting Rules (Constraints)
Constraints keep your data clean and reliable.
NOT NULL: Field cannot be empty.UNIQUE: No duplicates allowed (e.g., emails).DEFAULT: Fallback value if none is provided (DEFAULT 0).PRIMARY KEY: The fingerprint. Unique + Not Null.AUTO_INCREMENT: Let the ID count itself.FOREIGN KEY: The bridge between tables (prevents orphan data).
Phase 4: Advanced Query Skills
-
Functions:
COUNT(): Headcount.MAX()/MIN(): Extremes.AVG(): Averages.SUM(): Totals.
-
Wildcards (
LIKE):%: Matches any number of characters (LIKE 'a%'= everyone starting with ‘a’)._: Matches exactly one character.
-
ORDER BY: Sorting.DESC(Descending),ASC(Ascending).
-
LIMIT: Pagination tool.LIMIT 10: First 10 rows.LIMIT 10, 10: Skip 10, take next 10.
Phase 5: Combination Skills (Joins & Unions)
-
UNION: Vertical stacking.- Combines results from two queries into one list (columns must match).
-
JOINs: Horizontal stitching.INNER JOIN: The intersection (only matches).LEFT JOIN: All from Left, matches from Right (NULL if no match).RIGHT JOIN: The reverse.
Phase 6: Advanced Logic
-
Views:
- Saving a complex query as a “virtual table” for easy access later.
-
Indexes:
- Like a book’s table of contents. Speeds up reads, slows down writes (updates).
-
Subqueries:
- Nested queries.
SELECT ... WHERE id IN (SELECT ...).
- Nested queries.
-
GROUP BY: Categorizing.- Used with aggregations (e.g., “Average salary per department”).
-
HAVING:- The
WHEREclause for groups. (Key interview distinction:WHEREfilters rows,HAVINGfilters groups).
- The