MySQL

Unleash the power of MySQL with this comprehensive card pack designed for learners of all levels in 2025! Whether you're a beginner or an advanced user, these cards cover everything you need to know, from essential statements and constraints to complex data types and functions.

Knowledge check

Take a test before studying to see which topics you know and where the gaps are.

Take the test

Messenger lessons

Study cards and revisit what you have learned in interactive lessons.

How lessons work

Spaced repetition

Get individual cards to review at intervals that help you remember them longer.

How repetition works
SELECT Statement

The SELECT statement is used to select data from a database.


The data returned is stored in a result table, called the result-set.

1/47
WHERE Clause

The WHERE clause is used to filter records.


It is used to extract only those records that fulfill a specified condition.

2/47
Operators in The WHERE Clause

The following operators can be used in the WHERE clause.

3/47
AND, OR and NOT Operators

The WHERE clause can be combined with AND, OR, and NOT operators.

4/47
ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

5/47
INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

6/47
NULL Values

A field with a NULL value is a field with no value.

7/47
UPDATE Statement

The UPDATE statement is used to modify the existing records in a table.

8/47
DELETE Statement

The DELETE statement is used to delete existing records in a table.

9/47
LIMIT and OFFSET Clauses

The LIMIT clause is used to specify the number of records to return.

10/47
MIN() and MAX() Functions

The MIN() function returns the smallest value of the selected column.


The MAX() function returns the largest value of the selected column.

11/47
COUNT(), AVG() and SUM() Functions

The COUNT() function returns the number of rows that matches a specified criterion.


The AVG() function returns the average value of a numeric column.


The SUM() function returns the total sum of a numeric column.

12/47
LIKE Operator

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

13/47
IN Operator

The IN operator allows you to specify multiple values in a WHERE clause.


The IN operator is a shorthand for multiple OR conditions.

14/47
BETWEEN Operator

The BETWEEN operator selects values within a given range.

15/47
Aliases

Aliases are used to give a table, or a column in a table, a temporary name.


Aliases are often used to make column names more readable.


An alias only exists for the duration of that query.


An alias is created with the AS keyword.

16/47
INNER JOIN Keyword

The INNER JOIN keyword selects records that have matching values in both tables.

17/47
LEFT JOIN Keyword

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records (if any) from the right table (table2).

When a left row has no right-table match, its right-table columns are NULL in the result. In the example, customers without orders still appear.

18/47
RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records (if any) from the left table (table1).

When a right row has no left-table match, its left-table columns are NULL in the result. In the example, employees without orders still appear.

19/47
CROSS JOIN Keyword

The CROSS JOIN keyword returns all records from both tables (table1 and table2).

With no filtering condition, CROSS JOIN produces every pair of rows: if the two tables have m and n rows, the result has m × n rows. A WHERE condition can then retain only pairs with matching keys.

20/47
Self Join

A self join is a regular join, but the table is joined with itself.

Use different aliases for the two references to the same table. In the example, A and B both refer to Customers; A.City = B.City finds people in the same city, while A.CustomerID <> B.CustomerID excludes a row paired with itself.

21/47
UNION Operator

The UNION operator is used to combine the result-set of two or more SELECT statements.

22/47
GROUP BY Statement

The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".

23/47
HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

24/47
EXISTS Operator

The EXISTS operator is used to test for the existence of any record in a subquery.


The EXISTS operator returns TRUE if the subquery returns one or more records.

25/47
ANY and ALL Operators

The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.

26/47
INSERT INTO SELECT Statement

The INSERT INTO SELECT statement copies data from one table and inserts it into another table.

27/47
CASE Statement

The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else statement).

28/47
NULL Functions

IFNULL and COALESCE functions let you return an alternative value if an expression is NULL.

29/47
Comments

Comments are used to explain sections of SQL statements, or to prevent execution of SQL statements.

30/47
CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a new SQL database.

In MySQL, CREATE DATABASE IF NOT EXISTS avoids an error if the database already exists. The statement requires the CREATE privilege for that database. CHARACTER SET can specify the database’s default character set; CREATE SCHEMA is a synonym for CREATE DATABASE.

