• Learning MySQL By Example
  • Introduction
  • 1. How to Retrieve Data From a Single Table
  • 2. How to Retrieve Data from Multiple Tables
  • 3. Using Functions
  • 4. How to Insert, Update, Delete Data in Tables
  • 5. Summary Queries and Aggregate Functions
  • 6. Working With Subqueries
  • 7. SQL Views
  • 8. SQL Indexes
  • Glossary
  • Index
  • Download
  • Translations
  • 1

    How to Retrieve Data From a Single Table

    The Five Clauses of the SELECT statement

    • SELECT – the columns in the result set
    • FROM – names the base table(s) from which results will be retrieved
    • WHERE – specifies any conditions for the results set (filter)
    • ORDER BY – sets how the result set will be ordered
    • LIMIT – sets the number of rows to be returned

    The clauses MUST appear in the order shown above.

    Code Example:

    1    USE world;
    2    SELECT name
    3    FROM city
    4    WHERE CountryCode = “AFG”
    5    ORDER BY name
    6    LIMIT 3

    Results:

    Retrieve_data_from_a_single_table_example1.png

    Let us break the statement line by line:

    USE world;

    SELECT name

    FROM city

    ORDER BY name

    LIMIT 5;

    Table 1. Column Specifications

    Source Option Syntax

    Base Table Value

    Show all columns

     

    Base Table Value

    Column Name

    Comma separated list of column names

    Calculated Value

    Calculation result

    Arithmetic expression

    Calculated Value

    Calculation result

    Functions

    LIKE and REGEXP Operators

    • The LIKE keyword is used with the WHERE clause.
    • The LIKE keyword and can use two symbols as wildcards. The percent ( % ) symbol matches any number of characters and the underscore ( _ ) matches a single character
    • REGEXP keyword allows you to do more complex pattern matching than a LIKE keyword/
    • Some version of REGEXP exists in many computer languages. Refer to the “LIKE and REGEXP” handout for a full list of examples.

    Table 2. LIKE Keyword

    LIKE Symbol Description

    %

    Match any string of characters to the left of the symbol

    _

    Match a single character

    Code Example:

    USE world;
    SELECT name
    FROM country
    WHERE name LIKE ‘A%’

    Results:

    LIKE.png

    Table 3. REXEXP Keyword

    REGEXP Characters Description

    ^

    Match the pattern to the beginning of the value being tested.

    $

    Match the pattern to the end of the value being tested.

    .

    Matches any single character.

    [charlist]

    Matches any single character listed within the brackets.

    [char1 – char2]

    Matches any single character within the given range.

    |

    Separates two string patterns and matches either one

    Code Example:

    USE world;
    SELECT name
    FROM country
    WHERE name REGEXP 'g[o,u]';

    Results:

    REGEXP.png

    Arithmetic Operators

    • Arithmetic operators can be used in the SELECT, WHERE, and ORDER BY clauses.
    • Operators are evaluated in the same way as arithmetic in other contexts.


    Table 4. Operators and precendence order

    Operator Name Order of Precedence

    *

    Multiplication

    1

    /

    Division

    1

    DIV

    Integer Division

    1

    % (MOD)

    Modulo (remainder)

    1

    +

    Addition

    2

    -

    Subtraction

    2

    Code Example:

    USE world;
    SELECT name, population / SurfaceArea
    AS "People per square mile"
    FROM country;

    Results:

    arithmetic.png

    Column Aliases

    • A column alias provides a way to create a clean or more descriptive header for a results set.
    • A column alias cannot be used in a SELECT, WHERE, GROUP BY or HAVING clause due to the order of execution. You must refer to the original column name.

    In the previous example, we created a new column that was a calculated value. The problem is that the column header is now population / SurfaceArea. However we can rename the column header to something cleaner be create a column alias.  Look at the code snippet below.

    Code Example:

    SELECT name, population / SurfaceArea
         AS “People per square mile”
    FROM country;

    We used the AS keyword then in quotes we put the new column alias of “People per square mile.” Which changes the column header as seen show below.

    Results:

    column_alias.png

    Comparison Operators

    • Comparison operators compare two expressions.
    • The result of a comparison results to true or false.
    • Comparison operators are not case sensitive and are used with text and dates as well as numbers.

    Table 5. Comparison Operators

    Operator

    Description

    =

    Equal

    Less than

    Greater than

    <=

    Less than or equal to

    >=

    Greater than or equal to

    <> 

    Not equal

    !=

    Not equal

    Code Example:

    USE world;
    SELECT name, population
    FROM country
    WHERE population > 1000000;

    Results:

    comparison_operators.png

    IS NULL

    • Null values indicate an unknown or non-existent value and is different from an empty string (‘ ‘).
    • To test for a null value you use the IS NULL clause
    • The test for a value use IS NOT NULL clause

    Code Example:

    SELECT name, IndepYear
    FROM country
    WHERE IndepYear IS NULL;

    Results:

    is_null.png

    BETWEEN Operators

    • The BETWEEN operator is similar to >= and <=.
    • BETWEEN includes everything between the two values indicated.
    • BETWEEN works with both text and number.


    Code Example:

    USE world;
    SELECT name, IndepYear
    FROM country
    WHERE name BETWEEN "Aruba" and "Bahamas";

    Results:

    between.png

    The IN Keyword

    • The IN clause tests whether an expression is equal to a value or values in a list of expressions.
    • The order of the items in the list does not matter.
    • You can use the NOT operator to test for items not in the list.
    • The IN clause may be used with a subquery.


    Code Example:

    USE world;
    SELECT name
    FROM country
    WHERE name IN ('Aruba', 'Barbados', 'Cuba', 'Bahamas')
    ORDER BY population ASC;

    Results:

    IN.png

    AND, OR, NOT Logical Operators

    • Logical operators are used in the WHERE clause
    • You may use multiple logical operators in a WHERE clause to create a compound condition. The order of evaluation when multiple operators are used is shown in the table above.

    Table 6. Logical Operators

    Operator

    Description

    Order of Evaluation

    NOT

    ( a NOT b ) – a must be present but b must NOT be present to be included

    1

    AND

    ( a AND b ) –If  both a and b are present, item is included

    2

    OR

    ( a OR b ) – If either a OR b is present item is included

    3


    Example:

    USE world;
    SELECT name, population
    FROM country
    WHERE region = 'caribbean'
    AND population > 100000
    ORDER BY population ASC;

    Results:

    logical_operator.png

    DISTINCT Keyword

    • DISTINCT appears directly after the SELECT clause.
    • You can specify multiple columns, which means that the combination of columns must be unique.

    Table 7. DISTINCT Keyword

    Keyword

    Description

    Order of Evaluation

    DISTINCT

    Eliminates duplicate rows

    1

    Example:

    SELECT DISTINCT continent, name
    FROM country
    ORDER BY continent;

    Results:

    distinct.png

    The Five Clauses of the SELECT StatementColumn Specifications LIKE and REGEXP OperatorsArithmetic OperatorsColumn AliasesComparison OperatorsIS NULL, BETWEEN, IN OperatorsAND, OR, NOT Logical OperatorsDISTINCT Clause

    This content is provided to you freely by EdTech Books.

    Access it online or download it at https://edtechbooks.org/learning_mysql/how_to_retrieve_data.