MySQL by Examples for Beginners
1. Summary of MySQL Commands Used in this Tutorial
For detailed syntax, check MySQL manual “SQL Statement Syntax” @ http://dev.mysql.com/doc/refman/5.5/en/sql-syntax.html.
-- Database-Level DROP DATABASE databaseName -- Delete the database (irrecoverable!) DROP DATABASE IF EXISTS databaseName -- Delete if it exists CREATE DATABASE databaseName -- Create a new database CREATE DATABASE IF NOT EXISTS databaseName -- Create only if it does not exists SHOW DATABASES -- Show all the databases in this server USE databaseName -- Set the default (current) database SELECT DATABASE() -- Show the default database SHOW CREATE DATABASE databaseName -- Show the CREATE DATABASE statement -- Table-Level DROP TABLE [IF EXISTS] tableName, ... CREATE TABLE [IF NOT EXISTS] tableName ( columnName columnType columnAttribute, ... PRIMARY KEY(columnName), FOREIGN KEY (columnNmae) REFERENCES tableName (columnNmae) ) SHOW TABLES -- Show all the tables in the default database DESCRIBE|DESC tableName -- Describe the details for a table ALTER TABLE tableName ... -- Modify a table, e.g., ADD COLUMN and DROP COLUMN ALTER TABLE tableName ADD columnDefinition ALTER TABLE tableName DROP columnName ALTER TABLE tableName ADD FOREIGN KEY (columnNmae) REFERENCES tableName (columnNmae) ALTER TABLE tableName DROP FOREIGN KEY constraintName SHOW CREATE TABLE tableName -- Show the CREATE TABLE statement for this tableName -- Row-Level INSERT INTO tableName VALUES (column1Value, column2Value,...) -- Insert on all Columns INSERT INTO tableName VALUES (column1Value, column2Value,...), ... -- Insert multiple rows INSERT INTO tableName (column1Name, ..., columnNName) VALUES (column1Value, ..., columnNValue) -- Insert on selected Columns DELETE FROM tableName WHERE criteria UPDATE tableName SET columnName = expr, ... WHERE criteria SELECT * | column1Name AS alias1, ..., columnNName AS aliasN FROM tableName WHERE criteria GROUP BY columnName ORDER BY columnName ASC|DESC, ... HAVING groupConstraints LIMIT count | offset count -- Others SHOW WARNINGS; -- Show the warnings of the previous statement
2. An Example for the Beginners (But NOT for the dummies)
A MySQL database server contains many databases (or schemas). Each database consists of one or more tables. A table is made up of columns (or fields) and rows (records).
The SQL keywords and commands are NOT case-sensitive. For clarity, they are shown in uppercase. The names or identifiers (database names, table names, column names, etc.) are case-sensitive in some systems, but not in other systems. Hence, it is best to treat identifiers as case-sensitive.
SHOW DATABASES
You can use SHOW DATABASES
to list all the existing databases in the server.
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | ........
The databases “mysql
“, “information_schema
” and “performance_schema
” are system databases used internally by MySQL. A “test
” database is provided during installation for your testing.
Let us begin with a simple example – a product sales database. A product sales database typically consists of many tables, e.g., products, customers, suppliers, orders, payments, employees, among others. Let’s call our database “southwind
” (inspired from Microsoft’s Northwind Trader sample database). We shall begin with the first table called “products
” with the following columns (having data types as indicated) and rows:
Database: southwind Table: products |
||||
---|---|---|---|---|
productID INT |
productCode CHAR(3) |
name VARCHAR(30) |
quantity INT |
price DECIMAL(10,2) |
1001 | PEN | Pen Red | 5000 | 1.23 |
1002 | PEN | Pen Blue | 8000 | 1.25 |
1003 | PEN | Pen Black | 2000 | 1.25 |
1004 | PEC | Pencil 2B | 10000 | 0.48 |
1005 | PEC | Pencil 2H | 8000 | 0.49 |
2.1 Creating and Deleting a Database – CREATE DATABASE and DROP DATABASE
You can create a new database using SQL command “CREATE DATABASE databaseName
“; and delete a database using “DROP DATABASE databaseName
“. You could optionally apply condition “IF EXISTS
” or “IF NOT EXISTS
” to these commands. For example,
mysql> CREATE DATABASE southwind; Query OK, 1 row affected (0.03 sec) mysql> DROP DATABASE southwind; Query OK, 0 rows affected (0.11 sec) mysql> CREATE DATABASE IF NOT EXISTS southwind; Query OK, 1 row affected (0.01 sec) mysql> DROP DATABASE IF EXISTS southwind; Query OK, 0 rows affected (0.00 sec)
IMPORTANT: Use SQL DROP
(and DELETE
) commands with extreme care, as the deleted entities are irrecoverable. THERE IS NO UNDO!!!
SHOW CREATE DATABASE
The CREATE DATABASE
commands uses some defaults. You can issue a “SHOW CREATE DATABASE databaseName
” to display the full command and check these default values. We use \G
(instead of ';'
) to display the results vertically. (Try comparing the outputs produced by ';'
and \G
.)
mysql> CREATE DATABASE IF NOT EXISTS southwind;
mysql> SHOW CREATE DATABASE southwind \G
*************************** 1. row ***************************
Database: southwind
Create Database: CREATE DATABASE `southwind` /*!40100 DEFAULT CHARACTER SET latin1 */
Back-Quoted Identifiers (`name`)
Unquoted names or identifiers (such as database name, table name and column name) cannot contain blank and special characters, or crash with MySQL keywords (such as ORDER
and DESC
). You can include blanks and special characters or use MySQL keyword as identifier by enclosing it with a pair of back-quote, in the form of `name`
.
For robustness, the SHOW
command back-quotes all the identifiers, as illustrated in the above example.
Comments and Version Comments
MySQL multi-line comments are enclosed within /*
and */
; end-of-line comments begins with --
(followed by a space) or #
.
The /*!40100 ...... */
is known as version comment, which will only be run if the server is at or above this version number 4.01.00
. To check the version of your MySQL server, issue query “SELECT version()
“.
2.2 Setting the Default Database – USE
The command “USE databaseName
” sets a particular database as the default (or current) database. You can reference a table in the default database using tableName
directly. But you need to use the fully-qualified databaseName.tableName
to reference a table NOT in the default database.
In our example, we have a database named “southwind
” with a table named “products
“. If we issue “USE southwind
” to set southwind
as the default database, we can simply call the table as “products
“. Otherwise, we need to reference the table as “southwind.products
“.
To display the current default database, issue command “SELECT DATABASE()
“.
2.3 Creating and Deleting a Table – CREATE TABLE and DROP TABLE
You can create a new table in the default database using command “CREATE TABLE tableName
” and “DROP TABLE tableName
“. You can also apply condition “IF EXISTS
” or “IF NOT EXISTS
“. To create a table, you need to define all its columns, by providing the columns’ name, type, and attributes.
Let’s create a table “products
” in our database “southwind
“.
-- Remove the database "southwind", if it exists. -- Beware that DROP (and DELETE) actions are irreversible and not recoverable! mysql> DROP DATABASE IF EXISTS southwind; Query OK, 1 rows affected (0.31 sec) -- Create the database "southwind" mysql> CREATE DATABASE southwind; Query OK, 1 row affected (0.01 sec) -- Show all the databases in the server -- to confirm that "southwind" database has been created. mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | southwind | | ...... | -- Set "southwind" as the default database so as to reference its table directly. mysql> USE southwind; Database changed -- Show the current (default) database mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | southwind | +------------+ -- Show all the tables in the current database. -- "southwind" has no table (empty set). mysql> SHOW TABLES; Empty set (0.00 sec) -- Create the table "products". Read "explanations" below for the column defintions mysql> CREATE TABLE IF NOT EXISTS products ( productID INT UNSIGNED NOT NULL AUTO_INCREMENT, productCode CHAR(3) NOT NULL DEFAULT '', name VARCHAR(30) NOT NULL DEFAULT '', quantity INT UNSIGNED NOT NULL DEFAULT 0, price DECIMAL(7,2) NOT NULL DEFAULT 99999.99, PRIMARY KEY (productID) ); Query OK, 0 rows affected (0.08 sec) -- Show all the tables to confirm that the "products" table has been created mysql> SHOW TABLES; +---------------------+ | Tables_in_southwind | +---------------------+ | products | +---------------------+ -- Describe the fields (columns) of the "products" table mysql> DESCRIBE products; +-------------+------------------+------+-----+------------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+------------------+------+-----+------------+----------------+ | productID | int(10) unsigned | NO | PRI | NULL | auto_increment | | productCode | char(3) | NO | | | | | name | varchar(30) | NO | | | | | quantity | int(10) unsigned | NO | | 0 | | | price | decimal(7,2) | NO | | 99999.99 | | +-------------+------------------+------+-----+------------+----------------+ -- Show the complete CREATE TABLE statement used by MySQL to create this table mysql> SHOW CREATE TABLE products \G *************************** 1. row *************************** Table: products Create Table: CREATE TABLE `products` ( `productID` int(10) unsigned NOT NULL AUTO_INCREMENT, `productCode` char(3) NOT NULL DEFAULT '', `name` varchar(30) NOT NULL DEFAULT '', `quantity` int(10) unsigned NOT NULL DEFAULT '0', `price` decimal(7,2) NOT NULL DEFAULT '99999.99', PRIMARY KEY (`productID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
Explanations
We define 5 columns in the table products
: productID
, productCode
, name
, quantity
and price
. The types are:
productID
isINT UNSIGNED
– non-negative integers.productCode
isCHAR(3)
– a fixed-length alphanumeric string of 3 characters.name
isVARCHAR(30)
– a variable-length string of up to 30 characters.
We use fixed-length string forproductCode
, as we assume that theproductCode
contains exactly 3 characters. On the other hand, we use variable-length string forname
, as its length varies –VARCHAR
is more efficient thanCHAR
.quantity
is alsoINT UNSIGNED
(non-negative integers).price
isDECIMAL(10,2)
– a decimal number with 2 decimal places.
DECIMAL
is precise (represented as integer with a fix decimal point). On the other hand,FLOAT
andDOUBLE
(real numbers) are not precise and are approximated.DECIMAL
type is recommended for currency.
The attribute “NOT NULL
” specifies that the column cannot contain the NULL
value. NULL
is a special value indicating “no value”, “unknown value” or “missing value”. In our case, these columns shall have a proper value. We also set the default value of the columns. The column will take on its default value, if no value is specified during the record creation.
We set the column productID
as the so-called primary key. Values of the primary-key column must be unique. Every table shall contain a primary key. This ensures that every row can be distinguished from other rows. You can specify a single column or a set of columns (e.g., firstName
and lastName
) as the primary key. An index is build automatically on the primary-key column to facilitate fast search. Primary key is also used as reference by other tables.
We set the column productID
to AUTO_INCREMENT
. with default starting value of 1. When you insert a row with NULL
(recommended) (or 0, or a missing value) for the AUTO_INCREMENT
column, the maximum value of that column plus 1 would be inserted. You can also insert a valid value to an AUTO_INCREMENT
column, bypassing the auto-increment.
2.4 Inserting Rows – INSERT INTO
Let’s fill up our “products
” table with rows. We set the productID
of the first record to 1001, and use AUTO_INCREMENT
for the rest of records by inserting a NULL
, or with a missing column value. Take note that strings must be enclosed with a pair of single quotes (or double quotes).
-- Insert a row with all the column values mysql> INSERT INTO products VALUES (1001, 'PEN', 'Pen Red', 5000, 1.23); Query OK, 1 row affected (0.04 sec) -- Insert multiple rows in one command -- Inserting NULL to the auto_increment column results in max_value + 1 mysql> INSERT INTO products VALUES (NULL, 'PEN', 'Pen Blue', 8000, 1.25), (NULL, 'PEN', 'Pen Black', 2000, 1.25); Query OK, 2 rows affected (0.03 sec) Records: 2 Duplicates: 0 Warnings: 0 -- Insert value to selected columns -- Missing value for the auto_increment column also results in max_value + 1 mysql> INSERT INTO products (productCode, name, quantity, price) VALUES ('PEC', 'Pencil 2B', 10000, 0.48), ('PEC', 'Pencil 2H', 8000, 0.49); Query OK, 2 row affected (0.03 sec) -- Missing columns get their default values mysql> INSERT INTO products (productCode, name) VALUES ('PEC', 'Pencil HB'); Query OK, 1 row affected (0.04 sec) -- 2nd column (productCode) is defined to be NOT NULL mysql> INSERT INTO products values (NULL, NULL, NULL, NULL, NULL); ERROR 1048 (23000): Column 'productCode' cannot be null -- Query the table mysql> SELECT * FROM products; +-----------+-------------+-----------+----------+------------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+------------+ | 1001 | PEN | Pen Red | 5000 | 1.23 | | 1002 | PEN | Pen Blue | 8000 | 1.25 | | 1003 | PEN | Pen Black | 2000 | 1.25 | | 1004 | PEC | Pencil 2B | 10000 | 0.48 | | 1005 | PEC | Pencil 2H | 8000 | 0.49 | | 1006 | PEC | Pencil HB | 0 | 9999999.99 | +-----------+-------------+-----------+----------+------------+ 6 rows in set (0.02 sec) -- Remove the last row mysql> DELETE FROM products WHERE productID = 1006;
INSERT INTO Syntax
We can use the INSERT INTO
statement to insert a new row with all the column values, using the following syntax:
INSERT INTO tableName VALUES (firstColumnValue, ..., lastColumnValue) -- All columns
You need to list the values in the same order in which the columns are defined in the CREATE TABLE
, separated by commas. For columns of string data type (CHAR
, VARCHAR
), enclosed the value with a pair of single quotes (or double quotes). For columns of numeric data type (INT
, DECIMAL
, FLOAT
, DOUBLE
), simply place the number.
You can also insert multiple rows in one INSERT INTO
statement:
INSERT INTO tableName VALUES (row1FirstColumnValue, ..., row1lastColumnValue), (row2FirstColumnValue, ..., row2lastColumnValue), ...
To insert a row with values on selected columns only, use:
-- Insert single record with selected columns INSERT INTO tableName (column1Name, ..., columnNName) VALUES (column1Value, ..., columnNValue) -- Alternately, use SET to set the values INSERT INTO tableName SET column1=value1, column2=value2, ... -- Insert multiple records INSERT INTO tableName (column1Name, ..., columnNName) VALUES (row1column1Value, ..., row2ColumnNValue), (row2column1Value, ..., row2ColumnNValue), ...
The remaining columns will receive their default value, such as AUTO_INCREMENT
, default, or NULL
.
2.5 Querying the Database – SELECT
The most common, important and complex task is to query a database for a subset of data that meets your needs – with the SELECT
command. The SELECT
command has the following syntax:
-- List all the rows of the specified columns SELECT column1Name, column2Name, ... FROM tableName -- List all the rows of ALL columns, * is a wildcard denoting all columns SELECT * FROM tableName -- List rows that meet the specified criteria in WHERE clause SELECT column1Name, column2Name,... FROM tableName WHERE criteria SELECT * FROM tableName WHERE criteria
For examples,
-- List all rows for the specified columns mysql> SELECT name, price FROM products; +-----------+-------+ | name | price | +-----------+-------+ | Pen Red | 1.23 | | Pen Blue | 1.25 | | Pen Black | 1.25 | | Pencil 2B | 0.48 | | Pencil 2H | 0.49 | +-----------+-------+ 5 rows in set (0.00 sec) -- List all rows of ALL the columns. The wildcard * denotes ALL columns mysql> SELECT * FROM products; +-----------+-------------+-----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+-------+ | 1001 | PEN | Pen Red | 5000 | 1.23 | | 1002 | PEN | Pen Blue | 8000 | 1.25 | | 1003 | PEN | Pen Black | 2000 | 1.25 | | 1004 | PEC | Pencil 2B | 10000 | 0.48 | | 1005 | PEC | Pencil 2H | 8000 | 0.49 | +-----------+-------------+-----------+----------+-------+ 5 rows in set (0.00 sec)
SELECT without Table
You can also issue SELECT
without a table. For example, you can SELECT
an expression or evaluate a built-in function.
mysql> SELECT 1+1;
+-----+
| 1+1 |
+-----+
| 2 |
+-----+
1 row in set (0.00 sec)
mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2012-10-24 22:13:29 |
+---------------------+
1 row in set (0.00 sec)
// Multiple columns
mysql> SELECT 1+1, NOW();
+-----+---------------------+
| 1+1 | NOW() |
+-----+---------------------+
| 2 | 2012-10-24 22:16:34 |
+-----+---------------------+
1 row in set (0.00 sec)
Comparison Operators
For numbers (INT
, DECIMAL
, FLOAT
), you could use comparison operators: '='
(equal to), '<>'
or '!='
(not equal to), '>'
(greater than), '<'
(less than), '>='
(greater than or equal to), '<='
(less than or equal to), to compare two numbers. For example, price > 1.0
, quantity <= 500
.
mysql> SELECT name, price FROM products WHERE price < 1.0; +-----------+-------+ | name | price | +-----------+-------+ | Pencil 2B | 0.48 | | Pencil 2H | 0.49 | +-----------+-------+ 2 rows in set (0.00 sec) mysql> SELECT name, quantity FROM products WHERE quantity <= 2000; +-----------+----------+ | name | quantity | +-----------+----------+ | Pen Black | 2000 | +-----------+----------+ 1 row in set (0.00 sec)
CAUTION: Do not compare FLOAT
s (real numbers) for equality ('='
or '<>'
), as they are not precise. On the other hand, DECIMAL
are precise.
For strings, you could also use '='
, '<>'
, '>'
, '<'
, '>='
, '<='
to compare two strings (e.g., productCode = 'PEC').
The ordering of string depends on the so-called collation chosen. For example,
mysql> SELECT name, price FROM products WHERE productCode = 'PEN';
-- String values are quoted
+-----------+-------+
| name | price |
+-----------+-------+
| Pen Red | 1.23 |
| Pen Blue | 1.25 |
| Pen Black | 1.25 |
+-----------+-------+
3 rows in set (0.00 sec)
String Pattern Matching – LIKE and NOT LIKE
For strings, in addition to full matching using operators like '='
and '<>'
, we can perform pattern matching using operator LIKE
(or NOT LIKE
) with wildcard characters. The wildcard '_'
matches any single character; '%'
matches any number of characters (including zero). For example,
'abc%'
matches strings beginning with'abc'
;'%xyz'
matches strings ending with'xyz'
;'%aaa%'
matches strings containing'aaa'
;'___'
matches strings containing exactly three characters; and'a_b%'
matches strings beginning with'a'
, followed by any single character, followed by'b'
, followed by zero or more characters.
-- "name" begins with 'PENCIL' mysql> SELECT name, price FROM products WHERE name LIKE 'PENCIL%'; +-----------+-------+ | name | price | +-----------+-------+ | Pencil 2B | 0.48 | | Pencil 2H | 0.49 | +-----------+-------+ -- "name" begins with 'P', followed by any two characters, -- followed by space, followed by zero or more characters mysql> SELECT name, price FROM products WHERE name LIKE 'P__ %'; +-----------+-------+ | name | price | +-----------+-------+ | Pen Red | 1.23 | | Pen Blue | 1.25 | | Pen Black | 1.25 | +-----------+-------+
MySQL also support regular expression matching via the REGEXE
operator.
Arithmetic Operators
You can perform arithmetic operations on numeric fields using arithmetic operators, as tabulated below:
Operator | Description |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
DIV | Integer Division |
% | Modulus (Remainder) |
Logical Operators – AND, OR, NOT, XOR
You can combine multiple conditions with boolean operators AND
, OR
, XOR
. You can also invert a condition using operator NOT
. For examples,
mysql> SELECT * FROM products WHERE quantity >= 5000 AND name LIKE 'Pen %'; +-----------+-------------+----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+----------+----------+-------+ | 1001 | PEN | Pen Red | 5000 | 1.23 | | 1002 | PEN | Pen Blue | 8000 | 1.25 | +-----------+-------------+----------+----------+-------+ mysql> SELECT * FROM products WHERE quantity >= 5000 AND price < 1.24 AND name LIKE 'Pen %'; +-----------+-------------+---------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+---------+----------+-------+ | 1001 | PEN | Pen Red | 5000 | 1.23 | +-----------+-------------+---------+----------+-------+ mysql> SELECT * FROM products WHERE NOT (quantity >= 5000 AND name LIKE 'Pen %'); +-----------+-------------+-----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+-------+ | 1003 | PEN | Pen Black | 2000 | 1.25 | | 1004 | PEC | Pencil 2B | 10000 | 0.48 | | 1005 | PEC | Pencil 2H | 8000 | 0.49 | +-----------+-------------+-----------+----------+-------+
IN, NOT IN
You can select from members of a set with IN
(or NOT IN
) operator. This is easier and clearer than the equivalent AND-OR
expression.
mysql> SELECT * FROM products WHERE name IN ('Pen Red', 'Pen Black'); +-----------+-------------+-----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+-------+ | 1001 | PEN | Pen Red | 5000 | 1.23 | | 1003 | PEN | Pen Black | 2000 | 1.25 | +-----------+-------------+-----------+----------+-------+
BETWEEN, NOT BETWEEN
To check if the value is within a range, you could use BETWEEN ... AND ...
operator. Again, this is easier and clearer than the equivalent AND-OR
expression.
mysql> SELECT * FROM products WHERE (price BETWEEN 1.0 AND 2.0) AND (quantity BETWEEN 1000 AND 2000); +-----------+-------------+-----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+-------+ | 1003 | PEN | Pen Black | 2000 | 1.25 | +-----------+-------------+-----------+----------+-------+
IS NULL, IS NOT NULL
NULL
is a special value, which represent “no value”, “missing value” or “unknown value”. You can checking if a column contains NULL
by IS NULL
or IS NOT NULL
. For example,
mysql> SELECT * FROM products WHERE productCode IS NULL; Empty set (0.00 sec)
Using comparison operator (such as =
or <>
) to check for NULL
is a mistake – a very common mistake. For example,
SELECT * FROM products WHERE productCode = NULL; -- This is a common mistake. NULL cannot be compared.
ORDER BY Clause
You can order the rows selected using ORDER BY
clause, with the following syntax:
SELECT ... FROM tableName WHERE criteria ORDER BY columnA ASC|DESC, columnB ASC|DESC, ...
The selected row will be ordered according to the values in columnA
, in either ascending (ASC
) (default) or descending (DESC
) order. If several rows have the same value in columnA
, it will be ordered according to columnB
, and so on. For strings, the ordering could be case-sensitive or case-insensitive, depending on the so-called character collating sequence used. For examples,
-- Order the results by price in descending order mysql> SELECT * FROM products WHERE name LIKE 'Pen %' ORDER BY price DESC; +-----------+-------------+-----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+-------+ | 1002 | PEN | Pen Blue | 8000 | 1.25 | | 1003 | PEN | Pen Black | 2000 | 1.25 | | 1001 | PEN | Pen Red | 5000 | 1.23 | +-----------+-------------+-----------+----------+-------+ -- Order by price in descending order, followed by quantity in ascending (default) order mysql> SELECT * FROM products WHERE name LIKE 'Pen %' ORDER BY price DESC, quantity; +-----------+-------------+-----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+-------+ | 1003 | PEN | Pen Black | 2000 | 1.25 | | 1002 | PEN | Pen Blue | 8000 | 1.25 | | 1001 | PEN | Pen Red | 5000 | 1.23 | +-----------+-------------+-----------+----------+-------+
You can randomize the returned records via function RAND()
, e.g.,
mysql> SELECT * FROM products ORDER BY RAND();
LIMIT Clause
A SELECT
query on a large database may produce many rows. You could use the LIMIT
clause to limit the number of rows displayed, e.g.,
-- Display the first two rows
mysql> SELECT * FROM products ORDER BY price LIMIT 2;
+-----------+-------------+-----------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+-----------+----------+-------+
| 1004 | PEC | Pencil 2B | 10000 | 0.48 |
| 1005 | PEC | Pencil 2H | 8000 | 0.49 |
+-----------+-------------+-----------+----------+-------+
To continue to the following records , you could specify the number of rows to be skipped, followed by the number of rows to be displayed in the LIMIT
clause, as follows:
-- Skip the first two rows and display the next 1 row
mysql> SELECT * FROM products ORDER BY price LIMIT 2, 1;
+-----------+-------------+---------+----------+-------+
| productID | productCode | name | quantity | price |
+-----------+-------------+---------+----------+-------+
| 1001 | PEN | Pen Red | 5000 | 1.23 |
+-----------+-------------+---------+----------+-------+
AS – Alias
You could use the keyword AS
to define an alias for an identifier (such as column name, table name). The alias will be used in displaying the name. It can also be used as reference. For example,
mysql> SELECT productID AS ID, productCode AS Code, name AS Description, price AS `Unit Price` -- Define aliases to be used as display names FROM products ORDER BY ID; -- Use alias ID as reference +------+------+-------------+------------+ | ID | Code | Description | Unit Price | +------+------+-------------+------------+ | 1001 | PEN | Pen Red | 1.23 | | 1002 | PEN | Pen Blue | 1.25 | | 1003 | PEN | Pen Black | 1.25 | | 1004 | PEC | Pencil 2B | 0.48 | | 1005 | PEC | Pencil 2H | 0.49 | +------+------+-------------+------------+
Take note that the identifier “Unit Price
” contains a blank and must be back-quoted.
Function CONCAT()
You can also concatenate a few columns as one (e.g., joining the last name and first name) using function CONCAT()
. For example,
mysql> SELECT CONCAT(productCode, ' - ', name) AS `Product Description`, price FROM products; +---------------------+-------+ | Product Description | price | +---------------------+-------+ | PEN - Pen Red | 1.23 | | PEN - Pen Blue | 1.25 | | PEN - Pen Black | 1.25 | | PEC - Pencil 2B | 0.48 | | PEC - Pencil 2H | 0.49 | +---------------------+-------+
2.6 Producing Summary Reports
To produce a summary report, we often need to aggregate related rows.
DISTINCT
A column may have duplicate values, we could use keyword DISTINCT
to select only distinct values. We can also apply DISTINCT
to several columns to select distinct combinations of these columns. For examples,
-- Without DISTINCT mysql> SELECT price FROM products; +-------+ | price | +-------+ | 1.23 | | 1.25 | | 1.25 | | 0.48 | | 0.49 | +-------+ -- With DISTINCT on price mysql> SELECT DISTINCT price AS `Distinct Price` FROM products; +----------------+ | Distinct Price | +----------------+ | 1.23 | | 1.25 | | 0.48 | | 0.49 | +----------------+ -- DISTINCT combination of price and name mysql> SELECT DISTINCT price, name FROM products; +-------+-----------+ | price | name | +-------+-----------+ | 1.23 | Pen Red | | 1.25 | Pen Blue | | 1.25 | Pen Black | | 0.48 | Pencil 2B | | 0.49 | Pencil 2H | +-------+-----------+
GROUP BY Clause
The GROUP BY
clause allows you to collapse multiple records with a common value into groups. For example,
mysql> SELECT * FROM products ORDER BY productCode, productID; +-----------+-------------+-----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+-------+ | 1004 | PEC | Pencil 2B | 10000 | 0.48 | | 1005 | PEC | Pencil 2H | 8000 | 0.49 | | 1001 | PEN | Pen Red | 5000 | 1.23 | | 1002 | PEN | Pen Blue | 8000 | 1.25 | | 1003 | PEN | Pen Black | 2000 | 1.25 | +-----------+-------------+-----------+----------+-------+ mysql> SELECT * FROM products GROUP BY productCode; -- Only first record in each group is shown +-----------+-------------+-----------+----------+-------+ | productID | productCode | name | quantity | price | +-----------+-------------+-----------+----------+-------+ | 1004 | PEC | Pencil 2B | 10000 | 0.48 | | 1001 | PEN | Pen Red | 5000 | 1.23 | +-----------+-------------+-----------+----------+-------+
GROUP BY
by itself is not meaningful. It is used together with GROUP BY
aggregate functions (such as COUNT()
, AVG()
, SUM()
) to produce group summary.
GROUP BY Aggregate Functions: COUNT, MAX, MIN, AVG, SUM, STD, GROUP_CONCAT
We can apply GROUP BY
Aggregate functions to each group to produce group summary report.
The function COUNT(*)
returns the rows selected; COUNT(columnName)
counts only the non-NULL
values of the given column. For example,
-- Function COUNT(*) returns the number of rows selected mysql> SELECT COUNT(*) AS `Count` FROM products; -- All rows without GROUP BY clause +-------+ | Count | +-------+ | 5 | +-------+ mysql> SELECT productCode, COUNT(*) FROM products GROUP BY productCode; +-------------+----------+ | productCode | COUNT(*) | +-------------+----------+ | PEC | 2 | | PEN | 3 | +-------------+----------+ -- Order by COUNT - need to define an alias to be used as reference mysql> SELECT productCode, COUNT(*) AS count FROM products GROUP BY productCode ORDER BY count DESC; +-------------+-------+ | productCode | count | +-------------+-------+ | PEN | 3 | | PEC | 2 | +-------------+-------+
Besides COUNT()
, there are many other GROUP BY
aggregate functions such as AVG()
, MAX()
, MIN()
and SUM()
. For example,
mysql> SELECT MAX(price), MIN(price), AVG(price), STD(price), SUM(quantity) FROM products; -- Without GROUP BY - All rows +------------+------------+------------+------------+---------------+ | MAX(price) | MIN(price) | AVG(price) | STD(price) | SUM(quantity) | +------------+------------+------------+------------+---------------+ | 1.25 | 0.48 | 0.940000 | 0.371591 | 33000 | +------------+------------+------------+------------+---------------+ mysql> SELECT productCode, MAX(price) AS `Highest Price`, MIN(price) AS `Lowest Price` FROM products GROUP BY productCode; +-------------+---------------+--------------+ | productCode | Highest Price | Lowest Price | +-------------+---------------+--------------+ | PEC | 0.49 | 0.48 | | PEN | 1.25 | 1.23 | +-------------+---------------+--------------+ mysql> SELECT productCode, MAX(price), MIN(price), CAST(AVG(price) AS DECIMAL(7,2)) AS `Average`, CAST(STD(price) AS DECIMAL(7,2)) AS `Std Dev`, SUM(quantity) FROM products GROUP BY productCode; -- Use CAST(... AS ...) function to format floating-point numbers +-------------+------------+------------+---------+---------+---------------+ | productCode | MAX(price) | MIN(price) | Average | Std Dev | SUM(quantity) | +-------------+------------+------------+---------+---------+---------------+ | PEC | 0.49 | 0.48 | 0.49 | 0.01 | 18000 | | PEN | 1.25 | 1.23 | 1.24 | 0.01 | 15000 | +-------------+------------+------------+---------+---------+---------------+
HAVING clause
HAVING
is similar to WHERE
, but it can operate on the GROUP BY
aggregate functions; whereas WHERE
operates only on columns.
mysql> SELECT productCode AS `Product Code`, COUNT(*) AS `Count`, CAST(AVG(price) AS DECIMAL(7,2)) AS `Average` FROM products GROUP BY productCode HAVING Count >=3; -- CANNOT use WHERE count >= 3 +--------------+-------+---------+ | Product Code | Count | Average | +--------------+-------+---------+ | PEN | 3 | 1.24 | +--------------+-------+---------+
WITH ROLLUP
The WITH ROLLUP
clause shows the summary of group summary, e.g.,
mysql> SELECT productCode, MAX(price), MIN(price), CAST(AVG(price) AS DECIMAL(7,2)) AS `Average`, SUM(quantity) FROM products GROUP BY productCode WITH ROLLUP; -- Apply aggregate functions to all groups +-------------+------------+------------+---------+---------------+ | productCode | MAX(price) | MIN(price) | Average | SUM(quantity) | +-------------+------------+------------+---------+---------------+ | PEC | 0.49 | 0.48 | 0.49 | 18000 | | PEN | 1.25 | 1.23 | 1.24 | 15000 | | NULL | 1.25 | 0.48 | 0.94 | 33000 | +-------------+------------+------------+---------+---------------+
2.7 Modifying Data – UPDATE
To modify existing data, use UPDATE ... SET
command, with the following syntax:
UPDATE tableName SET columnName = {value|NULL|DEFAULT}, ... WHERE criteria
For example,
-- Increase the price by 10% for all products
mysql> UPDATE products SET price = price * 1.1;