Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Chapter 09 SQL SELECT Statement

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Chapter 09 SQL SELECT Statement

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

Chapter 9

SQL SELECT Statement


Structured Query Language (SQL)
• SQL (Structured Query Language)
– Pronounced as separate letters, "S" ̶ "Q" ̶ "L", not "sequel"
– A programming language for selecting and manipulating sets of data in
a relational database
– A nonprocedural language
 Focus is on input/output rather than on program steps
– Standardized by the American National Standards Institute (ANSI)
 Unfortunately, many vendors include proprietary SQL features that are not
part of the ANSI standard.
 This makes it difficult to move from one DBMS to another.
ANSI
• ANSI – American National Standards Institute
– Structured Query Language (SQL) is the industry-standard language of
relational database management systems (RDBMS)
– Originally designed by IBM in the mid 1970s
– Widespread use in the early 1980s
– Became an industry standard in 1986 when it was adopted by ANSI
• Three ANSI standardizations of SQL
– ANSI-86, ANSI-92, and ANSI-99

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:

• A keyword refers to an individual SQL command

– For example, SELECT and FROM are keywords

SELECT first_name, last_name


FROM employees;

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

SELECT employee_id, last_name - is a clause


FROM employees - is a clause

9
KEYWORD, CLAUSE, STATEMENT
Throughout this course, the following will be used:

• A SQL statement is a combination of two or more clauses

SELECT first_name, last_name


FROM employees;

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

SELECT first_name, last_name -- SELECT Clause


FROM employees; -- FROM 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

SELECT first_name, last_name


FROM employees;

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

– SELECT * FROM employees; -- All columns

13
SELECT Statement – FROM Clause

• FROM clause identifies tables and views

SELECT first_name, last_name


FROM employees;

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 )

• BODMAS Exponents2 Exponents2 POwer2 Indices2


Multiplication x Division ÷ Division ÷ Division ÷
• BIDMAS
Division ÷ Multiplication x Multiplication x Multiplication x
Addition + Addition + Addition + Addition +
Subtraction - Subtraction - Subtraction - Subtraction -

44
Arithmetic Order of Operations
• The order of operations is the order in which the DBMS
evaluates different operators in the same expression

() ^ * / + -

– Multiplication and division take priority over addition and subtraction


– Operators of the same priority (* /) (+ -) are evaluated from left to
right
– Parentheses or brackets are used to force the expression within
parentheses/brackets to be evaluated first
45
Arithmetic Order of Operations
• What's the answer:
• 15 x 2 / (5-3) – 23 + 4 x 2

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?

SELECT last_name, salary, 12 * weekly_salary + 100


FROM employees;

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

• Sometimes, the value for a column in a database table is not


known
• Relational databases use NULL values to represent unknown
values

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

• Depending upon the DBMS, NULL values may appear as NULL,


spaces, a dash (-), or other representation

52
NULL Values in Calculated Columns
SELECT salary, comm_pct, salary * comm_pct AS commission
FROM employees;

SALARY COMM_PCT COMMISSION


2500 (null) (null)
10500 .2 2100
11000 .3 3300
(null) .2 (null)
7000 .15 1050
(null) (null) (null)

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

You might also like