MySQL Examples

This page contains MySQL examples, including sample scripts for installing a DB, inserting data, running queries, and more.

Sample Database

Most examples linked to from this page use the Sakila sample database.

Install Sample Database

Installs the "Sakila" sample database complete with thousands of rows of data. Enables you to practice running queries against a database that contains lots of data.

SQL Statements

MySQL DELETE Statement

Basic statement for deleting a record from a table.

MySQL INSERT Statement

Basic statement for inserting data into a table.

MySQL SELECT Statement

Basic statement for selecting data from a table.

MySQL UPDATE Statement

Basic statement for updating data in a table.

Joins & Subqueries

MySQL INNER JOIN

For queries across multiple tables. Returns records where there is at least one row in both tables that match the join condition.

MySQL LEFT JOIN

For queries across multiple tables. All rows from the left table will be returned, even if there's no matching row in the right table.

MySQL RIGHT JOIN

For queries across multiple tables. All rows from the right table will be returned, even if there's no matching row in the left table.

MySQL Self Join

Self joins allow you to join a table to itself. Find out how…

MySQL Subquery

A subquery is a query within a query. Can be used anywhere an expression is allowed.

Clauses & Keywords

MySQL DISTINCT

Removes duplicate rows from the result set of an SQL query.

MySQL GROUP BY Clause

Allows you to group the results of an SQL statement.

MySQL HAVING Clause

Allows you to filter a GROUP BY clause.

MySQL ORDER BY Clause

Allows you to order the results of an SQL statement.

MySQL WHERE Clause

Allows you to narrow an SQL statement to a specific criteria.

Functions

MySQL CHAR_LENGTH()

Returns the number of characters in its parameter. Same as CHARACTER_LENGTH().

MySQL CONCAT()

Returns the string that results from concatenating its parameters.

MySQL COUNT()

Returns the number of rows in the result set of an SQL query.

MySQL LENGTH()

Returns the length of its parameter, in bytes. Same as OCTET_LENGTH().

MySQL SUM()

Aggregate function that returns the sum of an expression.