Mysql Course
Mysql Course
Imagine this:
If it weren't for databases, all those circumstances above (and many, many
other similar situations) would be our digital reality. Fortunately, we have
these things called "databases" which are basically just pieces of really
advanced software designed solely and specifically for the purpose of storing
our data in a clear and concise way - allowing us to query said data at our
leisure.
In the coming sections, we'll dive into using the MySQL database as we'll
introduce PostgreSQL. So, let's get started!
SQL vs MySQL
A very common question as people get started with databases is, "what's
the difference between SQL and MySQL?" The answer is quite simple.
MySQL, PostGre, SQL Server, and all the others are the database
engines (software applications) that store the data and execute the
SQL queries.
MySQL, PostgreSQL, Oracle, Microsoft SQL Server, and all the others just
independent database technologies. Fortunately, they all support ANSI SQL
- so once you know one of them, you kind of know all of them. They all have
some unique traits, but the commonalities between them far outweigh the
differences.
The SQL part of “MySQL” stands for “Structured Query Language”. SQL
is the most common standardized language used to access databases.
Depending on your programming environment, you might enter SQL
directly (for example, to generate reports), embed SQL statements into
code written in another language, or use a language-specific API that
hides the SQL syntax.
SQL is defined by the ANSI/ISO SQL Standard. The SQL standard has
been evolving since 1986 and several versions exist. In this
manual, “SQL-92” refers to the standard released in
1992, “SQL:1999” refers to the standard released in 1999,
and “SQL:2003” refers to the current version of the standard. We use
the phrase “the SQL standard” to mean the current version of the SQL
Standard at any time.
Open Source means that it is possible for anyone to use and modify the
software. Anybody can download the MySQL software from the Internet
and use it without paying anything. If you wish, you may study the
source code and change it to suit your needs. The MySQL software
uses the GPL (GNU General Public
License), http://www.fsf.org/licenses/, to define what you may and may
not do with the software in different situations. If you feel uncomfortable
with the GPL or need to embed MySQL code into a commercial
application, you can buy a commercially licensed version from Oracle.
If that is what you are looking for, you should give it a try. MySQL Server
can run comfortably on a desktop or laptop, alongside your other
applications, web servers, and so on, requiring little or no attention. If
you dedicate an entire machine to MySQL, you can adjust the settings
to take advantage of all the memory, CPU power, and I/O capacity
available. MySQL can also scale up to clusters of machines, networked
together.
MySQL Server was originally developed to handle large databases
much faster than existing solutions and has been successfully used in
highly demanding production environments for several years. Although
under constant development, MySQL Server today offers a rich and
useful set of functions. Its connectivity, speed, and security make
MySQL Server highly suited for accessing databases on the Internet.
IMPORTANT! Write down the (strong) password you set when you are
installing MySQL!
This password needs to be strong, and you'll need it again and again as you
move forward. So please take a moment to create a strong password and
make sure that you write in down and/or save it to a known location on your
machine.
Testing Installation
After completing the MySQL installation on your machine, please run the
following commands from the CLI to start MySQL (if it's not started already)
and verify it's installation:
On Mac:
1. On a Mac you can quickly and easily start and stop MySQL in the
System Preferences:
2. If you'd prefer to start MySQL from the CLI you can run:
sudo /usr/local/mysql/support-files/mysql.server start
1. Please note that if MySQL is already running, this will throw an
error.
3. If the command above does not work try this one: sudo launchctl load
-F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
4. mysql --version
On Linux
/etc/init.d/mysqld start
/etc/init.d/mysqld stop
/etc/init.d/mysqld restart
or
On Windows
After running the appropriate command above for your system, you should
see a nice message printed out that tells you the version of MySQL that is
now installed on your system. For instance, try running the command mysql
--version from the CLI. (Your version number will vary.)
If you're trying to run the "mysql" command but you're receiving the error
message "'mysql' is not recognized as an internal or external command" this
means that the MySQL "bin" directory was not added to the system PATH
variable. The PATH variable contains a list of directories where you're
operating system can search for the "MySQL" command.
To add MySQL to your PATH click here for simple instructions for Mac or
Linux, for PC users please follow the directions below (source):
Please follow the installation instructions here for your specific machine.
After installing MySQL Workbench, please launch the application and click
the "+" icon to create a new connection.
If you do not see a screen resembling the image above, and instead you see
a screen resembling the image below - that means you need to start MySQL
Server. In this case, quit MySQL Workbench, start MySQL (refer to the
previous page for instructions) then relaunch MySQL Workbench.
Understanding Relational Databases
A relational database is a collection of data items with pre-defined
relationships between them. These items are organized as a set of tables
with columns and rows. Tables are used to hold information about the
objects to be represented in the database. Each column in a table holds a
certain kind of data and a field stores the actual value of an attribute. The
rows in the table represent a collection of related values of one object or
entity. Each row in a table could be marked with a unique identifier called a
primary key, and rows among multiple tables can be made related using
foreign keys. This data can be accessed in many different ways without
reorganizing the database tables themselves. (link)
Notice in the image below how each table only contains data about that
element. For instance, the Passenger table only contains information directly
related to the passenger. The flight table only contains information about the
flight. The Payment table only contains information about the payments, and
so on. That said, we can connect/relate these tables to each using keys.
Using this design we are able to keep our database clean, concise and
scalable.
The content below is sourced from https://www.ntu.edu.sg
Introduction
The relational database was proposed by Edgar Codd (of IBM Research)
around 1969. It has since become the dominant database model for
commercial applications. Today, there are many commercial Relational
Database Management System (RDBMS), such as Oracle, IBM DB2 and
Microsoft SQL Server. There are also many free and open-source RDBMS,
such as MySQL, mSQL (mini-SQL) and the embedded JavaDB (Apache
Derby).
Database design is more art than science, as you have to make many
decisions. Databases are usually customized to suit a particular application.
No two customized applications are alike, and hence, no two databases are
alike. Guidelines (usually in terms of what not to do instead of what to do)
are provided in making these design decision, but the choices ultimately rest
on you - the designer.
Step 2: Gather Data, Organize in tables and Specify the Primary Keys
Once you have decided on the purpose of the database, gather the data that
are needed to be stored in the database. Divide the data into subject-based
tables.
Choose one column (or a few columns) as the so-called primary key, which
uniquely identifies each of the rows.
Primary Key
In the relational model, a table cannot contain duplicate rows, because that
would create ambiguities in retrieval. To ensure uniqueness, each table
should have a column (or a set of columns), called primary key, that uniquely
identifies every record of the table. For example, a unique
number customerID can be used as the primary key for
the Customers table; productCode for Products table; isbn for Books table. A
primary key is called a simple key if it is a single column; it is called
a composite key if it is made up of several columns.
Most RDBMSs build an index on the primary key to facilitate fast search and
retrieval.
The primary key is also used to reference other tables (to be elaborated
later).
You have to decide which column(s) is to be used for the primary key. The
decision may not be straight forward but the primary key shall have these
properties:
1. one-to-many
2. many-to-many
3. one-to-one
One-to-Many
In a "class roster" database, a teacher may teach zero or more classes, while
a class is taught by one (and only one) teacher. In a "company" database, a
manager manages zero or more employees, while an employee is managed
by one (and only one) manager. In a "product sales" database, a customer
may place many orders; while an order is placed by one particular customer.
This kind of relationship is known as one-to-many.
Source: ntu.edu.sg
The column teacherID in the child table Classes is known as the foreign key.
A foreign key of a child table is a primary key of a parent table, used to
reference the parent table.
Take note that for every value in the parent table, there could be zero, one, or
more rows in the child table. For every value in the child table, there is one
and only one row in the parent table.
Many-to-Many
Source: ntu.edu.sg
The many-to-many relationship is, in fact, implemented as two one-to-many
relationships, with the introduction of the junction table.
One-to-One
Some databases limit the number of columns that can be created inside a
table. You could use a one-to-one relationship to split the data into two
tables. A one-to-one relationship is also useful for storing certain sensitive
data in a secure table, while the non-sensitive ones in the main table.
Source: ntu.edu.sg
You need to choose an appropriate data type for each column. Commonly
data types include integers, floating-point numbers, string (or text),
date/time, binary, collection (such as enumeration and set).
Normalization
A table is 1NF if every cell contains a single value, not a list of values. This
property is known as atomic. 1NF also prohibits a repeating group of
columns such as item1, item2,.., itemN. Instead, you should create another
table using a one-to-many relationship.
A table is 3NF, if it is 2NF and the non-key columns are independent of each
other. In other words, the non-key columns are dependent on the primary
key, only on the primary key and nothing else. For example, suppose that we
have a Products table with columns productID (primary
key), name and unitPrice. The column discountRate shall not belong
to Products table if it is also dependent on the unitPrice, which is not part
of the primary key.
At times, you may decide to break some of the normalization rules, for
performance reason (e.g., create a column called totalPrice in
the Orders table which can be derived from the orderDetails records); or
because the end-user requested for it. Make sure that you are fully aware of
it, develop programming logic to handle it, and properly document the
decision.
Integrity Rules
You should also apply the integrity rules to check the integrity of your
design:
Each foreign key value must be matched to a primary key value in the table
referenced (or parent table).
You can insert a row with a foreign key in the child table only if the
value exists in the parent table.
If the value of the key changes in the parent table (e.g., the row
updated or deleted), all rows with this foreign key in the child
table(s) must be handled accordingly. You could either (a) disallow
the changes; (b) cascade the change (or delete the records) in the
child tables accordingly; (c) set the key value in the child tables to
NULL.
Most RDBMS can be set up to perform the check and ensure the referential
integrity, in a specified manner.
Besides the above two general integrity rules, there could be integrity
(validation) pertaining to the business logic, e.g., zip code shall be 5-digit
within a certain ranges, delivery date and time shall fall in the business
hours; quantity ordered shall be equal or less than quantity in stock, etc.
These could be carried out in a validation rule (for the specific column) or
programming logic.
Column Indexing
Step 2: Gather data, organize data into tables, and identify primary
keys
I like to start this process by thinking about the primary data elements that
we'll be dealing with. As mentioned above these elements will be:
So, let's start building out these tables. Remember, each table should ONLY
contain fields that correspond directly to that table's element.
UserId
int, primary key, required
First Name
varchar, required
Last Name
varchar, required
Email
varchar, required
DateCreated
varchar, required
ImageId
int, primary key
ImageURL
varchar, required
Date
date, required
Id
int, primary key
UserId_1
int, foreign key
Foreign Key references Users.UserId
Many-to Many
UserId_2
int, foreign key
Foreign Key references Users.UserId
Many to Many
Date
Step 3 - Create Relationships among Tables
Using the great tool found here, I have created a simple Entity Relationship
Diagram (ERD) to visualize the tables and their relationships with each other.
Notice again how each table ONLY contains fields that explicitly correlate to
the table entity. With a simple relational model like this, we only need to
create a user record once, we only need to create a post record once, we
only need to create an image record once, and we only need to create a
"friends" record once. We will never repeat the same information in any
table. We do not have tables that have a never-ending list of columns like a
spreadsheet from hell. It's a very simple diagram that illustrates the data we
need to store, in a nice and tidy fashion and the relationships between the
data.
Here is the code I used while creating diagram above:
Users
---
UserId PK int
FirstName string
LastName string
Email string
DateCreated date
Images
---
ImageId PK int
ImageURL string
Date date
Posts
---
PostId PK int
UserId int FK >- Users.UserId
PostText blob
ImageId int FK >- Images.ImageId
Date date
Users_Friends
---
Id PK int
UserId_1 int FK >- Users.UserId
UserId_2 int FK >- Users.UserId
Date date
In the next section, we'll create the DB Schema and tables. Let's do it!
Creating DB Schema & Tables
Now that we understand the basics of Relational Databases, and how to
model them. Let's go ahead and create one by following the steps below.
6. Refresh the "schemas" tab on the left side of the screen to see the new
schema appear by clicking the refresh icon - the two spinning arrows.
7. Expand the SocialDB Schema by clicking the arrow next to it.
10. Click "Apply" then "Apply" again to execute the following SQL
statement:
12. Click "Apply" and then "Apply" again to execute the following SQL:
14. Click "Apply" then "Apply" again to execute the following SQL:
15. After the table has been created, let's go create the foreign key
constraint for the UserId field (which will reference the UserId field in
the Users table). First, from the same table editor window, click on the
"Foreign Keys" tab towards the bottom of the window.
20. Now let's create the foreign keys required for this table. Start by clicking
on the "foreign keys" tab again.
22. Click "Apply" then "Apply" again to execute the following SQL:
If you have trouble following the instructions above, you can also run the
following SQL to create all the tables and constraints at once. (Though we
highly recommend manually creating each table as this is something you'll
need to do again and again.)
CREATE DATABASE IF NOT EXISTS `SocialDB` /*!40100 DEFAULT CHARACTER SET latin1 */
USE `SocialDB`;
-- MySQL dump 10.13 Distrib 5.7.17, for macos10.12 (x86_64)
--
-- Host: 127.0.0.1 Database: SocialDB
-- ------------------------------------------------------
-- Server version 5.7.17
--
-- Table structure for table `Images`
--
--
-- Dumping data for table `Images`
--
--
-- Table structure for table `Posts`
--
--
-- Dumping data for table `Posts`
--
--
-- Table structure for table `Users`
--
--
-- Dumping data for table `Users`
--
LOCK TABLES `Users` WRITE;
/*!40000 ALTER TABLE `Users` DISABLE KEYS */;
/*!40000 ALTER TABLE `Users` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `Users_Friends`
--
--
-- Dumping data for table `Users_Friends`
--
1. Close all open query tabs in Workbench just so we have a nice clear
slate.
2. In the navigation panel in the left, double click on "SocialDB" so that it is
now highlighted (or bold). This will set SocialDB as the default database
you want to run your queries against. It is possible, and common, to
manage multiple databases from within Workbench.
3. Beside the "Users" table, click on the little icon that looks like a
spreadsheet with a lightning bolt in it.
6. Add a few more rows and click "Apply" to execute SQL along these
lines:
7. Now run
again by clicking the lightning bolt up top. You will see that the records
have been created and the automatic fields have been populated.
8. Let's do the same for our Images table:
Nice! We've done it! We've created a simple relational database, we've
drawn correlations (using primary and foreign keys) between tables, and
we've add a bit of data.
Below is a little sneak peek at some of the amazing things we can now do
with this relational database. In query below we're joining the Users table,
the Posts table and the Images table to get the user's first name and last
name (from the Users table), the PostText (from the Posts table) and the
image URL (from the images table) all in a single query!
Now, let's take a closer look at all the wonderful queries we can run on this
database (and any other)!
Dashboard / My courses / SQL & Databases / Sections / 3) Creating Relational Databases
/ Travel/Tour Company Database
Please list out the tables (and their Celds) required to create a relational database used in a travel/tour company. There
are many ways to go about this. There is no single "correct" answer. If you were building an application to manage a
travel/tour company, what would the database behind that application look like? Please note all datatypes, primary keys
and foreign keys.
Example:
Table: Employees
-----------------
employee_id INT auto-increment primary key
first_name VARCHAR NOT NULL
last_name VARCHAR NOT NULL
email VARCHAR NOT NULL
hire_date DATE NOT NULL
...
Submission status
Mentor Review Status Review Pending (1-on-1 mentorship required for assignment review)
Andrew Francisco
Last modiCed -
Dashboard Course Options Members-Only Forum About
Submission comments
Comments (0)
Add submission
Working with Primary & Foreign Keys
In the submission box below, or in a file upload please answer the following
questions:
- ON UPDATE CASCADE
- ON UPDATE NO ACTION
- ON UPDATE RESTRICT
- ON DELETE CASCADE
- ON DELETE NO ACTION
- ON DELETE RESTRICT
8) What is the most common primary key we use in basically every table we
create?
Importing the Sakila Database
MySQL provides a few very useful demo databases for us to use as we learn
the ropes. You can take a peek at them here under Example Databases. For
the purposes of this tutorial, and learning the basics of SQL, we'll be using
the Sakila Database which you can download here. Under "Example
Databases", save the "sakila database" zip file to your "Downloads" folder.
When the download is complete, unzip the folder. Within the resulting
"unzipped" folder you should see two .sql files - sakila-data.sql and sakila-
schema.sql.
Side note, I know we've just spent some time creating our beautiful custom
database in the previous section, but we'll have to come back to that as the
Sakila DB is just so useful when learning how to use SQL and MySQL.
After downloading the Sakila DB please open MySQL Workbench, open your
connection to "localhost" and navigate to the "Management" tab (for me it's
on the top left).
Click "Start Import" after it completes you will see a screen similar to this:
Great, we've now imported the Sakila Database schema (just the table and
their structure etc). Now, let's import the data by repeating the process of
importing from a self-contained file, but this time we'll choose the sakila-
data.sql file.
To get started, click on "Import from Disk" at the top of the current window:
Select "Import from Self-Contained File" again, and navigate to the sakila-
data.sql file. Then click "Start Import". When that has completed
successfully, go back to the schemas tab:
In addition, run the following queries just to get the lay of the land:
use sakila;
describe actor;
use <DATABASE_NAME>;
For instance:
use sakila;
This will tell MySQL that the queries we're executing should be executed
against the sakila database. Then we can query directly to the tables. For
instance:
If you do not run the "use" query you can still execute queries, but you must
indicate the database name in all queries. For instance:
Let's SELECT all rows and columns from the actor table: (In SQL, "*" means
"ALL")
Output:
Example 2)
Now, let's just SELECT two distinct columns from the actor table, but all
rows.
Output:
Example 3)
Now, let's just SELECT one distinct column from the actor table, but again,
all rows.
Output:
As you can see, the SELECT query is quite simple and intuitive. We can use it
to SELECT data out of the one (or more) tables. (To SELECT data out of more
than one table we use JOIN, which we'll cover shortly).
SELECT DISTINCT
It is often the case where we have multiple records that have similar traits.
For instance, in the query below we have selected all the actors from the
actor table, and we have sorted the actors by first_name. (We sorted them
by first_name by simply clicking on "first_name" in the Result Grid in MySQL
Workbench.) See how we have multiple actors named "ADAM", "ALBERT",
"AUDREY", "BEN" and so on?
But let's say, for some reason, we just want to know the number of distinct
unique names in the actor table. We can do that by using the keyword
"DISTINCT". For example:
Here's another example, let's say that you're in HR at a big company with
500 employees and you need to figure out how many days of the year it is
someone's, anyone's birthday so that you can bring in some treats. You
don't need to know who's birthday it is, or how many people have birthday's
on that day. You just need to know what dates you need to bring in treats. In
this case you might run a query that looks like:
The resulting dataset would then give you a list of distinct dates on which
you need to bring in treats :)
LIMIT
The LIMIT keyword is just a nice little utility helper that will help us LIMIT the
number of results we get back. For instance, let's just select 10 customers:
Or, let's take the query we ran in the previous GROUP BY example where we
got the total payments for all customers. But this time, let's limit it to 5
results to just get our 5 best customers so we can send them a thank you
gift over the holidays :)
The query above will fail because the LIMIT clause must be the last
element/clause in the query.
INSERT
When we need to add data into the database, we use INSERT statements.
With an insert statement we specify the table we want to insert data into, the
columns we want to insert data into, and the values we actually want to be
inserted. For instance:
# in SQL, the hashtag or pound sign is the comment symbol, just FYI
In the query above you can see that we are INSERT(ing) INTO the
sakila.actor table. The columns we're inserting into are "first_name" and
"last_name". The values we want inserted into those fields are "Ryan" and
"Desmond".
Here's another INSERT statement, this time for the categories table. In this
case, the categories table only has 3 fields. Two of which are autogenerated.
So all we have to do is INSERT the one non-auto-generated field.
And if we run:
Here's a working example. In this case we're making a full backup of the
actor table.
And now after we refresh the schema navigator on the left, (by clicking the
little refresh, spinning arrows icon) we can see that we do indeed have a new
table named "actor_backup", which we can select all rows from.
We do not need to select * either. We can just select the rows we want. For
instance, take a look at this query:
I'm going to DROP both of those new tables I created to keep the DB nice
and tidy.
and
Now, if I run:
UPDATE `sakila`.`film`
SET rating = "G"
WHERE rating = "PG";
(I'm not actually going to run that query as I don't actually want to make that
update.)
DELETE
When it is time for us to DELETE a record from the one of our
tables, we use the following syntax:
Caution!!
In the table on the left, the "actors" table, we have information solely about
actors. It doesn't contain any information about any of the films they're in.
In the table in the center, the "film_actor" table, we have a simple lookup
table that contains the fields "actor_id" and "film_id". This is because actors
and films have a many-to-many relationship. So we create lookup tables like
this to associate films with their many actors and actors with their many
films. This allows both the "actor" table and the "film" table to stay nice and
pure and clean.
Then, in the table on the right, the "films" table, we have information solely
about films. It doesn't contain any information about any of the actors in
those films. (This is great relational DB design.)
So how do we write a query that will tell us all the films that a specific actor is
in? Or, how do we write a query that will tell us all the actors in a given film?
We use JOIN queries!
There are several types of JOIN queries. The most prominent are the INNER
JOIN (this is the default join when you just use the "JOIN" keyword), the
LEFT/RIGHT JOIN, FULL JOIN (or UNION) and the OUTER JOIN. In the image
below, it is the YELLOW area that will be returned from JOIN queries.
Let's take a look at the INNER JOIN first.
The INNER JOIN selects all records that have matching values in both
tables.
The way this works is we use columns to join on. To join tables there must be
corresponding columns that have corresponding values. For instance, we
can join the actor table to the film_actor table because they both contain the
field "actor_id". Then, we can join the "film_actor" table with the film table
because they both contain the field "film_id". Let's take a look. Let's select
all the films that the actor "PENELOPE GUINESS" is in.
There's a lot going on here, let's see if we can unpack it. (The upcoming
video should also help clear it up.)
So, we want to select the first name and last name from the "actor" table,
and the film name and release year from the "film" table. The only way to do
this is to join through the "film_actor" table. This is because the "actor" table
has no data about films whatsoever, and the "film" table has no data about
actors whatsoever. Fortunately, we have this handy "film_actor" table which
links actor_id's with film_id's.
Using this "lookup table" we can now associate the actor with actor_id = 1 to
the film with film_id = 1. As well as the actor with actor_id = 1 to the film with
film_id = 23, and so on. Using this lookup table we can now link the "actor"
table with the "film" table by linking the actor_id from the "actor" table to the
actor_id in the "film_actor" table, and then we link the film_id from the
"film_actor" table to the film_id in the "film" table.
Two notes, since INNER JOIN is the default join we actually don't need to
write the word "INNER". So this query can be re-written as follows:
Additionally, we can further shorten the query by not typing the table names
over and over and over. Instead, we can create aliases (or nicknames) for
each table and then refer to the tables by those aliases. See below:
A LEFT JOIN will return all rows from the left table (the first one mentioned
in the join clause), even if there's no matching record in the right table.
A RIGHT JOIN will return all rows from the right table (the second one
mentioned in the join clause), even if there's no matching record in the left
table.
An INNER JOIN will only return rows where there's a matching record
in both tables.
By "LEFT" and "RIGHT" it really just means which one you read first or
second. If you want all the records from the table you read first in the query
(whether or not there's a match in the other table), it's a LEFT join. If you
want all the records from the table you read second in the query (whether or
not there are matches in the other table) it's a RIGHT join.
So, let's say we want to get a list of all customers from the customer table,
and we also want to return a list of actors from actor table that share the
same first or last name as the customer. That said, we need the entire list of
customers, whether or not there is an actor with the same first or last name.
In this case, we can use a LEFT JOIN to get all records from the customer
table, as well as any matching records from the actor table. (When there is
no match in the actor table the result will just show NULL for those fields.)
LEFT JOIN
SELECT
c.first_name,
c.last_name,
a.first_name,
a.last_name
FROM customer c
LEFT JOIN actor a
ON c.last_name = a.last_name
ORDER BY c.last_name;
Notice how we read the "customer" table before we read the "actor" table?
Because we're using a LEFT join that means it will select ALL records from
the "customer" table as well as any matching records from the "actor" table.
If there's no match in the actor table it will just show NULL.
RIGHT JOIN
Now, let's take a look at a RIGHT JOIN. To do this, let's simply switch the
word "LEFT" in the query to "RIGHT". The RIGHT JOIN will return all actors
no matter what, as well as any matching records in the customers table.
SELECT
c.first_name,
c.last_name,
a.first_name,
a.last_name
FROM customer c
RIGHT JOIN actor a
ON c.last_name = a.last_name
Notice how we read the "customer" table before we read the "actor" table?
Because we're using a RIGHT join that means it will select ALL records from
the "actor" table as well as any matching records from the "customer" table.
If there's no match in the customer table it will just show NULL.
Now, let's say we ONLY want records from the actor table and customer
table where this is a match. We don't care about records from either table
where there is not a match. In this case, we'd use the default INNER join.
Remember here that we don't actually have to type the word "INNER" as the
INNER JOIN is the default join MySQL uses when you use the keyword
"JOIN" all by itself.
SELECT
c.first_name,
c.last_name,
a.first_name,
a.last_name
FROM customer c
INNER JOIN actor a
ON c.last_name = a.last_name
Quick note, MySQL does not support the FULL OUTER JOIN you see in this
table.
Shout out
to https://www.quackit.com/mysql/examples/mysql_left_join.cfm for helping
me come up with some good LEFT and RIGHT join examples for this page.
UNION
UNION vs. JOIN
So, let's say for some reason we want to know all the names of all customers
and all actors in a single result set. In this case we could run the following
query:
And the result set we get back will be a list of all actors and customers,
without any duplicates.
One add-on to the UNION keyword is "UNION ALL". If you modify the query
to say UNION ALL you will indeed get duplicates in the result set should they
exist.
Notice how there are two additional records returned from this query (799
vs. 797).
SUM / AVG / COUNT
In MySQL, and all other common databases there are a number of helper
functions such as SUM(), COUNT(), AVG().
These functions do exactly as they say. They find the SUM of a given
column, or the COUNT of a number fo records, or the AVERAGE (AVG) of a
given column. So let's take a look.
SUM()
Notice here that the name of the column in the result set is "SUM(amount)".
That's not a great column name. Fortunately, we can rename it very easily
using the "AS" keyword. The "AS" keyword simply allows us to rename the
column in the result set.
Let's run another query just to check the total revenue in the year 2005:
COUNT()
AVG()
So, suppose we want to know the total number of films each actor has been
in. In order to do this, we need to GROUP BY a field such as actor_id. For
instance:
SELECT title
FROM film
WHERE title LIKE "P%"
This query will return the titles of all films that start with "P" and are followed
by any other letter.
Let's take another look at the LIKE keyword with the wildcard "%". In this
example, let's find all film titles that contain the letters "cit" anywhere in the
title.
SELECT title
FROM film
WHERE title LIKE "%cit%"
In this query all films that have titles that have any combination of letters
before "cit" and any combination of letters following "cit" will be returned.
Or, how about this query which returns all titles where the letter "e" appears
at least three times. The title must start with "e" and all following "e's" can
be preceded and followed by any other combination of other letters.
SELECT title
FROM film
WHERE title LIKE "e%e%e";
IN
The IN keyword allows you to specify multiple values in the WHERE clause.
The IN keyword is functionally equivalent to having multiple OR boolean
conditions. In the following query, we're selecting all films that were released
in 2004, 2005, or 2006.
ANY
ALL
The ALL operator returns TRUE if all of the subquery values meet the
condition.
For instance, let's say that we want all of our employees to be able to quickly
pull up the top 10 customers, because all top 10 customers get 10% off all
their rentals. But not all of our employees are so great at joins and aggregate
functions and so forth. In this case, the handy SQL wizard can simply store
the query as a stored procedure in the database, and then all of the
employees can just call that procedure to get the top 10 employees and time
they want without really having to write any SQL. Pretty cool, eh? Totally!
So let's do it! Let's use MySQL Workbench and type in the following SQL:
DELIMITER //
CREATE PROCEDURE GetTop10Customers()
BEGIN
SELECT c.first_name, c.last_name, SUM(p.amount) as total_payments
FROM customer c
JOIN payment p
ON c.customer_id = p.customer_id
GROUP BY c.customer_id
ORDER BY total_payments DESC
limit 10;
END //
DELIMITER ;
Then, refresh the schema with the little refresh icon (the circular spinning
arrows) and expand the Stored Procedures section of the schema.
Then, to invoke the stored procedure you use the following SQL:
CALL `sakila`.`GetTop10Customers`();
# DELIMTER is used to tell mysql that the semi-colon is no longer used to end the q
# this is because the SQL we're executing to create the procedure contains a SQL quer
# a semi-colon - so we need to tell MySQL that, for the moment, the "//" is what will
DELIMITER //
# This is the actual query that we want to store as the procedure itself
# -------------------------------------------------------------------------------
SELECT c.first_name, c.last_name, SUM(p.amount) as total_payments
FROM customer c
JOIN payment p
ON c.customer_id = p.customer_id
GROUP BY c.customer_id
ORDER BY total_payments DESC
limit 10;
# Here is where the actual query that we want to store as the procedure itself en
# -------------------------------------------------------------------------------
# set the delimiter to the semi-colon again (aka what marks the end of the SQL statem
DELIMITER ;
Using MySQL from the CLI
While using MySQL Workbench is definitely a nicer user interface for
interacting with MySQL databases, it is often useful and/or expedient to
connect to MySQL from the CLI. Fortunately, this is simple enough.
Before running the commands below, please ensure that you have started
MySQL.
mysql -u root -p
After entering the (strong) password you created and saved during the
installing you should see an image the resembles the following:
This means you have successfully connected to MySQL and are now with
the "MySQL Shell" - which is basically a unique and specific CLI environment
just for managing your MySQL databases.
Switch to a database.
To delete a db.
To delete a table.
Creating a new user. Login as root. Switch to the MySQL db. Make
the user. Update privs.
# mysql -u root -p
mysql> use mysql;
mysql> INSERT INTO user (Host,User,Password)
VALUES('%','username',PASSWORD('password'));
mysql> flush privileges;
# mysql -u root -p
mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('password');
mysql> flush privileges;
# /etc/init.d/mysql stop
# mysqld_safe --skip-grant-tables
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("newpassword") where
User='root';
mysql> flush privileges;
mysql> quit
# /etc/init.d/mysql stop
# /etc/init.d/mysql start
# mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to Someone@localhost identified by 'passwd';
mysql> flush privileges;
mysql> create table [table name] (personid int(50) not null auto_increment
primary key,firstname varchar(35),middlename
varchar(50),lastnamevarchar(50) default 'somethiing');
Importing & Exporting Schemas and
Data from CLI
MySQL has made it wonderfully easy for us to create backups of our
databases (exporting schemas and data) as well as to restoring databases
from backups (importing schemas and data).
We can do this either from the CLI or from MySQL Workbench. In this
section, we'll cover how to go about it from the CLI.
In the steps below we'll create a backup of the Sakila DB and we'll save it to
our Desktop, then we'll drop (delete) the Sakila DB and verify that it has
indeed been deleted, then we'll restore the Sakila DB from our backup.
1. Let's log in to the mysql shell again - type mysql -u root -p and hit
ENTER
2. enter your root password to login into the MySQL Shell
3. type the MySQL command drop schema sakila; and hit ENTER
4. type the MySQL command show databases; and hit ENTER
5. Verify that the sakila db is NOT present
6. Oh no! What have we done! We deleted the whole database!!! :) (Not to
worry! We made a backup just a second ago!)
1. From within the MySQL shell, let's create an empty schema by typing
the MySQL command create schema sakila; and hit ENTER
2. type the MySQL command exit; and hit ENTER
3. type the command mysql -u root -p sakila <
~/Desktop/sakila_backup.sql and hit ENTER
4. enter your password - wait for the process to complete
1. Let's log in to the mysql shell again - type mysql -u root -p and hit
ENTER
2. Enter your root password to login into the MySQL Shell
3. Type the MySQL command show databases; and hit ENTER
4. Verify that the sakila DB is now present again
5. Type the MySQL command use sakila; and hit ENTER
6. Type the MySQL command select * from actor limit 10; and hit
ENTER
7. TADA!!! WE DID IT!
Importing & Exporting Schemas and
Data from MySQL Workbench
We can also very easily import and export data using MySQL Workbench.
We've actually already done the import when we initially imported the Sakila
DB back in the beginning of this module. That said, let's do the whole
process from scratch.
1. Head back over to the Schemas tab (just beside the "Management"
tab)
2. Right click on the sakila schema and select "Drop Schema"
3. Click "Drop Now"
4. Verify that the sakila DB is gone
5. Oh NO!!! What have we done?!?! :) jk - we're all good!
Notice that we use the letters "asc" for "ascending". Inversely, we use the
letters "desc" for "descending". It's all about keeping the queries as neat
and tidy as possible!
Let's run that same query but ORDER BY the last names in a DESC fashion:
SELECT * from actor
ORDER BY last_name DESC;
Lastly, let's just order the actors by their actor_id's. (It will order by "ASC" by
default.)
AND
This query will only return results from the film table WHERE the length of
the film is less than 50 AND the rating of the film is "G".
OR
Let's say that our kids are a bit old and they can watch both "G" movies as
well as "PG" movies. In this case we can select all films from the film table
that have a rating of "G" OR "PG".
SELECT * FROM sakila.film where rating = "G" OR rating = "PG" OR rating = "PG-13"
NOT
Ok, so let's say our kids are even a bit older now and they can watch just
about any movie so long as it does NOT have the rating of "NC-17". As these
movies are only for the adults. In this case, we can run a query like this:
In this case, we're saying return all rows where the rating is NOT "NC-17".
The use of AND, OR and NOT is very common in day-to-day queries to help
us filter our queries on multiple boolean conditions.
SELECT - WHERE
Now that we understand how to SELECT all data from a given table, it's time
to understand how to filter that data. For instance, let's say that we want to
select all actors from the actors table with the first name "Julia". But, we
ONLY want to select actors with the first name "Julia". In this case, and in so
many others we use the "WHERE" clause. The WHERE keyword is used to
filter results to those who pass a certain boolean condition. For instance:
Or, let's say we only want to retrieve the actor who has the actor_id of 123.
1) Please list out the exact steps to export a database from MySQL using the
CLI.
2) Please list out the exact steps to import a database from MySQL using the
CLI.
3) Please list out the exact steps to export a database from MySQL using
MySQL Workbench.
4) Please list out the exact steps to import a database from MySQL using
MySQL Workbench.
Submission status
Name Description
ABS() Return the absolute value
ACOS() Return the arc cosine
ADDDATE() Add time values (intervals) to a date value
ADDTIME() Add time
AES_DECRYPT() Decrypt using AES
AES_ENCRYPT() Encrypt using AES
AND, && Logical AND
ANY_VALUE() Suppress ONLY_FULL_GROUP_BY value rejection
ASCII() Return numeric value of left-most character
ASIN() Return the arc sine
Assign a value (as part of a SET statement, or as part of the SET clause
=
in an UPDATEstatement)
:= Assign a value
ASYMMETRIC_DECRYPT() Decrypt ciphertext using private or public key
ASYMMETRIC_DERIVE() Derive symmetric key from asymmetric keys
ASYMMETRIC_ENCRYPT() Encrypt cleartext using private or public key
ASYMMETRIC_SIGN() Generate signature from digest
ASYMMETRIC_VERIFY() Verify that signature matches digest
ATAN() Return the arc tangent
ATAN2(), ATAN() Return the arc tangent of the two arguments
AVG() Return the average value of the argument
BENCHMARK() Repeatedly execute an expression
BETWEEN ... AND ... Check whether a value is within a range of values
BIN() Return a string containing binary representation of a number
BIN_TO_UUID() Convert binary UUID to string
BINARY Cast a string to a binary string
BIT_AND() Return bitwise AND
BIT_COUNT() Return the number of bits that are set
BIT_LENGTH() Return length of argument in bits
BIT_OR() Return bitwise OR
BIT_XOR() Return bitwise XOR
& Bitwise AND
~ Bitwise inversion
| Bitwise OR
^ Bitwise XOR
CAN_ACCESS_COLUMN() Internal use only
CAN_ACCESS_DATABASE() Internal use only
CAN_ACCESS_TABLE() Internal use only
CAN_ACCESS_VIEW() Internal use only
CASE Case operator
CAST() Cast a value as a certain type
CEIL() Return the smallest integer value not less than the argument
CEILING() Return the smallest integer value not less than the argument
CHAR() Return the character for each integer passed
CHAR_LENGTH() Return number of characters in argument
CHARACTER_LENGTH() Synonym for CHAR_LENGTH()
CHARSET() Return the character set of the argument
COALESCE() Return the ^rst non-NULL argument
COERCIBILITY() Return the collation coercibility value of the string argument
COLLATION() Return the collation of the string argument
COMPRESS() Return result as a binary string
CONCAT() Return concatenated string
CONCAT_WS() Return concatenate with separator
CONNECTION_ID() Return the connection ID (thread ID) for the connection
CONV() Convert numbers between different number bases
CONVERT() Cast a value as a certain type
CONVERT_TZ() Convert from one time zone to another
COS() Return the cosine
COT() Return the cotangent
COUNT() Return a count of the number of rows returned
COUNT(DISTINCT) Return the count of a number of different values
CRC32() Compute a cyclic redundancy check value
CREATE_ASYMMETRIC_PRIV_KEY() Create private key
CREATE_ASYMMETRIC_PUB_KEY() Create public key
CREATE_DH_PARAMETERS() Generate shared DH secret
CREATE_DIGEST() Generate digest from string
CUME_DIST() Cumulative distribution value
CURDATE() Return the current date
CURRENT_DATE(), CURRENT_DATE Synonyms for CURDATE()
CURRENT_ROLE() Return the current active roles
CURRENT_TIME(), CURRENT_TIME Synonyms for CURTIME()
CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP Synonyms for NOW()
CURRENT_USER(), CURRENT_USER The authenticated user name and host name
CURTIME() Return the current time
DATABASE() Return the default (current) database name
DATE() Extract the date part of a date or datetime expression
DATE_ADD() Add time values (intervals) to a date value
DATE_FORMAT() Format date as speci^ed
DATE_SUB() Subtract a time value (interval) from a date
DATEDIFF() Subtract two dates
DAY() Synonym for DAYOFMONTH()
DAYNAME() Return the name of the weekday
DAYOFMONTH() Return the day of the month (0-31)
DAYOFWEEK() Return the weekday index of the argument
DAYOFYEAR() Return the day of the year (1-366)
DECODE() Decode a string encrypted using ENCODE()
DEFAULT() Return the default value for a table column
DEGREES() Convert radians to degrees
DENSE_RANK() Rank of current row within its partition, without gaps
DES_DECRYPT() Decrypt a string
DES_ENCRYPT() Encrypt a string
DIV Integer division
/ Division operator
ELT() Return string at index number
ENCODE() Encode a string
ENCRYPT() Encrypt a string
= Equal operator
<=> NULL-safe equal to operator
EXP() Raise to the power of
Return a string such that for every bit set in the value bits, you get an
EXPORT_SET()
on string and for every unset bit, you get an off string
EXTRACT() Extract part of a date
ExtractValue() Extract a value from an XML string using XPath notation
FIELD() Index (position) of ^rst argument in subsequent arguments
FIND_IN_SET() Index (position) of ^rst argument within second argument
FIRST_VALUE() Value of argument from ^rst row of window frame
FLOOR() Return the largest integer value not greater than the argument
FORMAT() Return a number formatted to speci^ed number of decimal places
FORMAT_BYTES() Convert byte count to value with units
FORMAT_PICO_TIME() Convert time in picoseconds to value with units
For a SELECT with a LIMIT clause, the number of rows that would be
FOUND_ROWS()
returned were there no LIMIT clause
FROM_BASE64() Decode base64 encoded string and return result
FROM_DAYS() Convert a day number to a date
FROM_UNIXTIME() Format Unix timestamp as a date
GeomCollection() Construct geometry collection from geometries
GeometryCollection() Construct geometry collection from geometries
GET_DD_COLUMN_PRIVILEGES() Internal use only
GET_DD_CREATE_OPTIONS() Internal use only
GET_DD_INDEX_SUB_PART_LENGTH() Internal use only
GET_FORMAT() Return a date format string
GET_LOCK() Get a named lock
> Greater than operator
>= Greater than or equal operator
GREATEST() Return the largest argument
GROUP_CONCAT() Return a concatenated string
GROUPING() Distinguish super-aggregate ROLLUP rows from regular rows
GTID_SUBSET() Return true if all GTIDs in subset are also in set; otherwise false.
GTID_SUBTRACT() Return all GTIDs in set that are not in subset.
HEX() Hexadecimal representation of decimal or string value
HOUR() Extract the hour
ICU_VERSION() ICU library version
IF() If/else construct
IFNULL() Null if/else construct
IN() Check whether a value is within a set of values
INET_ATON() Return the numeric value of an IP address
INET_NTOA() Return the IP address from a numeric value
INET6_ATON() Return the numeric value of an IPv6 address
INET6_NTOA() Return the IPv6 address from a numeric value
Insert substring at speci^ed position up to speci^ed number of
INSERT()
characters
INSTR() Return the index of the ^rst occurrence of substring
INTERNAL_AUTO_INCREMENT() Internal use only
INTERNAL_AVG_ROW_LENGTH() Internal use only
INTERNAL_CHECK_TIME() Internal use only
INTERNAL_CHECKSUM() Internal use only
INTERNAL_DATA_FREE() Internal use only
INTERNAL_DATA_LENGTH() Internal use only
INTERNAL_DD_CHAR_LENGTH() Internal use only
INTERNAL_GET_COMMENT_OR_ERROR() Internal use only
INTERNAL_GET_VIEW_WARNING_OR_ERROR() Internal use only
INTERNAL_INDEX_COLUMN_CARDINALITY() Internal use only
INTERNAL_INDEX_LENGTH() Internal use only
INTERNAL_KEYS_DISABLED() Internal use only
INTERNAL_MAX_DATA_LENGTH() Internal use only
INTERNAL_TABLE_ROWS() Internal use only
INTERNAL_UPDATE_TIME() Internal use only
INTERVAL() Return the index of the argument that is less than the ^rst argument
IS Test a value against a boolean
IS_FREE_LOCK() Whether the named lock is free
IS_IPV4() Whether argument is an IPv4 address
IS_IPV4_COMPAT() Whether argument is an IPv4-compatible address
IS_IPV4_MAPPED() Whether argument is an IPv4-mapped address
IS_IPV6() Whether argument is an IPv6 address
IS NOT Test a value against a boolean
IS NOT NULL NOT NULL value test
IS NULL NULL value test
IS_USED_LOCK() Whether the named lock is in use; return connection identi^er if true
IS_UUID() Whether argument is a valid UUID
ISNULL() Test whether the argument is NULL
JSON_ARRAY() Create JSON array
JSON_ARRAY_APPEND() Append data to JSON document
JSON_ARRAY_INSERT() Insert into JSON array
JSON_ARRAYAGG() Return result set as a single JSON array
Return value from JSON column after evaluating path; equivalent to
->
JSON_EXTRACT().
JSON_CONTAINS() Whether JSON document contains speci^c object at path
JSON_CONTAINS_PATH() Whether JSON document contains any data at path
JSON_DEPTH() Maximum depth of JSON document
JSON_EXTRACT() Return data from JSON document
Return value from JSON column after evaluating path and unquoting
->>
the result; equivalent to JSON_UNQUOTE(JSON_EXTRACT()).
JSON_INSERT() Insert data into JSON document
JSON_KEYS() Array of keys from JSON document
JSON_LENGTH() Number of elements in JSON document
Merge JSON documents, preserving duplicate keys. Deprecated
JSON_MERGE() (deprecated 8.0.3)
synonym for JSON_MERGE_PRESERVE()
JSON_MERGE_PATCH() Merge JSON documents, replacing values of duplicate keys
JSON_MERGE_PRESERVE() Merge JSON documents, preserving duplicate keys
JSON_OBJECT() Create JSON object
JSON_OBJECTAGG() Return result set as a single JSON object
JSON_PRETTY() Print a JSON document in human-readable format
JSON_QUOTE() Quote JSON document
JSON_REMOVE() Remove data from JSON document
JSON_REPLACE() Replace values in JSON document
JSON_SEARCH() Path to value within JSON document
JSON_SET() Insert data into JSON document
Freed space within binary representation of a JSON column value
JSON_STORAGE_FREE()
following a partial update
JSON_STORAGE_SIZE() Space used for storage of binary representation of a JSON document
JSON_TABLE() Return data from a JSON expression as a relational table
JSON_TYPE() Type of JSON value
JSON_UNQUOTE() Unquote JSON value
JSON_VALID() Whether JSON value is valid
LAG() Value of argument from row lagging current row within partition
LAST_DAY Return the last day of the month for the argument
LAST_INSERT_ID() Value of the AUTOINCREMENT column for the last INSERT
LAST_VALUE() Value of argument from last row of window frame
LCASE() Synonym for LOWER()
LEAD() Value of argument from row leading current row within partition
LEAST() Return the smallest argument
LEFT() Return the leftmost number of characters as speci^ed
<< Left shift
LENGTH() Return the length of a string in bytes
< Less than operator
<= Less than or equal operator
LIKE Simple pattern matching
LineString() Construct LineString from Point values
LN() Return the natural logarithm of the argument
LOAD_FILE() Load the named ^le
LOCALTIME(), LOCALTIME Synonym for NOW()
LOCALTIMESTAMP, LOCALTIMESTAMP() Synonym for NOW()
LOCATE() Return the position of the ^rst occurrence of substring
LOG() Return the natural logarithm of the ^rst argument
LOG10() Return the base-10 logarithm of the argument
LOG2() Return the base-2 logarithm of the argument
LOWER() Return the argument in lowercase
LPAD() Return the string argument, left-padded with the speci^ed string
LTRIM() Remove leading spaces
Return a set of comma-separated strings that have the corresponding
MAKE_SET()
bit in bits set
MAKEDATE() Create a date from the year and day of year
MAKETIME() Create time from hour, minute, second
Block until the slave has read and applied all updates up to the
MASTER_POS_WAIT()
speci^ed position
MATCH Perform full-text search
MAX() Return the maximum value
MBRContains() Whether MBR of one geometry contains MBR of another
MBRCoveredBy() Whether one MBR is covered by another
MBRCovers() Whether one MBR covers another
MBRDisjoint() Whether MBRs of two geometries are disjoint
MBREquals() Whether MBRs of two geometries are equal
MBRIntersects() Whether MBRs of two geometries intersect
MBROverlaps() Whether MBRs of two geometries overlap
MBRTouches() Whether MBRs of two geometries touch
MBRWithin() Whether MBR of one geometry is within MBR of another
MD5() Calculate MD5 checksum
MICROSECOND() Return the microseconds from argument
MID() Return a substring starting from the speci^ed position
MIN() Return the minimum value
- Minus operator
MINUTE() Return the minute from the argument
MOD() Return the remainder
%, MOD Modulo operator
MONTH() Return the month from the date passed
MONTHNAME() Return the name of the month
MultiLineString() Contruct MultiLineString from LineString values
MultiPoint() Construct MultiPoint from Point values
MultiPolygon() Construct MultiPolygon from Polygon values
NAME_CONST() Cause the column to have the given name
NOT, ! Negates value
NOT BETWEEN ... AND ... Check whether a value is not within a range of values
!=, <> Not equal operator
NOT IN() Check whether a value is not within a set of values
NOT LIKE Negation of simple pattern matching
NOT REGEXP Negation of REGEXP
NOW() Return the current date and time
NTH_VALUE() Value of argument from N-th row of window frame
NTILE() Bucket number of current row within its partition.
NULLIF() Return NULL if expr1 = expr2
OCT() Return a string containing octal representation of a number
OCTET_LENGTH() Synonym for LENGTH()
||, OR Logical OR
ORD() Return character code for leftmost character of the argument
PASSWORD() Calculate and return a password string
PERCENT_RANK() Percentage rank value
PERIOD_ADD() Add a period to a year-month
PERIOD_DIFF() Return the number of months between periods
PI() Return the value of pi
+ Addition operator
Point() Construct Point from coordinates
Polygon() Construct Polygon from LineString arguments
POSITION() Synonym for LOCATE()
POW() Return the argument raised to the speci^ed power
POWER() Return the argument raised to the speci^ed power
PS_CURRENT_THREAD_ID() Performance Schema thread ID for current thread
PS_THREAD_ID() Performance Schema thread ID for given thread
QUARTER() Return the quarter from a date argument
QUOTE() Escape the argument for use in an SQL statement
RADIANS() Return argument converted to radians
RAND() Return a random joating-point value
RANDOM_BYTES() Return a random byte vector
RANK() Rank of current row within its partition, with gaps
REGEXP Whether string matches regular expression
REGEXP_INSTR() Starting index of substring matching regular expression
REGEXP_LIKE() Whether string matches regular expression
REGEXP_REPLACE() Replace substrings matching regular expression
REGEXP_SUBSTR() Return substring matching regular expression
RELEASE_ALL_LOCKS() Release all current named locks
RELEASE_LOCK() Release the named lock
REPEAT() Repeat a string the speci^ed number of times
REPLACE() Replace occurrences of a speci^ed string
REVERSE() Reverse the characters in a string
RIGHT() Return the speci^ed rightmost number of characters
>> Right shift
RLIKE Whether string matches regular expression
ROLES_GRAPHML() Return a GraphML document representing memory role subgraphs
ROUND() Round the argument
ROW_COUNT() The number of rows updated
ROW_NUMBER() Number of current row within its partition
RPAD() Append string the speci^ed number of times
RTRIM() Remove trailing spaces
SCHEMA() Synonym for DATABASE()
SEC_TO_TIME() Converts seconds to 'HH:MM:SS' format
SECOND() Return the second (0-59)
SESSION_USER() Synonym for USER()
SHA1(), SHA() Calculate an SHA-1 160-bit checksum
SHA2() Calculate an SHA-2 checksum
SIGN() Return the sign of the argument
SIN() Return the sine of the argument
SLEEP() Sleep for a number of seconds
SOUNDEX() Return a soundex string
SOUNDS LIKE Compare sounds
SPACE() Return a string of the speci^ed number of spaces
SQRT() Return the square root of the argument
ST_Area() Return Polygon or MultiPolygon area
ST_AsBinary(), ST_AsWKB() Convert from internal geometry format to WKB
ST_AsGeoJSON() Generate GeoJSON object from geometry
ST_AsText(), ST_AsWKT() Convert from internal geometry format to WKT
ST_Buffer() Return geometry of points within given distance from geometry
ST_Buffer_Strategy() Produce strategy option for ST_Buffer()
ST_Centroid() Return centroid as a point
ST_Contains() Whether one geometry contains another
ST_ConvexHull() Return convex hull of geometry
ST_Crosses() Whether one geometry crosses another
ST_Difference() Return point set difference of two geometries
ST_Dimension() Dimension of geometry
ST_Disjoint() Whether one geometry is disjoint from another
ST_Distance() The distance of one geometry from another
ST_Distance_Sphere() Minimum distance on earth between two geometries
ST_EndPoint() End Point of LineString
ST_Envelope() Return MBR of geometry
ST_Equals() Whether one geometry is equal to another
ST_ExteriorRing() Return exterior ring of Polygon
ST_GeoHash() Produce a geohash value
ST_GeomCollFromText(), ST_GeometryCollecti
Return geometry collection from WKT
onFromText(), ST_GeomCollFromTxt()
ST_GeomCollFromWKB(), ST_GeometryCollectio
Return geometry collection from WKB
nFromWKB()
ST_GeometryN() Return N-th geometry from geometry collection
ST_GeometryType() Return name of geometry type
ST_GeomFromGeoJSON() Generate geometry from GeoJSON object
ST_GeomFromText(), ST_GeometryFromText() Return geometry from WKT
ST_GeomFromWKB(), ST_GeometryFromWKB() Return geometry from WKB
ST_InteriorRingN() Return N-th interior ring of Polygon
ST_Intersection() Return point set intersection of two geometries
ST_Intersects() Whether one geometry intersects another
ST_IsClosed() Whether a geometry is closed and simple
ST_IsEmpty() Placeholder function
ST_IsSimple() Whether a geometry is simple
ST_IsValid() Whether a geometry is valid
ST_LatFromGeoHash() Return latitude from geohash value
ST_Latitude() Return latitude of Point
ST_Length() Return length of LineString
ST_LineFromText(), ST_LineStringFromText() Construct LineString from WKT
ST_MLineFromWKB(), ST_MultiLineStringFromW
Construct MultiLineString from WKB
KB()
ST_MPointFromText(), ST_MultiPointFromText
Construct MultiPoint from WKT
()
ST_MPolyFromText(), ST_MultiPolygonFromTex
Construct MultiPolygon from WKT
t()
ST_MPolyFromWKB(), ST_MultiPolygonFromWKB(
Construct MultiPolygon from WKB
)
USER() The user name and host name provided by the client
UTC_DATE() Return the current UTC date
UTC_TIME() Return the current UTC time
UTC_TIMESTAMP() Return the current UTC date and time
UUID() Return a Universal Unique Identi^er (UUID)
UUID_SHORT() Return an integer-valued universal identi^er
UUID_TO_BIN() Convert string UUID to binary
VALIDATE_PASSWORD_STRENGTH() Determine strength of password
VALUES() De^ne the values to be used during an INSERT
VAR_POP() Return the population standard variance
VAR_SAMP() Return the sample variance
VARIANCE() Return the population standard variance
VERSION() Return a string that indicates the MySQL server version
WAIT_FOR_EXECUTED_GTID_SET() Wait until the given GTIDs have executed on slave.
WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS() Wait until the given GTIDs have executed on slave.
WEEK() Return the week number
WEEKDAY() Return the weekday index
WEEKOFYEAR() Return the calendar week of the date (1-53)
WEIGHT_STRING() Return the weight string for a string
XOR Logical XOR
YEAR() Return the year
YEARWEEK() Return the year and week
See later in this section for information about the character set of implicit
number-to-string conversions, and for modified rules that apply to CREATE
TABLE ... SELECT statements.
If one or both arguments are NULL, the result of the comparison is NULL,
except for the NULL-safe <=> equality comparison operator. For NULL <=>
NULL, the result is true. No conversion is needed.
The descriptions for those operators later in this section detail how they
work with row operands. For additional examples of row comparisons in the
context of row subqueries, see Section 13.2.11.5, “Row Subqueries”.
Note
Beginning with MySQL 8.0.4, the server no longer attempts to infer context
in this fashion. Instead, the function is executed using the arguments as
provided, performing data type conversions to one or more of the arguments
if and only if they are not all of the same type. Any type coercion mandated
by an expression that makes use of the return value is now performed
following function execution. This means that, in MySQl 8.0.4 and
later, LEAST("11", "45", "2") + 0 evaluates to "11" + 0 and thus to integer
11. (Bug #83895, Bug #25123839)
To convert a value to a specific type for comparison purposes, you can use
the CAST() function. String values can be converted to a different character
set using CONVERT(). See Section 12.10, “Cast Functions and Operators”.
By default, string comparisons are not case-sensitive and use the current
character set. The default is utf8mb4.
Equal:
mysql> SELECT 1 = 0;
-> 0
mysql> SELECT '0' = 0;
-> 1
mysql> SELECT '0.0' = 0;
-> 1
mysql> SELECT '0.01' = 0;
-> 0
mysql> SELECT '.01' = 0.01;
-> 1
(a = x) AND (b = y)
<=>
<>, !=
Not equal:
For row comparisons, (a, b) <> (x, y) and (a, b) != (x, y) are
equivalent to:
(a <> x) OR (b <> y)
<=
<
Less than:
>=
>
Greater than:
IS NOT boolean_value
IS NULL
To work well with ODBC programs, MySQL supports the following extra
features when using IS NULL:
For DATE and DATETIME columns that are declared as NOT NULL, you
can find the special date '0000-00-00' by using a statement like
this:
IS NOT NULL
If expr is greater than or equal to min and expr is less than or equal
to max, BETWEEN returns 1, otherwise it returns 0. This is equivalent to the
expression (min <= expr AND expr <= max) if all the arguments are of
the same type. Otherwise type conversion takes place according to the
rules described in Section 12.2, “Type Conversion in Expression
Evaluation”, but applied to all the three arguments.
mysql> SELECT 2 BETWEEN 1 AND 3, 2 BETWEEN 3 and 1;
-> 1, 0
mysql> SELECT 1 BETWEEN 2 AND 3;
-> 0
mysql> SELECT 'b' BETWEEN 'a' AND 'c';
-> 1
mysql> SELECT 2 BETWEEN 2 AND '3';
-> 1
mysql> SELECT 2 BETWEEN 2 AND 'x-3';
-> 0
For best results when using BETWEEN with date or time values,
use CAST() to explicitly convert the values to the desired data type.
Examples: If you compare a DATETIME to two DATE values, convert
the DATE values to DATETIME values. If you use a string constant such
as '2001-1-1' in a comparison to a DATE, cast the string to a DATE.
COALESCE(value,...)
Returns the first non-NULL value in the list, or NULL if there are no non-
NULL values.
expr IN (value,...)
You should never mix quoted and unquoted values in an IN list because
the comparison rules for quoted values (such as strings) and unquoted
values (such as numbers) differ. Mixing types may therefore lead to
inconsistent results. For example, do not write an IN expression like this:
To comply with the SQL standard, IN returns NULL not only if the
expression on the left hand side is NULL, but also if no match is found in
the list and one of the expressions in the list is NULL.
INTERVAL(N,N1,N2,N3,...)
LEAST(value1,value2,...)
MySQL evaluates any nonzero, non-NULL value to TRUE. For example, the
following statements all assess to TRUE:
-> 1
mysql> SELECT -10 IS TRUE;
-> 1
mysql> SELECT 'string' IS NOT NULL;
-> 1
NOT, !
AND, &&
Logical AND. Evaluates to 1 if all operands are nonzero and not NULL, to 0 if
one or more operands are 0, otherwise NULL is returned.
OR, ||
Logical OR. When both operands are non-NULL, the result is 1 if any
operand is nonzero, and 0 otherwise. With a NULL operand, the result
is 1if the other operand is nonzero, and NULL otherwise. If both operands
are NULL, the result is NULL.
mysql> SELECT 1 OR 1;
-> 1
mysql> SELECT 1 OR 0;
-> 1
mysql> SELECT 0 OR 0;
-> 0
mysql> SELECT 0 OR NULL;
-> NULL
mysql> SELECT 1 OR NULL;
-> 1
XOR
Note
The syntax of the CASE expr described here differs slightly from that of
the SQL CASE statement described in Section 13.6.5.1, “CASE Syntax”,
for use inside stored programs. The CASE statement cannot have
an ELSE NULL clause, and it is terminated with END CASE instead of END.
The return type of a CASE expression result is the aggregated type of all
result values:
If all types are BIT, the result is BIT. Otherwise, BIT arguments are
treated similar to BIGINT.
If all types are YEAR, the result is YEAR. Otherwise, YEAR arguments
are treated similar to INT.
SET and ENUM are treated similar to VARCHAR; the result is VARCHAR.
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL), IF() returns expr2.
Otherwise, it returns expr3.
Note
The default return type of IF() (which may matter when it is stored into
a temporary table) is calculated as follows:
IFNULL(expr1,expr2)
NULLIF(expr1,expr2)
The return value has the same type as the first argument.
Dashboard / My courses / SQL & Databases / Sections / 7) Extended MySQL Functions Reference
/ String Functions Reference
Name Description
ASCII() Return numeric value of left-most character
BIN() Return a string containing binary representation of a number
BIT_LENGTH() Return length of argument in bits
CHAR() Return the character for each integer passed
CHAR_LENGTH() Return number of characters in argument
CHARACTER_LENGTH() Synonym for CHAR_LENGTH()
CONCAT() Return concatenated string
CONCAT_WS() Return concatenate with separator
ELT() Return string at index number
Return a string such that for every bit set in the value bits, you get an
EXPORT_SET()
on string and for every unset bit, you get an off string
FIELD() Index (position) of Urst argument in subsequent arguments
FIND_IN_SET() Index (position) of Urst argument within second argument
FORMAT() Return a number formatted to speciUed number of decimal places
FROM_BASE64() Decode base64 encoded string and return result
HEX() Hexadecimal representation of decimal or string value
Insert substring at speciUed position up to speciUed number of
INSERT()
characters
INSTR() Return the index of the Urst occurrence of substring
LCASE() Synonym for LOWER()
LEFT() Return the leftmost number of characters as speciUed
LENGTH() Return the length of a string in bytes
LIKE Simple pattern matching
LOAD_FILE() Load the named Ule
LOCATE() Return the position of the Urst occurrence of substring
LOWER() Return the argument in lowercase
LPAD() Return the string argument, left-padded with the speciUed string
LTRIM() Remove leading spaces
Return a set of comma-separated strings that have the corresponding
MAKE_SET()
bit in bits set
MATCH Perform full-text search
MID() Return a substring starting from the speciUed position
NOT LIKE Negation of simple pattern matching
NOT REGEXP Negation of REGEXP
OCT() Return a string containing octal representation of a number
String-valued functions return NULL if the length of the result would be greater than the value of
the max_allowed_packet system variable. See Section 5.1.1, “ConUguring the Server”.
For functions that operate on string positions, the Urst position is numbered 1.
For functions that take length arguments, noninteger arguments are rounded to the nearest integer.
ASCII(str)
Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the empty string.
Returns NULL if str is NULL .ASCII() works for 8-bit characters.
BIN(N)
Returns a string representation of the binary value of N, where N is a longlong (BIGINT) number. This is equivalent
to CONV(N,10,2). Returns NULL if N is NULL .
BIT_LENGTH(str)
CHAR() interprets each argument N as an integer and returns a string consisting of the characters given by the
code values of those integers. NULL values are skipped.
CHAR() arguments larger than 255 are converted into multiple result bytes. For example, CHAR(256) is equivalent
to CHAR(1,0), and CHAR(256*256) is equivalent to CHAR(1,0,0):
By default, CHAR() returns a binary string. To produce a string in a given character set, use the
optional USING clause:
If USING is given and the result string is illegal for the given character set, a warning is issued. Also, if strict SQL
mode is enabled, the result from CHAR() becomes NULL .
CHAR_LENGTH(str)
Returns the length of the string str, measured in characters. A multibyte character counts as a single character.
This means that for a string containing Uve 2-byte characters, LENGTH() returns 10 ,
whereas CHAR_LENGTH() returns 5 .
CHARACTER_LENGTH(str)
CONCAT(str1,str2,...)
Returns the string that results from concatenating the arguments. May have one or more arguments. If all
arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the
result is a binary string. A numeric argument is converted to its equivalent nonbinary string form.
For quoted strings, concatenation can be performed by placing the strings next to each other:
CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special form of CONCAT(). The Urst argument is the
separator for the rest of the arguments. The separator is added between the strings to be concatenated. The
separator can be a string, as can the rest of the arguments. If the separator is NULL , the result is NULL .
CONCAT_WS() does not skip empty strings. However, it does skip any NULL values after the separator argument.
ELT(N,str1,str2,str3,...)
ELT() returns the Nth element of the list of strings: str1 if N = 1 , str2 if N = 2 , and so on. Returns NULL if N is less
than 1 or greater than the number of arguments. ELT() is the complement of FIELD().
EXPORT_SET(bits,on,off[,separator[,number_of_bits]])
Returns a string such that for every bit set in the value bits, you get an on string and for every bit not set in the
value, you get an off string. Bits in bits are examined from right to left (from low-order to high-order bits). Strings
are added to the result from left to right, separated by the separator string (the default being the comma
character , ). The number of bits examined is given by number_of_bits, which has a default of 64 if not
speciUed. number_of_bits is silently clipped to 64 if larger than 64. It is treated as an unsigned integer, so a value
of −1 is effectively the same as 64.
FIELD(str,str1,str2,str3,...)
Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found.
If all arguments to FIELD() are strings, all arguments are compared as strings. If all arguments are numbers, they
are compared as numbers. Otherwise, the arguments are compared as double.
If str is NULL , the return value is 0 because NULL fails equality comparison with any value. FIELD() is the
complement of ELT().
mysql> SELECT FIELD('Bb', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff');
-> 2
mysql> SELECT FIELD('Gg', 'Aa', 'Bb', 'Cc', 'Dd', 'Ff');
-> 0
FIND_IN_SET(str,strlist)
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string
list is a string composed of substrings separated by , characters. If the Urst argument is a constant string and the
second is a column of type SET, the FIND_IN_SET()function is optimized to use bit arithmetic. Returns 0 if str is
not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL . This function does not
work properly if the Urst argument contains a comma ( , ) character.
FORMAT(X,D[,locale])
Formats the number X to a format like '#,###,###.##' , rounded to D decimal places, and returns the result as a
string. If D is 0 , the result has no decimal point or fractional part.
The optional third parameter enables a locale to be speciUed to be used for the result number's decimal point,
thousands separator, and grouping between separators. Permissible locale values are the same as the legal
values for the lc_time_names system variable (see Section 10.15, “MySQL Server Locale Support”). If no locale is
speciUed, the default is 'en_US' .
FROM_BASE64(str)
Takes a string encoded with the base-64 encoded rules used by TO_BASE64() and returns the decoded result as a
binary string. The result is NULL if the argument is NULL or not a valid base-64 string. See the description
of TO_BASE64() for details about the encoding and decoding rules.
HEX(str), HEX(N)
For a string argument str, HEX() returns a hexadecimal string representation of str where each byte of each
character in str is converted to two hexadecimal digits. (Multibyte characters therefore become more than two
character in str is converted to two hexadecimal digits. (Multibyte characters therefore become more than two
digits.) The inverse of this operation is performed by theUNHEX() function.
For a numeric argument N, HEX() returns a hexadecimal string representation of the value of N treated as a
longlong (BIGINT) number. This is equivalent to CONV(N,10,16). The inverse of this operation is performed
by CONV(HEX(N),16,10).
INSERT(str,pos,len,newstr)
Returns the string str, with the substring beginning at position pos and len characters long replaced by the
string newstr. Returns the original string if pos is not within the length of the string. Replaces the rest of the string
from position pos if len is not within the length of the rest of the string. Returns NULL if any argument is NULL .
INSTR(str,substr)
Returns the position of the Urst occurrence of substring substr in string str. This is the same as the two-argument
form of LOCATE(), except that the order of the arguments is reversed.
This function is multibyte safe, and is case-sensitive only if at least one argument is a binary string.
LCASE(str)
LCASE() used in a view is rewritten as LOWER() when storing the view's deUnition. (Bug #12844279)
LEFT(str,len)
Returns the leftmost len characters from the string str, or NULL if any argument is NULL .
mysql> SELECT LEFT('foobarbar', 5);
-> 'fooba'
LENGTH(str)
Returns the length of the string str, measured in bytes. A multibyte character counts as multiple bytes. This
means that for a string containing Uve 2-byte characters, LENGTH() returns 10 , whereas CHAR_LENGTH() returns 5 .
Note
The Length() OpenGIS spatial function is named ST_Length() in MySQL.
LOAD_FILE(file_name)
Reads the Ule and returns the Ule contents as a string. To use this function, the Ule must be located on the server
host, you must specify the full path name to the Ule, and you must have the FILE privilege. The Ule must be
readable by the server and its size less thanmax_allowed_packet bytes. If the secure_file_priv system variable is
set to a nonempty directory name, the Ule to be loaded must be located in that directory. (Prior to MySQL 8.0.17,
the Ule must be readable by all, not just readable by the server.)
If the Ule does not exist or cannot be read because one of the preceding conditions is not satisUed, the function
returns NULL .
The character_set_filesystem system variable controls interpretation of Ule names that are given as literal
strings.
mysql> UPDATE t
SET blob_col=LOAD_FILE('/tmp/picture')
WHERE id=1;
LOCATE(substr,str), LOCATE(substr,str,pos)
The Urst syntax returns the position of the Urst occurrence of substring substr in string str. The second syntax
returns the position of the Urst occurrence of substring substr in string str, starting at position pos.
Returns 0 if substr is not in str. Returns NULL if any argument is NULL .
This function is multibyte safe, and is case-sensitive only if at least one argument is a binary string.
LOWER(str)
Returns the string str with all characters changed to lowercase according to the current character set mapping.
The default is utf8mb4 .
LOWER() (and UPPER()) are ineffective when applied to binary strings (BINARY, VARBINARY, BLOB). To perform
lettercase conversion, convert the string to a nonbinary string:
For collations of Unicode character sets, LOWER() and UPPER() work according to the Unicode Collation Algorithm
(UCA) version in the collation name, if there is one, and UCA 4.0.0 if no version is speciUed. For
example, utf8mb4_0900_ai_ci and utf8_unicode_520_ci work according to UCA 9.0.0 and 5.2.0, respectively,
whereas utf8_unicode_ci works according to UCA 4.0.0. See Section 10.10.1, “Unicode Character Sets”.
LPAD(str,len,padstr)
Returns the string str, left-padded with the string padstr to a length of len characters. If str is longer than len, the
return value is shortened to len characters.
LTRIM(str)
MAKE_SET(bits,str1,str2,...)
Returns a set value (a string containing substrings separated by , characters) consisting of the strings that have
the corresponding bit in bitsset. str1 corresponds to bit 0, str2 to bit 1, and so on. NULL values
in str1, str2, ... are not appended to the result.
mysql> SELECT MAKE_SET(1,'a','b','c');
-> 'a'
mysql> SELECT MAKE_SET(1 | 4,'hello','nice','world');
-> 'hello,world'
mysql> SELECT MAKE_SET(1 | 4,'hello','nice',NULL,'world');
-> 'hello'
mysql> SELECT MAKE_SET(0,'a','b','c');
-> ''
MID(str,pos,len)
OCT(N)
Returns a string representation of the octal value of N, where N is a longlong (BIGINT) number. This is equivalent
to CONV(N,10,8). Returns NULL if N is NULL .
OCTET_LENGTH(str)
ORD(str)
If the leftmost character of the string str is a multibyte character, returns the code for that character, calculated
from the numeric values of its constituent bytes using this formula:
If the leftmost character is not a multibyte character, ORD() returns the same value as the ASCII() function.
POSITION(substr IN str)
QUOTE(str)
Quotes a string to produce a result that can be used as a properly escaped data value in an SQL statement. The
string is returned enclosed by single quotation marks and with each instance of backslash ( \ ), single quote ( ' ),
ASCII NUL , and Control+Z preceded by a backslash. If the argument is NULL , the return value is the
word “NULL” without enclosing single quotation marks.
For comparison, see the quoting rules for literal strings and within the C API in Section 9.1.1, “String Literals”,
and Section 28.7.7.56, “mysql_real_escape_string_quote()”.
REPEAT(str,count)
Returns a string consisting of the string str repeated count times. If count is less than 1, returns an empty string.
Returns NULL if str orcount are NULL .
REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str replaced by the
string to_str. REPLACE() performs a case-sensitive match when searching for from_str.
REVERSE(str)
Returns the string str with the order of the characters reversed.
RIGHT(str,len)
Returns the rightmost len characters from the string str, or NULL if any argument is NULL .
RPAD(str,len,padstr)
Returns the string str, right-padded with the string padstr to a length of len characters. If str is longer than len,
the return value is shortened to len characters.
RTRIM(str)
SOUNDEX(str)
Returns a soundex string from str. Two strings that sound almost the same should have identical soundex
strings. A standard soundex string is four characters long, but the SOUNDEX() function returns an arbitrarily long
string. You can use SUBSTRING() on the result to get a standard soundex string. All nonalphabetic characters
in str are ignored. All international alphabetic characters outside the A-Z range are treated as vowels.
Important
When using SOUNDEX(), you should be aware of the following limitations:
This function, as currently implemented, is intended to work well with strings that are in the English language
only. Strings in other languages may not produce reliable results.
This function is not guaranteed to provide consistent results with strings that use multibyte character sets,
including utf-8 . See Bug #22638 for more information.
Note
This function implements the original Soundex algorithm, not the more popular enhanced version (also described
by D. Knuth). The difference is that original version discards vowels Urst and duplicates second, whereas the
enhanced version discards duplicates Urst and vowels second.
SPACE(N)
SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len)
The forms without a len argument return a substring from string str starting at position pos. The forms with
a len argument return a substring len characters long from string str, starting at position pos. The forms that
use FROM are standard SQL syntax. It is also possible to use a negative value for pos. In this case, the beginning of
the substring is pos characters from the end of the string, rather than the beginning. A negative value may be used
for pos in any of the forms of this function.
For all forms of SUBSTRING(), the position of the Urst character in the string from which the substring is to be
extracted is reckoned as 1 .
SUBSTRING_INDEX(str,delim,count)
Returns the substring from string str before count occurrences of the delimiter delim. If count is positive,
everything to the left of the Unal delimiter (counting from the left) is returned. If count is negative, everything to the
right of the Unal delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive
match when searching for delim.
TO_BASE64(str)
Converts the string argument to base-64 encoded form and returns the result as a character string with the
connection character set and collation. If the argument is not a string, it is converted to a string before conversion
takes place. The result is NULL if the argument is NULL . Base-64 encoded strings can be decoded using
takes place. The result is NULL if the argument is NULL . Base-64 encoded strings can be decoded using
the FROM_BASE64() function.
Different base-64 encoding schemes exist. These are the encoding and decoding rules used
by TO_BASE64() and FROM_BASE64():
Encoded output consists of groups of 4 printable characters. Each 3 bytes of the input data are encoded using
4 characters. If the last group is incomplete, it is padded with '=' characters to a length of 4.
A newline is added after each 76 characters of encoded output to divide long output into multiple lines.
Decoding recognizes and ignores newline, carriage return, tab, and space.
Returns the string str with all remstr preUxes or supxes removed. If none of the speciUers BOTH , LEADING ,
or TRAILING is given, BOTH is assumed. remstr is optional and, if not speciUed, spaces are removed.
UCASE(str)
UNHEX(str)
For a string argument str, UNHEX(str) interprets each pair of characters in the argument as a hexadecimal number
and converts it to the byte represented by the number. The return value is a binary string.
The characters in the argument string must be legal hexadecimal digits: '0' .. '9' , 'A' .. 'F' , 'a' .. 'f' . If the
argument contains any nonhexadecimal digits, the result is NULL :
A NULL result can occur if the argument to UNHEX() is a BINARY column, because values are padded with 0x00
bytes when stored but those bytes are not stripped on retrieval. For example, '41' is stored into
a CHAR(3) column as '41 ' and retrieved as '41' (with the trailing pad space stripped), so UNHEX() for the
column value returns 'A' . By contrast '41' is stored into a BINARY(3) column as '41\0' and retrieved
as '41\0' (with the trailing pad 0x00 byte not stripped). '\0' is not a legal hexadecimal digit, so UNHEX() for the
column value returns NULL .
For a numeric argument N, the inverse of HEX(N) is not performed by UNHEX(). Use CONV(HEX(N),16,10) instead.
See the description ofHEX().
UPPER(str)
Returns the string str with all characters changed to uppercase according to the current character set mapping.
The default is utf8mb4 .
See the description of LOWER() for information that also applies to UPPER(). This included information about how
to perform lettercase conversion of binary strings (BINARY, VARBINARY, BLOB) for which these functions are
ineffective, and information about case folding for Unicode character sets.
This function returns the weight string for the input string. The return value is a binary string that represents the
comparison and sorting value of the string. It has these properties:
If WEIGHT_STRING(str1) = WEIGHT_STRING(str2), then str1 = str2 (str1 and str2 are considered equal)
If WEIGHT_STRING(str1) < WEIGHT_STRING(str2), then str1 < str2 (str1 sorts before str2)
WEIGHT_STRING() is a debugging function intended for internal use. Its behavior can change without notice
between MySQL versions. It can be used for testing and debugging of collations, especially if you are adding a new
collation. See Section 10.13, “Adding a Collation to a Character Set”.
This list briery summarizes the arguments. More details are given in the discussion following the list.
AS clause: Optional; cast the input string to a given type and length.
The input string, str, is a string expression. If the input is a nonbinary (character) string such as a CHAR, VARCHAR,
or TEXT value, the return value contains the collation weights for the string. If the input is a binary (byte) string such
as a BINARY, VARBINARY, or BLOB value, the return value is the same as the input (the weight for each byte in a binary
string is the byte value). If the input is NULL , WEIGHT_STRING() returns NULL .
Examples:
The preceding examples use HEX() to display the WEIGHT_STRING() result. Because the result is a binary
value, HEX() can be especially useful when the result contains nonprinting values, to display it in printable form:
For non- NULL return values, the data type of the value is VARBINARY if its length is within the maximum length
for VARBINARY, otherwise the data type is BLOB.
The AS clause may be given to cast the input string to a nonbinary or binary string and to force it to a given length:
AS CHAR(N) casts the string to a nonbinary string and pads it on the right with spaces to a length
of N characters. N must be at least 1. IfN is less than the length of the input string, the string is truncated
to N characters. No warning occurs for truncation.
AS BINARY(N) is similar but casts the string to a binary string, N is measured in bytes (not characters), and
padding uses 0x00 bytes (not spaces).
+---------------------------------------+
Dashboard / My courses / SQL & Databases / Sections / 7) Extended MySQL Functions Reference
/ Numeric Functions & Operators Reference
Name Description
ABS() Return the absolute value
ACOS() Return the arc cosine
ASIN() Return the arc sine
ATAN() Return the arc tangent
ATAN2(), ATAN() Return the arc tangent of the two arguments
CEIL() Return the smallest integer value not less than the argument
CEILING() Return the smallest integer value not less than the argument
CONV() Convert numbers between different number bases
COS() Return the cosine
COT() Return the cotangent
CRC32() Compute a cyclic redundancy check value
DEGREES() Convert radians to degrees
DIV Integer division
/ Division operator
EXP() Raise to the power of
FLOOR() Return the largest integer value not greater than the argument
LN() Return the natural logarithm of the argument
LOG() Return the natural logarithm of the Qrst argument
LOG10() Return the base-10 logarithm of the argument
LOG2() Return the base-2 logarithm of the argument
- Minus operator
MOD() Return the remainder
%, MOD Modulo operator
PI() Return the value of pi
+ Addition operator
POW() Return the argument raised to the speciQed power
POWER() Return the argument raised to the speciQed power
RADIANS() Return argument converted to radians
RAND() Return a random Yoating-point value
ROUND() Round the argument
SIGN() Return the sign of the argument
SIN() Return the sine of the argument
SQRT() Return the square root of the argument
TAN() Return the tangent of the argument
* Multiplication operator
TRUNCATE() Truncate to speciQed number of decimal places
- Change the sign of the argument
If both operands are integers and any of them are unsigned, the result is
an unsigned integer. For subtraction, if
theNO_UNSIGNED_SUBTRACTION SQL mode is enabled, the result is signed
even if any operand is unsigned.
In division performed with /, the scale of the result when using two
exact-value operands is the scale of the first operand plus the value of
thediv_precision_increment system variable (which is 4 by default).
For example, the result of the expression 5.05 / 0.014 has a scale of
six decimal places (360.714286).
These rules are applied for each operation, such that nested calculations
imply the precision of each component. Hence, (14620 / 9432456) /
(24250 / 9432456), resolves first to (0.0014) / (0.0026), with the final
result having 8 decimal places (0.60288653).
Because of these rules and the way they are applied, care should be taken to
ensure that components and subcomponents of a calculation use the
appropriate level of precision. See Section 12.10, “Cast Functions and
Operators”.
Addition:
Subtraction:
mysql> SELECT - 2;
-> -2
Note
If this operator is used with a BIGINT, the return value is also a BIGINT.
This means that you should avoid using - on integers that may have the
value of −263.
Multiplication:
The last expression produces an error because the result of the integer
multiplication exceeds the 64-bit range of BIGINT calculations.
(SeeSection 11.2, “Numeric Types”.)
Division:
mysql> SELECT 3/5;
-> 0.60
DIV
Integer division. Discards from the division result any fractional part to
the right of the decimal point.
N % M, N MOD M
Name Description
AVG() Return the average value of the argument
BIT_AND() Return bitwise AND
BIT_OR() Return bitwise OR
BIT_XOR() Return bitwise XOR
COUNT() Return a count of the number of rows returned
COUNT(DISTINCT) Return the count of a number of different values
GROUP_CONCAT() Return a concatenated string
JSON_ARRAYAGG() Return result set as a single JSON array
JSON_OBJECTAGG() Return result set as a single JSON object
MAX() Return the maximum value
MIN() Return the minimum value
STD() Return the population standard deviation
STDDEV() Return the population standard deviation
STDDEV_POP() Return the population standard deviation
STDDEV_SAMP() Return the sample standard deviation
SUM() Return the sum
VAR_POP() Return the population standard variance
VAR_SAMP() Return the sample variance
VARIANCE() Return the population standard variance
If you use a group function in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows. For
more information, seeSection 12.20.3, “MySQL Handling of GROUP BY”.
Most aggregate functions can be used as window functions. Those that can be used this way are signi`ed in their
syntax description by [over_clause] , representing an optional OVER clause. over_clause is described in Section 12.21.2,
“Window Function Concepts and Syntax”, which also includes other information about window function usage.
For numeric arguments, the variance and standard deviation functions return a DOUBLE value.
The SUM() and AVG() functions return a DECIMALvalue for exact-value arguments (integer or DECIMAL), and a DOUBLE value
for approximate-value arguments (FLOAT or DOUBLE).
The SUM() and AVG() aggregate functions do not work with temporal values. (They convert the values to numbers,
losing everything after the `rst nonnumeric character.) To work around this problem, convert to numeric units, perform
the aggregate operation, and convert back to a temporal value. Examples:
Functions such as SUM() or AVG() that expect a numeric argument cast the argument to a number if necessary.
For SET or ENUM values, the cast operation causes the underlying numeric value to be used.
The BIT_AND(), BIT_OR(), and BIT_XOR() aggregate functions perform bit operations. Prior to MySQL 8.0, bit functions
and operators required BIGINT (64-bit integer) arguments and returned BIGINT values, so they had a maximum range of
64 bits. Non-BIGINT arguments were converted to BIGINT prior to performing the operation and truncation could occur.
In MySQL 8.0, bit functions and operators permit binary string type arguments (BINARY, VARBINARY, and the BLOB types)
and return a value of like type, which enables them to take arguments and produce return values larger than 64 bits. For
discussion about argument evaluation and result types for bit operations, see the introductory discussion
in Section 12.12, “Bit Functions and Operators”.
Returns the average value of expr . The DISTINCT option can be used to return the average of the distinct values
of expr.
BIT_AND(expr) [over_clause]
The result type depends on whether the function argument values are evaluated as binary strings or numbers:
Binary-string evaluation occurs when the argument values have a binary string type, and the argument is not a
hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument value
conversion to unsigned 64-bit integers as necessary.
Binary-string evaluation produces a binary string of the same length as the argument values. If argument values
have unequal lengths, anER_INVALID_BITWISE_OPERANDS_SIZE error occurs. If the argument size exceeds 511
bytes, anER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE error occurs. Numeric evaluation produces an unsigned
64-bit integer.
If there are no matching rows, BIT_AND() returns a neutral value (all bits set to 1) having the same length as the
argument values.
NULL values do not affect the result unless all values are NULL . In that case, the result is a neutral value having the
same length as the argument values.
For more information discussion about argument evaluation and result types, see the introductory discussion
in Section 12.12, “Bit Functions and Operators”.
As of MySQL 8.0.12, this function executes as a window function if over_clause is present. over_clause is as
described in Section 12.21.2, “Window Function Concepts and Syntax”.
BIT_OR(expr) [over_clause]
The result type depends on whether the function argument values are evaluated as binary strings or numbers:
Binary-string evaluation occurs when the argument values have a binary string type, and the argument is not a
hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument value
conversion to unsigned 64-bit integers as necessary.
Binary-string evaluation produces a binary string of the same length as the argument values. If argument values
have unequal lengths, anER_INVALID_BITWISE_OPERANDS_SIZE error occurs. If the argument size exceeds 511
bytes, anER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE error occurs. Numeric evaluation produces an unsigned
64-bit integer.
If there are no matching rows, BIT_OR() returns a neutral value (all bits set to 0) having the same length as the
argument values.
NULL values do not affect the result unless all values are NULL . In that case, the result is a neutral value having the
same length as the argument values.
For more information discussion about argument evaluation and result types, see the introductory discussion
in Section 12.12, “Bit Functions and Operators”.
As of MySQL 8.0.12, this function executes as a window function if over_clause is present. over_clause is as
described in Section 12.21.2, “Window Function Concepts and Syntax”.
BIT_XOR(expr) [over_clause]
The result type depends on whether the function argument values are evaluated as binary strings or numbers:
Binary-string evaluation occurs when the argument values have a binary string type, and the argument is not a
hexadecimal literal, bit literal, or NULL literal. Numeric evaluation occurs otherwise, with argument value
conversion to unsigned 64-bit integers as necessary.
Binary-string evaluation produces a binary string of the same length as the argument values. If argument values
have unequal lengths, anER_INVALID_BITWISE_OPERANDS_SIZE error occurs. If the argument size exceeds 511
bytes, anER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE error occurs. Numeric evaluation produces an unsigned
64-bit integer.
If there are no matching rows, BIT_XOR() returns a neutral value (all bits set to 0) having the same length as the
argument values.
NULL values do not affect the result unless all values are NULL . In that case, the result is a neutral value having the
same length as the argument values.
For more information discussion about argument evaluation and result types, see the introductory discussion
in Section 12.12, “Bit Functions and Operators”.
As of MySQL 8.0.12, this function executes as a window function if over_clause is present. over_clause is as
described in Section 12.21.2, “Window Function Concepts and Syntax”.
COUNT(expr) [over_clause]
Returns a count of the number of non- NULL values of expr in the rows retrieved by a SELECT statement. The result is
a BIGINT value.
COUNT(*) is somewhat different in that it returns a count of the number of rows retrieved, whether or not they
contain NULL values.
For transactional storage engines such as InnoDB , storing an exact row count is problematic. Multiple transactions
may be occurring at the same time, each of which may affect the count.
InnoDB does not keep an internal count of rows in a table because concurrent transactions might “see” different
numbers of rows at the same time. Consequently, SELECT COUNT(*) statements only count rows visible to the
current transaction.
As of MySQL 8.0.13, SELECT COUNT(*) FROM tbl_name query performance for InnoDB tables is optimized for single-
threaded workloads if there are no extra clauses such as WHERE or GROUP BY .
InnoDB processes SELECT COUNT(*) statements by traversing the smallest available secondary index unless an
index or optimizer hint directs the optimizer to use a different index. If a secondary index is not
present, InnoDB processes SELECT COUNT(*) statements by scanning the clustered index.
Processing SELECT COUNT(*) statements takes some time if index records are not entirely in the buffer pool. For a
faster count, create a counter table and let your application update it according to the inserts and deletes it does.
However, this method may not scale well in situations where thousands of concurrent transactions are initiating
updates to the same counter table. If an approximate row count is suicient, use SHOW TABLE STATUS.
InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way. There is no performance
difference.
For MyISAM tables, COUNT(*) is optimized to return very quickly if the SELECT retrieves from one table, no other
columns are retrieved, and there is no WHERE clause. For example:
This optimization only applies to MyISAM tables, because an exact row count is stored for this storage engine and
can be accessed very quickly. COUNT(1) is only subject to the same optimization if the `rst column is de`ned as NOT
NULL .
COUNT(DISTINCT expr,[expr...])
Returns a count of the number of rows with different non- NULL expr values.
In MySQL, you can obtain the number of distinct expression combinations that do not contain NULL by giving a list
of expressions. In standard SQL, you would have to do a concatenation of all expressions inside COUNT(DISTINCT
...).
GROUP_CONCAT(expr)
This function returns a string result with the concatenated non- NULL values from a group. It returns NULL if there
are no non- NULL values. The full syntax is as follows:
Or:
In MySQL, you can get the concatenated values of expression combinations. To eliminate duplicate values, use
the DISTINCT clause. To sort values in the result, use the ORDER BY clause. To sort in reverse order, add
the DESC (descending) keyword to the name of the column you are sorting by in the ORDER BY clause. The default is
ascending order; this may be speci`ed explicitly using the ASC keyword. The default separator between values in a
group is comma ( , ). To specify a separator explicitly, use SEPARATOR followed by the string literal value that should
be inserted between group values. To eliminate the separator altogether, specify SEPARATOR '' .
The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has
a default value of 1024. The value can be set higher, although the effective maximum length of the return value is
constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at
runtime is as follows, where val is an unsigned integer:
The return value is a nonbinary or binary string, depending on whether the arguments are nonbinary or binary
strings. The result type is TEXTor BLOB unless group_concat_max_len is less than or equal to 512, in which case the
result type is VARCHAR or VARBINARY.
JSON_ARRAYAGG(col_or_expr) [over_clause]
Aggregates a result set as a single JSON array whose elements consist of the rows. The order of elements in this
array is unde`ned. The function acts on a column or an expression that evaluates to a single value. Returns NULL if
the result contains no rows, or in the event of an error.
As of MySQL 8.0.14, this function executes as a window function if over_clause is present. over_clause is as
described in Section 12.21.2, “Window Function Concepts and Syntax”.
Takes two column names or expressions as arguments, the `rst of these being used as a key and the second as a
value, and returns a JSON object containing key-value pairs. Returns NULL if the result contains no rows, or in the
event of an error. An error occurs if any key name is NULL or the number of arguments is not equal to 2.
As of MySQL 8.0.14, this function executes as a window function if over_clause is present. over_clause is as
described in Section 12.21.2, “Window Function Concepts and Syntax”.
mysql> SELECT o_id, attribute, value FROM t3;
+------+-----------+--------+
| o_id | attribute | value |
+------+-----------+--------+
| 2 | color | red |
| 2 | fabric | silk |
| 3 | color | green |
| 3 | shape | square |
+------+-----------+--------+
4 rows in set (0.00 sec)
The key chosen from the last query is nondeterministic. If you prefer a particular key ordering, you can
invoke JSON_OBJECTAGG() as a window function by including an OVER clause with an ORDER BY speci`cation to
impose a particular order on frame rows. The following examples show what happens with and without ORDER
BY for a few different frame speci`cations.
To return a particular key value (such as the smallest or largest), include a LIMIT clause in the appropriate query.
For example:
mysql> SELECT JSON_OBJECTAGG(c, i)
OVER (ORDER BY i) AS json_object FROM t LIMIT 1;
+-------------+
| json_object |
+-------------+
| {"key": 3} |
+-------------+
mysql> SELECT JSON_OBJECTAGG(c, i)
OVER (ORDER BY i DESC) AS json_object FROM t LIMIT 1;
+-------------+
| json_object |
+-------------+
| {"key": 5} |
+-------------+
See Normalization, Merging, and Autowrapping of JSON Values, for additional information and examples.
Returns the maximum value of expr. MAX() may take a string argument; in such cases, it returns the maximum string
value. See Section 8.3.1, “How MySQL Uses Indexes”. The DISTINCT keyword can be used to `nd the maximum of
the distinct values of expr, however, this produces the same result as omitting DISTINCT .
For MAX(), MySQL currently compares ENUM and SET columns by their string value rather than by the string's relative
position in the set. This differs from how ORDER BY compares them.
Returns the minimum value of expr. MIN() may take a string argument; in such cases, it returns the minimum string
value. See Section 8.3.1, “How MySQL Uses Indexes”. The DISTINCT keyword can be used to `nd the minimum of
the distinct values of expr, however, this produces the same result as omitting DISTINCT .
STD(expr) [over_clause]
Returns the population standard deviation of expr. STD() is a synonym for the standard SQL function STDDEV_POP(),
provided as a MySQL extension.
STDDEV(expr) [over_clause]
Returns the population standard deviation of expr. STDDEV() is a synonym for the standard SQL
function STDDEV_POP(), provided for compatibility with Oracle.
STDDEV_POP(expr) [over_clause]
Returns the population standard deviation of expr (the square root of VAR_POP()). You can also
use STD() or STDDEV(), which are equivalent but not standard SQL.
STDDEV_SAMP(expr) [over_clause]
Returns the sample standard deviation of expr (the square root of VAR_SAMP().
Returns the sum of expr. If the return set has no rows, Dashboard
SUM() returns NULL . The DISTINCT keyword can be used to
Course Options Members-Only Forum About
sum only the distinct values of expr.
VAR_POP(expr) [over_clause]
Returns the population standard variance of expr. It considers rows as the whole population, not as a sample, so it
has the number of rows as the denominator. You can also use VARIANCE(), which is equivalent but is not standard
SQL.
VAR_SAMP(expr) [over_clause]
Returns the sample variance of expr. That is, the denominator is the number of rows minus one.
VARIANCE(expr) [over_clause]
Returns the population standard variance of expr. VARIANCE() is a synonym for the standard SQL
function VAR_POP(), provided as a MySQL extension.
◀︎ Date & Time Functions Reference (Beast) GROUP BY WITH ROLLUP Reference ▶︎
"Success occurs when opportunity meets preparation."
Suppose that a sales table has year, country, product, and profit columns
for recording sales profitability:
To summarize table contents per year, use a simple GROUP BY like this:
The NULL value in the year column identifies the grand total super-aggregate
line.
ROLLUP has a more complex effect when there are multiple GROUP
BY columns. In this case, each time there is a change in value in any but the
last grouping column, the query produces an extra super-aggregate
summary row.
Now the output includes summary information at four levels of analysis, not
just one:
Following each set of product rows for a given year and country, an
extra super-aggregate summary row appears showing the total for all
products. These rows have the product column set to NULL.
The NULL indicators in each super-aggregate row are produced when the row
is sent to the client. The server looks at the columns named in the GROUP
BY clause following the leftmost one that has changed value. For any column
in the result set with a name that matches any of those names, its value is
set to NULL. (If you specify grouping columns by column position, the server
identifies which columns to set to NULL by position.)
Because the NULL values in the super-aggregate rows are placed into the
result set at such a late stage in query processing, you can test them
asNULL values only in the select list or HAVING clause. You cannot test them
as NULL values in join conditions or the WHERE clause to determine which rows
to select. For example, you cannot add WHERE product IS NULL to the query
to eliminate from the output all but the super-aggregate rows.
The NULL values do appear as NULL on the client side and can be tested as
such using any MySQL client programming interface. However, at this point,
you cannot distinguish whether a NULL represents a regular grouped value or
a super-aggregate value. To test the distinction, use theGROUPING() function,
described later.
Previously, MySQL did not allow the use of DISTINCT or ORDER BY in a query
having a WITH ROLLUP option. This restriction is lifted in MySQL 8.0.12 and
later. (Bug #87450, Bug #86311, Bug #26640100, Bug #26073513)
For GROUP BY ... WITH ROLLUP queries, to test whether NULL values in the
result represent super-aggregate values, the GROUPING() function is available
for use in the select list, HAVING clause, and (as of MySQL 8.0.12) ORDER
BY clause. For example, GROUPING(year) returns 1 when NULL in
the year column occurs in a super-aggregate row, and 0 otherwise.
Similarly, GROUPING(country) and GROUPING(product) return 1 for super-
aggregate NULL values in the country and product columns, respectively:
mysql> SELECT
year, country, product, SUM(profit) AS profit,
GROUPING(year) AS grp_year,
GROUPING(country) AS grp_country,
GROUPING(product) AS grp_product
FROM sales
GROUP BY year, country, product WITH ROLLUP;
+------+---------+------------+--------+----------+-------------+-------------+
| year | country | product | profit | grp_year | grp_country | grp_product |
+------+---------+------------+--------+----------+-------------+-------------+
| 2000 | Finland | Computer | 1500 | 0 | 0 | 0 |
| 2000 | Finland | Phone | 100 | 0 | 0 | 0 |
| 2000 | Finland | NULL | 1600 | 0 | 0 | 1 |
| 2000 | India | Calculator | 150 | 0 | 0 | 0 |
| 2000 | India | Computer | 1200 | 0 | 0 | 0 |
| 2000 | India | NULL | 1350 | 0 | 0 | 1 |
| 2000 | USA | Calculator | 75 | 0 | 0 | 0 |
| 2000 | USA | Computer | 1500 | 0 | 0 | 0 |
| 2000 | USA | NULL | 1575 | 0 | 0 | 1 |
| 2000 | NULL | NULL | 4525 | 0 | 1 | 1 |
| 2001 | Finland | Phone | 10 | 0 | 0 | 0 |
| 2001 | Finland | NULL | 10 | 0 | 0 | 1 |
| 2001 | USA | Calculator | 50 | 0 | 0 | 0 |
| 2001 | USA | Computer | 2700 | 0 | 0 | 0 |
| 2001 | USA | TV | 250 | 0 | 0 | 0 |
| 2001 | USA | NULL | 3000 | 0 | 0 | 1 |
| 2001 | NULL | NULL | 3010 | 0 | 1 | 1 |
| NULL | NULL | NULL | 7535 | 1 | 1 | 1 |
+------+---------+------------+--------+----------+-------------+-------------+
mysql> SELECT
IF(GROUPING(year), 'All years', year) AS year,
IF(GROUPING(country), 'All countries', country) AS country,
IF(GROUPING(product), 'All products', product) AS product,
SUM(profit) AS profit
FROM sales
GROUP BY year, country, product WITH ROLLUP;
+-----------+---------------+--------------+--------+
| year | country | product | profit |
+-----------+---------------+--------------+--------+
| 2000 | Finland | Computer | 1500 |
| 2000 | Finland | Phone | 100 |
| 2000 | Finland | All products | 1600 |
| 2000 | India | Calculator | 150 |
| 2000 | India | Computer | 1200 |
| 2000 | India | All products | 1350 |
| 2000 | USA | Calculator | 75 |
| 2000 | USA | Computer | 1500 |
| 2000 | USA | All products | 1575 |
| 2000 | All countries | All products | 4525 |
| 2001 | Finland | Phone | 10 |
| 2001 | Finland | All products | 10 |
| 2001 | USA | Calculator | 50 |
| 2001 | USA | Computer | 2700 |
| 2001 | USA | TV | 250 |
| 2001 | USA | All products | 3000 |
| 2001 | All countries | All products | 3010 |
| All years | All countries | All products | 7535 |
+-----------+---------------+--------------+--------+
The sales table contains no NULL values, so all NULL values in a ROLLUP result
represent super-aggregate values. When the data set
contains NULLvalues, ROLLUP summaries may contain NULL values not only in
super-aggregate rows, but also in regular grouped rows. GROUPING() enables
these to be distinguished. Suppose that table t1 contains a simple data set
with two grouping factors for a set of quantity values, where NULL indicates
something like “other” or “unknown”:
mysql> SELECT
IF(GROUPING(name) = 1, 'All items', name) AS name,
IF(GROUPING(size) = 1, 'All sizes', size) AS size,
SUM(quantity) AS quantity
FROM t1
GROUP BY name, size WITH ROLLUP;
+-----------+-----------+----------+
| name | size | quantity |
+-----------+-----------+----------+
| ball | NULL | 5 |
| ball | large | 20 |
| ball | small | 10 |
| ball | All sizes | 35 |
| hoop | NULL | 3 |
| hoop | large | 5 |
| hoop | small | 15 |
| hoop | All sizes | 23 |
| All items | All sizes | 58 |
+-----------+-----------+----------+
Prior to MySQL 8.0.12, when you use ROLLUP, you cannot also use an ORDER
BY clause to sort the results. In other words, ROLLUP and ORDER BYwere
mutually exclusive in MySQL. However, you still have some control over sort
order. To work around the restriction that prevents using ROLLUPwith ORDER
BY and achieve a specific sort order of grouped results, generate the
grouped result set as a derived table and apply ORDER BY to it. For example:
In both cases, the super-aggregate summary rows sort with the rows from
which they are calculated, and their placement depends on sort order (at the
end for ascending sort, at the beginning for descending sort).
Using LIMIT with ROLLUP may produce results that are more difficult to
interpret, because there is less context for understanding the super-
aggregate rows.
A MySQL extension permits a column that does not appear in the GROUP
BY list to be named in the select list. (For information about nonaggregated
columns and GROUP BY, see Section 12.20.3, “MySQL Handling of GROUP
BY”.) In this case, the server is free to choose any value from this
nonaggregated column in summary rows, and this includes the extra rows
added by WITH ROLLUP. For example, in the following query, country is a
nonaggregated column that does not appear in the GROUP BY list and values
chosen for this column are nondeterministic: