CoverIntroduction1. How to Retrieve Data From a Single Table1.1. The Five Clauses of the SELECT Statement1.2. Column Specifications1.3. LIKE and REGEXP Operators1.4. Arithmetic Operators1.5. Column Aliases1.6. Comparison Operators1.7. IS NULL, BETWEEN, IN Operators1.8. AND, OR, NOT Logical Operators1.9. DISTINCT Clause2. How to Retrieve Data from Multiple Tables2.1. The JOIN Clause2.2. Joining More Than Two Tables2.3. The OUTER JOIN Clause2.4. How to Code a UNION3. Using Functions3.1. Date Functions3.2. Numeric Functions3.3. String Functions4. How to Insert, Update, Delete Data in Tables4.1. The INSERT Clause With a Column List4.2. The INSERT Clause Without a Column List4.4. The UPDATE Clause With a Column List4.4. The DELETE Clause5. Summary Queries and Aggregate Functions5.1. Aggregate Functions5.2. Grouping Data5.3. Simple GROUP BY Query5.4. Improving the GROUP BY Query5.5. Using the HAVING Clause5.5. Using the HAVING and WHERE Clauses Together5.6. COUNT(column_name) and COUNT(*)5.7. Using the DISTINCT Statement6. Working With Subqueries6.1. The Subquery In a SELECT Statement6.2. The Subquery in an UPDATE statement6.3. Create a Duplicate Table From An Existing Table 6.4. The Subquery In a Delete Statement7. SQL Views7.1. SQL View Explained7.2. Benefits of Using Views7.3. Views That Allow UPDATE Statements8. SQL Indexes8.1. SQL Indexes Explained8.2. Clustered vs. Non-clustered Indexes8.3. Create an Index in Workbench Using an ERD8.4. How to Manually Add an Index to an Existing TableGlossaryIndex
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:

Code Snippet
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

End-of-Chapter Survey

: How would you rate the overall quality of this chapter?
  1. Very Low Quality
  2. Low Quality
  3. Moderate Quality
  4. High Quality
  5. Very High Quality
Comments will be automatically submitted when you navigate away from the page.
Like this? Endorse it!