Chapter 09 SQL SELECT Statement
Chapter 09 SQL SELECT Statement
3
SELECT Statement
• The SELECT statement returns a result set from the database
based on criteria defined in the query request
• A query is a request to retrieve a result set from the database
containing specific data according to user requirements
• A result set is all the data or a subset of data from the database
as a set of columns and rows determined by the criteria in the
SELECT statement
4
Accessing Data
5
Basic Format of SELECT Statement
6
Basic Format of SELECT Statement
7
KEYWORD, CLAUSE, STATEMENT
Throughout this course, the following will be used:
8
KEYWORD, CLAUSE, STATEMENT
Throughout this course, the following will be used:
• A clause is a meaningful logical unit of a SQL statement
• Usually optional, except for SELECT and FROM clauses
9
KEYWORD, CLAUSE, STATEMENT
Throughout this course, the following will be used:
is a SQL statement
10
Basic SELECT Statement
• In its simplest form, a SELECT statement must include:
– A SELECT clause, which specifies the columns to be returned
– A FROM clause, which specifies the table(s) containing the columns
listed in the SELECT clause
11
SELECT Clause Column-List
• SELECT clause contains a column-list that identifies the
column(s) to be returned in the result set
• Columns are listed one after another separated by a comma
12
SELECT Statement Column-List
• The column-list can contain:
– An asterisk (*) symbol that represents all columns in the table
– One or more column names, specified in any order
– Constants that are static values embedded in each row of the result set
– SQL functions and arithmetic operators used to return a calculated
value
13
SELECT Statement – FROM Clause
14
Selecting All Columns
with SELECT *
15
Selecting All Columns
Using Column-List
16
Selecting Specific Columns
Using Column-List
17
Selecting Specific Columns
in a Different Order
18
Selecting One Specific Column
19
Column Alias
• A temporary name assigned to a column in the column-list
– Only exists for the duration of the query
– Immediately follows the column name
– Renames a column heading in the result set
Display a column name that is easier to understand
Is useful with calculations
20
Column Alias
• Without aliases, column names are:
– Column names from the table in upper case
– A number or name showing an arithmetic operation such as
salary*commission_pct
SALARY COMMISSION_PCT 00003
SELECT salary,
commission_pct, 2500 (null) (null)
salary * commission_pct
10500 .2 2100
FROM employees;
11000 .3 3300
(null) .2 (null)
7000 .15 1050
(null) (null) (null) 21
Column Alias
22
AS Clause
• Optional AS keyword between the column name and alias
• Requires double quotation marks if the alias contains spaces or
special characters, or is case-sensitive
23
Column Alias Using
Optional AS Clause
24
Single Quotes
• Single Quotes(')
– Identify the beginning and ending of character and date constants
– A constant or literal is a character, numeric, or date value that is fixed
and never changes
– Characters and date constants are enclosed within single quotes
– Numeric constants are not included in quotes
• Examples of constants:
– Character constant: 'yearly salary'
– Numeric constant: 500
– Date constant: 'January 31, 2025' 25
Single Quotes
26
Double Quotes
• Double Quote(")
– Encloses identifiers that contain spaces, special symbols, like plus
(+) and negative (-) signs and are case sensitive
– An identifier is the name of an object such as tables, columns, and
alias names
– Double quotes are optional with table and column names and
rarely used
– Table and column names are stored in the database in upper case
27
Double Quotes
28
Double Quotes
29
Double Quotes
30
Concatenation Operator (||)
• Concatenation means to connect two items together
• Two methods of concatenation:
– Concatenation operator uses two vertical bars ( || ), referred to as
"pipes"
– CONCAT function discussed later with SQL functions
• Values on either side of the vertical bars (||) are joined together
resulting in a character string
• Each value can be a column value, arithmetic expression, or
constant value
31
Concatenation Operator (||)
32
Concatenation with Constants
33
Concatenation with Constants/Literal Values
• Constant/literal values can be included in the SELECT column-list
with the concatenation operator
• Every row returned from a query with constant/literal values will
have the same character string in it
• Consider the example on next slide
34
Concatenation and Constant/Literal Values
35
SQL Statement Line Spacing
36
Using the Correct Syntax
• Syntax is the set of rules used to create SQL statements, which
includes the correct spelling and arrangement of keywords
37
Using the Correct Syntax
Correct Spelling on Keyword
• The following statement has a spelling error:
38
Using the Correct Syntax
Correct Spelling on Table & Column Names
39
Computed Columns
• A computed column is:
– A column that does not exist in the database
– Usually, a calculated value using data from existing columns in the
database
• Computed columns do not create new columns or modify data
in existing column in the database
• The result of a calculations in the SELECT statement only appear
in the result set
40
Calculated Columns using Arithmetic Operators
• Arithmetic operators are used to perform calculations on
numeric values:
41
Calculated Columns
using Arithmetic Operators
42
Calculated Columns
using Arithmetic Operators
43
Arithmetic Order of Operations – Which is it?
• PEMDAS PEMDAS BEDMAS BODMAS BIDMAS
• BEDMAS ( Parentheses ) ( Brackets ) ( Brackets ) ( Brackets )
44
Arithmetic Order of Operations
• The order of operations is the order in which the DBMS
evaluates different operators in the same expression
() ^ * / + -
46
Arithmetic Order of Operations
15
47
Using Arithmetic Operators
• Employees are paid a weekly salary. Calculate the new annual
salary by giving employees a 100.00 raise each month on their
weekly salary
• Is this correct?
48
Solution
49
NULL Values in a Calculated Column
• What is a NULL value?
50
NULL Values
• A null is a value that is unavailable, unassigned, or unknown
– NULL is not the same as zero. Zero is a number
– NULL is not a space. Space is a character
51
NULL Values in a Calculated Column
• If any column value in an arithmetic expression is NULL, the
result is NULL
• Dividing by NULL, results is NULL
• Dividing by zero, returns a divide by zero error
52
NULL Values in Calculated Columns
SELECT salary, comm_pct, salary * comm_pct AS commission
FROM employees;
53
Using DISTINCT to Eliminate Duplicate Rows
• Many times, you may want to know how many unique instances
of something exist
• For example, what if the company wanted a list of cities where
they ship products to customers
54
List the cities the company ships to
Does this give us what we want?
55
Using DISTINCT to Eliminate Duplicate Rows
• DISTINCT - used in the column-list to eliminate duplicate values
• As a result, there are fewer rows returned
• The keyword DISTINCT must appear directly after the SELECT
keyword
• The DISTINCT qualifier affects all the columns listed in the
column-list and returns every distinct combination of the
columns in the SELECT clause
56
Using DISTINCT
to Eliminate
Duplicate Rows
57
58