:
Retrieve Data using the SQL SELECT Statement
Agenda
• Capabilities of SQL SELECT Statements
• Arithmetic expressions and NULL values in the
SELECT statement
• Column aliases
• Use of concatenation operator, literal character strings,
and the DISTINCT keyword
• DESCRIBE command
Basic SELECT Statement
SELECT {* | [DISTINCT] column | expression [alias],...}
FROM table;
In its simplest form, a SELECT statement must include the following:
• A SELECT clause, which specifies the columns to be displayed
• A FROM clause, which identifies the table containing the columns that
are listed in the SELECT clause
In the syntax:
• SELECT - Is a list of one or more columns (* selects all columns)
• DISTINCT - Suppresses duplicates
• Column | expression - Selects the named column or the expression
• label - Gives different headings to the selected columns
• FROM table - Specifies the table containing the columns
Selecting Specific Columns
SELECT location_id, department_id
FROM departments;
• You can use the SELECT statement to display specific
columns of the table by specifying the column names,
separated by commas
SQL Statements
• SQL statements are not case sensitive
• SQL statements can be entered on one or more lines
• Keywords cannot be abbreviated or split across lines
• Clauses are usually placed on separate lines
• Indents are used to enhance readability
• In MySQL Workbench, SQL statements can be optionally
terminated by a semicolon (;). Semicolons are required when
you execute multiple SQL statements
Arithmetic expressions
An arithmetic expression can contain column names, constant
numeric values, and the arithmetic operators.
Arithmetic Operators:
• Add: +
• Subtract: -
• Multiply: *
• Divide: /
• An arithmetic expression can contain column names, constant
numeric values, and the arithmetic operators
• You can use arithmetic operators in any clause of a SQL statement
(except the FROM clause)
Using Arithmetic Operators
Operator Precedence
• If an arithmetic expression contains more than one operator,
multiplication and division are evaluated first. If operators in an
expression are of the same priority, evaluation is done from left to right
• You can use parentheses to force the expression that is enclosed by
the parentheses to be evaluated first
Example:
SELECT last_name, salary, 12*salary+commission_pct
FROM employees;
SELECT last_name, salary, 12*(salary+commission_pct)
FROM employees;
Null Value
• Null is a value that is unavailable, unassigned,
unknown, or inapplicable
• Null is not the same as zero or a blank space
SELECT job_id, salary, commission_pct
FROM employees;
• By default, MySQL Workbench uses the literal, (null), to identify
null values.
Null Values in Arithmetic Expressions
• Arithmetic expressions containing a null value evaluate
to null
SELECT last_name,12*(salary+commission_pct)
FROM employees;
• If any column value in an arithmetic expression is null, the result is
null. For example, if you attempt to perform division by zero, you
get an error. However, if you divide a number by null, the result is a
null or unknown
Defining a Column Alias
A column alias:
• Renames a column heading
• Is useful with calculations
• Immediately follows the column name (optional AS
keyword between the column name and the alias)
• Requires double quotation marks if it contains spaces
or special characters, or if it is case-sensitive
• An alias cannot be referenced in the column list that
contains the alias definition
Using Column Aliases
SELECT last_name AS name,commission_pct com
FROM employees;
SELECT last_name "Name" , salary*12 "Annual
Salary"
FROM employees;
Concatenation Operator
A concatenation operator:
• Links columns or character strings to other columns
• Is represented by concat function
• Creates a resultant column that is a character
expression
SELECT concat(last_name,’ ‘,job_id) AS
"Employees"
FROM employees;
Using Literal Character Strings
SELECT concat(last_name ,' is a ‘,job_id)
AS "Employee Details"
FROM employees;
Duplicate Rows
The default display of queries is all rows, including duplicate
rows.
SELECT department_id
FROM employees;
SELECT distinct department_id
FROM employees;
• The DISTINCT qualifier affects all the selected columns, and
the result is every distinct combination
• You may also specify the keyword UNIQUE, which is a
synonym for the keyword DISTINCT
Quiz
Identify the SELECT statements that execute successfully:
a) SELECT first_name, last_name, job_id, salary AS ‘yearly sal’
FROM employees;
b) SELECT first_name, last_name, job_id, salary AS yearly sal
FROM employees;
c) SELECT first_name, last_name, job_id, salary AS yearly_sal
FROM employees;
d) SELECT first_name, last_name job_id, salary AS “yearly_sal”
FROM employees;