Special Datatype Handling

CASE

Def

Syntax: CASE, WHEN, THEN, ELSE structure

SELECT 
    col1, 
    col2, 
    CASE 
        WHEN condition1 THEN result1
        WHEN condition2 THEN result2
    ELSE resultN AS col3
FROM tbl;

DISTINCT

Date Handling Methods

Date Reading/Reformatting

Date Difference

Text Processing Methods

Basics

LIKE keyword

LIKE Def

Exact Character Matching

_ Operator

– eg, extracting rating from text description SELECT SUBSTRING(‘Rated PG-13 for content’ FROM ‘Rated ([\w-]+)’);


## Numerical Handling Methods
Operating on numerical cols together
- Helpful for numerical transformation (eg weighted averages)
```sql
SELECT AVERAGE(vals * weights) FROM tbl
GROUP BY col3

Case Statement Classics

Evaluate Boolean Expression

Main Idea

Why it’s a classic

Implementation

SELECT 
    e.left_operand, 
    e.operator, 
    e.right_operand,
    CASE -- use `CASE` keyword 
        WHEN e.operator = '>' AND v1.value > v2.value THEN 'true' -- don't try to directly pull operator 
        WHEN e.operator = '<' AND v1.value < v2.value THEN 'true' -- translate what it means in SQL
        WHEN e.operator = '=' AND v1.value = v2.value THEN 'true' -- set the true conditions and else false all else
        ELSE 'false' -- else false cuz there's more false cases than true ones
    END AS value
FROM expressions e -- double join so we have
JOIN variables v1 ON e.left_operand = v1.name -- one col w/ left translated into nums
JOIN variables v2 ON e.right_operand = v2.name; -- one col w/ right translated into nums

Aggregations Conceptual

Def

Syntax

Aggregations Classics

1193. Monthly Transactions I

Main Idea

Why it’s classic

Implementation

SELECT 
    -- we format YYYY-MM to avoid counting Jan 2024 and Jan 2025 as the same
    DATE_FORMAT(trans_date, '%Y-%m') AS month, 
    country,
    COUNT(*) AS trans_count,
    -- using case statements wrapped in for conditional aggregation
    SUM(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) AS approved_count,
    SUM(amount) AS trans_total_amount,
    SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) AS approved_total_amount
FROM Transactions
GROUP BY YEAR(trans_date), MONTH(trans_date), country;

1045. Customers Who Bought All Products

Main Idea

Intuition

Why it’s classic

Implementation

SELECT c.customer_id FROM Customer as c
GROUP BY c.customer_id
HAVING COUNT(DISTINCT(c.product_key)) = (SELECT COUNT(DISTINCT(p.product_key)) FROM Product AS p)

Subqueries Conceptual

Background: EXISTS and NOT EXISTS Keyword

Def

Syntax: EXISTS

Syntax: NOT EXISTS

Subqueries Definition and Use Cases

Def

Use Cases

Subqueries Classics

Customers Who Bought A and B but Not C

Main Idea

Intuition

Why it’s classic

Implementation

SELECT c.customer_id, c.customer_name FROM customers AS c 
WHERE 
    EXISTS (
        SELECT * FROM orders AS o
        WHERE o.customer_id = c.customer_id AND product_name = 'A'
    )
    AND
    EXISTS (
        SELECT * FROM orders AS o
        WHERE o.customer_id = c.customer_id AND product_name = 'B'
    )
    AND
    NOT EXISTS (
        SELECT * FROM orders AS o
        WHERE o.customer_id = c.customer_id AND product_name = 'C'
    )
ORDER BY c.customer_name

570. Managers with at Least 5 Direct Reports

Main Idea

Why it’s a classic

Implementation

SELECT e1.name FROM Employee AS e1
WHERE (
    SELECT COUNT(*) FROM Employee AS e2
    WHERE e2.managerId = e1.id
    ) >= 5

Views

Definition and Syntax

Def

Syntax

Properties

Common Table Expressions (CTEs)

Definition and Syntax

Def

Syntax

Properties

Materialized Views

Definition and Syntax

Def

Syntax

Properties

Data Definitional Language (DDL) Conceptual

Definition and Syntax

Def

Syntax: Creating Tables

CREATE TABLE cars (
    id VARCHAR(20) PRIMARY KEY,  --VARCHAR(n) means we allow alphanumeric characters making a string of length n
    carName VARCHAR(100) NOT NULL, 
    factoryId VARCHAR(14) UNIQUE,  --UNIQUE means each row in this table has a unique val under this col
    price DECIMAL(10, 2),  --DECIMAL(k, m) means this col can hold k digits with 2 of them being right of the decimal point
    inStock BOOLEAN, 
    numSold INT, 
    popular BIT -- values are either 1 or 0, conversion to BIT means all non-zero values are converted to 1
);

Syntax: Dropping Tables

DROP TABLE erase_tbl; -- drops erase_tbl entirely from the database
DROP TABLE IF EXISTS erase_tbl; -- same as above but with error handling

Syntax: Dropping, Adding and Altering Columns

ALTER TABLE cars
    ADD saleDate DATE, 
    DROP factoryId;

Data Manipulation Language (DML) Conceptual

Definition and Syntax

Def

Syntax: Adding Rows

CREATE TABLE cars (
    id VARCHAR(20) PRIMARY KEY,  
    carName VARCHAR(100) NOT NULL, 
    factoryId VARCHAR(14) UNIQUE,  
    price DECIMAL(10, 2),  
    inStock BOOLEAN, 
    numSold INT, 
    popular BIT
);

INSERT INTO target_relation
VALUES
    ('AAa19AYha7', '2025 Honda Accord', 'io7186BH', 22925.60, TRUE, 190, 1), 
    ('AAa18BHoau8s', '2026 Toyota Corolla Cross', 'jk71bah1s', 30952.57, TRUE, 171, 0);
    -- we insert tuples where each value must correspond with the schema for the given target_relation or else we error

Syntax: Manipulating Rows

Window Functions Conceptual

Window Functions Classics

Recursive Subqueries Conceptual

Recursive Subqueries Classics