Mysql

MySQL

SQL commands and their ordering

previous query(s)

WITH - queries as variables yay! (e.g. WITH previous_results AS (the actual previouw query), then you can use the previous table)

first

SELECT - required "view" (col_name1, col_name2 or * for all) FROM - required table name WHERE - filter rows with comparators

before ORDER BY

GROUP BY - group rows by column (e.g. price, movie_category) HAVING - filter groups with comparators

last

ORDER BY __ ASC/DESC - sort by column values LIMIT - row limit

reason why multiple tables are created

tables split by meaningful groups (e.g. customer_orders, customer_subscriptions) are meant to contain just the information necessary to describe what was ordered.

However, a lot of this information would be repeated. If the same customer has multiple subscriptions, that customer’s name and address will be reported multiple times. If the same subscription type is ordered by multiple customers, then the subscription price and subscription description will be repeated. This will make our table big and unmanageable.

JOIN example

SELECT customer_name, address, purchase_date
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id;

JOINS

JOIN / INNER JOIN

LEFT JOIN

CROSS JOIN

UNION

SQL has strict rules for appending data:

Tables must have the same number of columns.
The columns must have the same data types in the same order as the first table.

primary and foreign keys

PRIMARY KEYS

FOREIGN KEYS

CANDIDATE KEYS

Question
Is it possible for a table to have more than one unique identifier column, like an id column?

Answer
Yes, it is possible for a table to have more than one column which can uniquely identify a row of data. A column that can uniquely identify a record of data is known as a "Candidate Key". Tables can have multiple "Candidate Key"s, each of which could potentially be the "Primary Key", but there must only be one "Primary Key" per table. Usually, the column chosen as the "Primary Key" follows the naming convention like customer_id or product_id.

For example, say that we had a table of employee records, with the columns employee_id and phone_number. Every employee has a unique employee_id value, and a unique phone_number value. Both of these columns can be unique identifiers for a row, so they are "Candidate keys", but the "Primary Key" would most likely be set to employee_id.

the most common types of joins will be joining a foreign key from one table with the primary key from another table

CROSS JOIN example

TASK: find total number of subscriptions per month

-- first, filter out and COUNT() newpaper subscriptions in march (3)
SELECT COUNT(*)
FROM newspaper
WHERE
start_month <= 3 AND
end_month >= 3;

-- now CROSS JOIN over all months (a table with one column depicting months)
SELECT *
FROM newspaper
CROSS JOIN months;

-- now filter them by teach month instead of one month
SELECT *
FROM newspaper
CROSS JOIN months
WHERE
start_month <= month AND
end_month >= month;

-- finally group them and COUNT() them by each month
-- CROSS JOIN is used here just to "loop over" each month
-- kinda like "for i in range(10)" in python
SELECT month, COUNT(*)
FROM newspaper
CROSS JOIN months
WHERE
start_month <= month AND
end_month >= month
GROUP BY month;

WITH example

  1. make a table called previous
  2. do a SELECT on the table previous
WITH previous AS (
SELECT *, COUNT(subscription_id)
  AS 'subscriptions'
FROM orders
GROUP BY customer_id
)
SELECT * FROM previous ORDER BY subscriptions;

notes on MySQL

  1. JOIN without ON is possible (ON is optional for MySQL)
    • this makes it a CROSS JOIN

REFs