MySQL Cheat Sheet
MySQL Cheat Sheet
Get rows where a number is greater than a value with WHERE col > n Get the total number of rows SELECT COUNT(*)
SELECT franchise, inception_year
SELECT COUNT(*)
FROM franchises
FROM franchises
Learn SQL online at www.DataCamp.com WHERE inception_year > 1928
Get the total value of a column with SELECT SUM(col)
Get rows where a number is greater than or equal to a value with WHERE col >= n
SELECT SUM(total_revenue_busd)
WHERE inception_year >= 1928 Get the mean value of a column with SELECT AVG(col)
What is MySQL? SELECT AVG(total_revenue_busd)
Get rows where a number is less than a value with WHERE col < n
FROM franchises
MySQL is an open-source relational database management system (RDBMS) known for its fast SELECT franchise, inception_year
FROM franchises
Get the minimum value of a column with SELECT MIN(col)
performance and reliability. Developed by Oracle Corporation, it's widely used for web
WHERE inception_year <= 1977 SELECT MIN(total_revenue_busd)
FROM franchises
Get rows where a number is not equal to a value with WHERE col <> n or WHERE col != n
The dataset contains details of the world's highest valued media franchises by gross revenue.
Each row contains one franchise, and the table is named franchises.
SELECT franchise, inception_year
Grouping, filtering, and sorting
FROM franchises
Company
SELECT franchise, inception_year
FROM franchises
Company
GROUP BY original_medium
The Pokémon
Pokémon 1996 88 video game 24 ORDER BY total_movies DESC
Company
Get rows where text is equal to a value with WHERE col = 'x'
Disney Princess 2000 45.4 movie
The Walt Disney
Company SELECT franchise, original_medium
Get rows where values in a group meet a criterion with GROUP BY col HAVING condn
FROM franchises
SELECT original_medium, SUM(n_movies) AS total_movies
GROUP BY original_medium
> Querying tables Get rows where text is one of several values with WHERE col IN ('x', 'y')
ORDER BY total_movies DESC
Get all the columns from a table using SELECT * WHERE original_medium IN ('movie', 'video game') Filter before and after grouping with WHERE condn_before GROUP BY col HAVING condn_after
SELECT *
SELECT original_medium, SUM(n_movies) AS total_movies
FROM franchises Get rows where text contains specific letters with WHERE col LIKE '%abc%'
FROM franchises
Get a column from a table by name using SELECT col GROUP BY original_medium
SELECT franchise
FROM franchises
ORDER BY total_movies DESC
Get multiple columns from a table by name using SELECT col1, col2
Filtering on multiple columns
SELECT franchise, inception_year
FROM franchises
WHERE inception_year < 1950 AND total_revenue_busd > 50 Limit the number of rows returned, offset from the top with LIMIT m, n
Arrange the rows in ascending order of values in a column with ORDER BY col
Get the rows where one condition or another condition holds with WHERE condn1 OR condn2 SELECT *
FROM franchises
LIMIT 2, 3
FROM franchises
ORDER BY inception_year
WHERE inception_year < 1950 OR total_revenue_busd > 50 B y default, MySQL uses case insensitive matching in WHERE clauses.
Arrange the rows in descending order of values in a column with ORDER BY col DESC
SELECT *
FROM franchises
WHERE owner = 'THE WALT DISNEY COMPANY'
ORDER BY total_revenue_busd DESC
Get rows where values are missing with WHERE col IS NULL o get case sensitive matching, use WHERE BINARY condn
T
SELECT *
FROM franchises
FROM franchises
FROM franchises
WHERE n_movies IS NULL WHERE BINARY owner = 'THE WALT DISNEY COMPANY'
LIMIT 2
Get rows where values are not missing with WHERE col IS NOT NULL Get the current date with CURDATE() and the current datetime with NOW() or CURTIME()
Get unique values with SELECT DISTINCT
SELECT franchise, n_movies
SELECT CURDATE(), NOW(), CURTIME()
SELECT DISTINCT owner
FROM franchises
FROM franchises WHERE n_movies IS NOT NULL List available tables with show tables
show tables