31/47
DROP DATABASE Statement

The DROP DATABASE statement is used to drop an existing SQL database.

In MySQL, DROP DATABASE removes the database and all its tables; review the target carefully before running it. IF EXISTS avoids an error when the database does not exist. The statement requires the DROP privilege, and DROP SCHEMA is a synonym.

32/47
CREATE TABLE Statement

The CREATE TABLE statement is used to create a new table in a database.

33/47
DROP TABLE Statement

The DROP TABLE statement is used to drop an existing table in a database.


The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself.

DROP TABLE removes both the table definition and its data. IF EXISTS avoids an error when the named table does not exist. TRUNCATE TABLE instead removes rows while retaining the table definition.

34/47
ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.


The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

35/47
NOT NULL Constraint

The NOT NULL constraint enforces a column to NOT accept NULL values.

36/47
UNIQUE Constraint

The UNIQUE constraint ensures that all values in a column are different.

37/47
PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each record in a table.

38/47
FOREIGN KEY Constraint

The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.

39/47
CHECK Constraint

The CHECK constraint is used to limit the value range that can be placed in a column.

40/47
DEFAULT Constraint

The DEFAULT constraint is used to set a default value for a column.


The default value will be added to all new records, if no other value is specified.

41/47
CREATE INDEX Statement

The CREATE INDEX statement is used to create indexes in tables.

42/47
AUTO INCREMENT Field

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table.

43/47
Views

A view is a named query that presents rows and columns like a virtual table. It can draw fields from one or more underlying tables.

MySQL evaluates the view definition when queried, so results reflect current underlying data; the view does not store a separate static copy of the rows. Use CREATE VIEW to define it, CREATE OR REPLACE VIEW to replace its definition, and DROP VIEW to remove it.

When an identifier contains spaces, MySQL uses backticks for quoting. The examples below use names without spaces for simpler syntax.

44/47
String Data Types

Choose a string type by whether values are text or raw bytes, and by the required length.

CHAR(n) is fixed length; VARCHAR(n) is variable length. Their declared lengths count characters. The effective VARCHAR maximum also depends on row-size limits and character encoding.

BINARY(n) and VARBINARY(n) store byte strings; their lengths count bytes. Use them for bytes rather than character text.

TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT store increasingly large text values. The corresponding BLOB types store binary values. Their storage limits are byte-based, so multibyte characters reduce the number of text characters that fit.

Use a suitable character set and collation for text comparisons. Do not assume VARCHAR(65535) can hold 65,535 multibyte characters.

45/47
Numeric Data Types

Choose a numeric type according to its range and whether exact arithmetic matters.

TINYINT, SMALLINT, MEDIUMINT, INT, BIGINT are progressively wider integer types. UNSIGNED disallows negative values and extends the positive range. BOOL/BOOLEAN are synonyms for TINYINT(1); zero is false and nonzero is true in boolean contexts.

DECIMAL stores exact fixed-point values and is suitable for money. FLOAT and DOUBLE store approximate floating-point values, so avoid assuming exact decimal equality.

BIT(n) stores bit values. Integer display width does not change the values a type can store. Integer display widths and ZEROFILL are deprecated in MySQL 8.4; ZEROFILL implies UNSIGNED, but new designs should format values in the application or use LPAD().

46/47
Date and Time Data Types

MySQL has five main temporal types: DATE, TIME, DATETIME, TIMESTAMP, YEAR.

DATE stores a calendar date, displayed as YYYY-MM-DD. TIME stores a time or duration, displayed as hh:mm:ss; it can also represent negative durations.

DATETIME stores a calendar date and time without automatic time-zone conversion. TIMESTAMP stores an instant, with session time-zone conversion on storage and retrieval; its range is roughly 1970 to 2038, while DATETIME supports a much wider calendar range.

YEAR stores a four-digit year. TIME, DATETIME, and TIMESTAMP can use an optional fractional-seconds precision from 0 to 6; the default is 0. TIMESTAMP and DATETIME columns can be configured with DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP.

47/47