0% found this document useful (0 votes)
38 views

MySQL Cheat Sheet

The document provides an overview of filtering and aggregating data in MySQL, including using WHERE clauses to filter on numeric and text columns, and functions like COUNT, SUM, AVG, MIN, MAX to aggregate data. It also discusses grouping data using GROUP BY and sorting aggregated results with ORDER BY. The sample data set contains details of the highest valued media franchises by revenue like Star Wars, Mickey Mouse, and Pokémon.

Uploaded by

BilalAhmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

MySQL Cheat Sheet

The document provides an overview of filtering and aggregating data in MySQL, including using WHERE clauses to filter on numeric and text columns, and functions like COUNT, SUM, AVG, MIN, MAX to aggregate data. It also discusses grouping data using GROUP BY and sorting aggregated results with ORDER BY. The sample data set contains details of the highest valued media franchises by revenue like Star Wars, Mickey Mouse, and Pokémon.

Uploaded by

BilalAhmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

> Filtering Data > Aggregating Data

MySQL Cheat Sheet Filtering on numeric columns Simple aggregations

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)

SELECT franchise, inception_year


FROM franchises
FROM franchises

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)

applications and online publishing.


FROM franchises
Get rows where a number is equal to a value with WHERE col = n
SELECT franchise, inception_year
Get the maximum value of a column with SELECT MAX(col)
FROM franchises

> Sample Data WHERE inception_year = 1996


SELECT MAX(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

WHERE inception_year <> 1996


Franchise inception_year total_revenue_busd original_medium owner n_movies Get summaries grouped by values with GROUP BY col
Get rows where a number is between two values (inclusive) with WHERE col BETWEEN m AND n
Star Wars
1977
46.7 movie
The Walt Disney
12
SELECT owner, COUNT(*)

Company
SELECT franchise, inception_year
FROM franchises

Mickey Mouse The Walt Disney


FROM franchises
GROUP BY owner
1928 52.2 cartoon
and Friends Company WHERE inception_year BETWEEN 1928 AND 1977
Get summaries grouped by values, in order of summaries with GROUP BY col ORDER BY smmry DESC
Anpanman 1973 38.4 book Froebel-kan 33
SELECT original_medium, SUM(n_movies) AS total_movies

Winnie the Pooh


1924
48.5 book
The Walt Disney
6
Filtering on text columns 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

WHERE original_medium = 'book' FROM franchises

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

SELECT franchise, original_medium


HAVING total_movies > 10
FROM franchises

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

(% represents any characters) WHERE owner = 'The Walt Disney Company'

Get a column from a table by name using SELECT col GROUP BY original_medium

SELECT franchise, original_medium

SELECT franchise
FROM franchises
ORDER BY total_movies DESC

FROM franchises WHERE original_medium LIKE '%oo%' HAVING total_movies > 10

Get multiple columns from a table by name using SELECT col1, col2
Filtering on multiple columns
SELECT franchise, inception_year

FROM franchises > MySQL-Specific Syntax


Get the rows where one condition and another condition holds with WHERE condn1 AND condn2
Override column names with SELECT col AS new_name
N ot all code works in every dialect of SQL. The following examples work in MySQL, but are not
SELECT franchise, inception_year, total_revenue_busd

SELECT franchise, inception_year AS creation_year


guaranteed to work in other dialects.
FROM franchises

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 *

SELECT franchise, inception_year


FROM franchises

SELECT franchise, inception_year, total_revenue_busd

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 *

SELECT franchise, total_revenue_busd


Filtering on missing data FROM franchises

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

Limit the number of rows returned with LIMIT n


SELECT franchise, n_movies
SELECT *

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

You might also like