Module 1: MySQL Basics
Work through the complete MYSQL stack: querying, joins, indexes, schema design, constraints, views, stored.
01MySQL SQLWhat is MySQL? MySQL is the world's most popular open-source relational database management system (RDBMS). It was created in. SQL (Structured Query Language) is the language. MySQL is a. Open-source — free to use under GPL license Cross-platform — Wcodequizbeginner
What is MySQL? MySQL is the world's most popular open-source relational database management system (RDBMS). It was created in. SQL (Structured Query Language) is the language. MySQL is a. Open-source — free to use under GPL license Cross-platform — W
-- Connect via CLI
mysql -u root -p
mysql -u username -p -h hostname database_name
-- Show version
SELECT VERSION();
-- Show current user
SELECT USER();MySQL is:
SQL vs MySQL:
MySQL's default storage engine is:
02MySQL SELECTThe SELECT Statement SELECT is the most fundamental MySQL command — it retrieves data from one or more tables. MySQL processes clauses in this order (different from how you. FROM / JOIN WHERE SELECT column1, column2, ... FROM table_name; -- Select alcodequizbeginner
The SELECT Statement SELECT is the most fundamental MySQL command — it retrieves data from one or more tables. MySQL processes clauses in this order (different from how you. FROM / JOIN WHERE SELECT column1, column2, ... FROM table_name; -- Select al
SELECT column1, column2, ...
FROM table_name;
-- Select all columns
SELECT * FROM customers;
-- Select specific columns
SELECT first_name, last_name, email
FROM customers;
-- With calculation
SELECT
product_name,
price,
price * 1.1 AS price_with_tax
FROM products;
-- With string function
SELECT
CONCAT(first_name, ' ', last_name) AS full_name,
UPPER(email) AS email_upper
FROM customers;SELECT * means:
MySQL uses backticks (`) for:
Which is NOT a valid SELECT clause?
03MySQL SELECT DISTINCTSELECT DISTINCT — Remove Duplicates DISTINCT filters out duplicate rows from the result set. It applies to all columns in the. -- Get unique cities (ignores duplicates) SELECT DISTINCT city FROM customers; -- DISTINCT applies to the COMBINATION of cocodequizbeginner
SELECT DISTINCT — Remove Duplicates DISTINCT filters out duplicate rows from the result set. It applies to all columns in the. -- Get unique cities (ignores duplicates) SELECT DISTINCT city FROM customers; -- DISTINCT applies to the COMBINATION of co
-- Get unique cities (ignores duplicates)
SELECT DISTINCT city FROM customers;
-- DISTINCT applies to the COMBINATION of columns
SELECT DISTINCT city, country FROM customers;
-- Returns unique city+country pairs
-- Count distinct values
SELECT COUNT(DISTINCT city) AS unique_cities
FROM customers;
-- DISTINCT with ORDER BY
SELECT DISTINCT category
FROM products
ORDER BY category;SELECT DISTINCT city, country returns:
COUNT(DISTINCT column) counts:
DISTINCT is applied:
04MySQL WHEREWHERE Clause — Filtering Rows WHERE filters rows BEFORE they are returned. Only rows where the condition is TRUE are included. SELECT column1, column2 FROM table_name WHERE condition; -- Comparison operators WHERE price = 99.99 -- equal WHERE price !codequizbeginner
WHERE Clause — Filtering Rows WHERE filters rows BEFORE they are returned. Only rows where the condition is TRUE are included. SELECT column1, column2 FROM table_name WHERE condition; -- Comparison operators WHERE price = 99.99 -- equal WHERE price !
SELECT column1, column2
FROM table_name
WHERE condition;
-- Comparison operators
WHERE price = 99.99 -- equal
WHERE price != 99.99 -- not equal (also: <>)
WHERE price > 100 -- greater than
WHERE price >= 100 -- greater or equal
WHERE price < 50 -- less than
WHERE price <= 50 -- less or equal
-- String comparison
WHERE name = 'Alice' -- exact match (case-insensitive by default)
WHERE status = 'active'
-- Range
WHERE price BETWEEN 10 AND 100 -- inclusive!
WHERE age BETWEEN 18 AND 65
-- NULL check (NEVER use = NULL!)
WHERE email IS NULL
WHERE email IS NOT NULL
-- Multiple conditions (AND, OR, NOT)
WHERE price > 100 AND category = 'Electronics'
WHERE city = 'NYC' OR city = 'LA'
WHERE NOT status = 'deleted'To check if a column is NULL in MySQL:
WHERE price BETWEEN 10 AND 50 includes:
WHERE price > 100 AND category = 'Electronics':
05MySQL ORDER BYORDER BY — Sorting Results ORDER BY sorts the result set. Without ORDER BY, MySQL makes NO guarantee about row order. -- Sort ascending (default) SELECT * FROM products ORDER BY price; SELECT * FROM products ORDER BY price ASC; -- Sort descending SELcodequizbeginner
ORDER BY — Sorting Results ORDER BY sorts the result set. Without ORDER BY, MySQL makes NO guarantee about row order. -- Sort ascending (default) SELECT * FROM products ORDER BY price; SELECT * FROM products ORDER BY price ASC; -- Sort descending SEL
-- Sort ascending (default)
SELECT * FROM products ORDER BY price;
SELECT * FROM products ORDER BY price ASC;
-- Sort descending
SELECT * FROM products ORDER BY price DESC;
-- Sort by multiple columns
SELECT * FROM employees
ORDER BY department ASC, salary DESC;
-- Sorts by dept A→Z, then by salary high→low within each dept
-- Sort by column alias
SELECT name, price * 0.9 AS discounted
FROM products
ORDER BY discounted DESC;
-- Sort by expression
SELECT * FROM orders ORDER BY YEAR(created_at) DESC, MONTH(created_at);
-- Sort by column position (avoid in production)
SELECT name, age, salary FROM employees ORDER BY 3 DESC; -- 3 = salaryDefault sort order without ASC/DESC is:
ORDER BY dept ASC, salary DESC means:
Without ORDER BY, MySQL guarantees: