1

How to Retrieve Data From a Single Table

The Five Clauses of the SELECT statement

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

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


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

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

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

Code Example:

SELECT name, IndepYear
FROM country
WHERE IndepYear IS NULL;

Results:

is_null.png

BETWEEN Operators


Code Example:

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

Results:

between.png

The IN Keyword


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

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

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

CC BY-NC-ND International 4.0

CC BY-NC-ND International 4.0: This work is released under a CC BY-NC-ND International 4.0 license, which means that you are free to do with it as you please as long as you (1) properly attribute it, (2) do not use it for commercial gain, and (3) do not create derivative works.