Selecting Rows

advertisement
Selecting Rows
Objectives
• Write a SELECT statement to query the
database.
• Perform arithmetic calculations.
• Handle null values.
• Specify alternative column headings using
aliases.
• Concatenate columns.
1-2
The Basic Query Block
SELECT [DISTINCT]
FROM
table;
{*,column [alias],..}
• SELECT identifies what columns
• FROM identifies which table
1-3
Selecting All Columns, All Rows
SELECT
ID
-------10
31
32
33
34
35
41
42
43
44
45
50
*
s_dept;
NAME
REGION_ID
------------- ---------Finance
1
Sales
1
Sales
2
Sales
3
Sales
4
Sales
5
Operations
1
Operations
2
Operations
3
Operations
4
Operations
5
Administration
1
12 rows selected.
1-4
FROM
Selecting Specific Columns
SELECT dept_id,last_name,manager_id
FROM
s_emp;
• Specify columns in the order
you want them to appear.
1-5
Arithmetic Expressions
Create expressions on NUMBER and
DATE datatypes by using operators.
• Add
• Subtract
• Multiply
• Divide
1-6
+
*
/
Arithmetic Expressions
SELECT last_name, salary * 12, commission_pct
FROM
s_emp;
LAST_NAME
SALARY*12 COMMISSION_PCT
------------ ------------ -------------...
Havel
15684
Magee
16800
10
Giljum
17880
12.5
Sedeghi
18180
10
Nguyen
18300
15
Dumas
17400
17.5
Maduro
16800
...
1-7
Operator Precedence
Parentheses change the order in which a statement
is evaluated.
SELECT last_name, salary, 12 * salary + 100
FROM
s_emp;
... Velasquez 2500 30100
SELECT last_name, salary, 12 * (salary + 100)
FROM
s_emp;
... Velasquez 2500 31200
1-8
Column Aliases
A column alias renames a column.
• Especially useful with
calculations
• Immediately follows column
name
–Optional AS keyword between
column name and alias
1-9
Column Aliases
SELECT last_name, salary * 12 as soso, commission
FROM
s_emp;
LAST_NAME
soso
COMMISSION
------------ ------------ -------------...
Havel
15684
Magee
16800
10
Giljum
17880
12.5
Sedeghi
18180
10
Nguyen
18300
15
Dumas
17400
17.5
Maduro
16800
...
1-10
Concatenation Operator
The concatenation operator
• Is represented by two vertical bars (||).
• Links columns or character strings to
other columns.
• Creates a resultant column that is a
character expression.
1-11
Concatenation Operator: Example
Display the full names of the employees with the
heading Employees.
SELECT
FROM
first_name||last_name
s_emp;
Employees
Employees
------------------------------------------------CarmenVelasquez
LaDorisNgao
MidoriNagayama
MarkQuick-To-See
AudryRopeburn
MollyUrguhart
...
1-12
Literal Character String: Example
SELECT first_name ||' '|| last_name
||', '|| title Employees
FROM
s_emp;
Employees
-------------------------------------------Carmen Velasquez, President
LaDoris Ngao, VP, Operations
Midori Nagayama, VP, Sales
Mark Quick-To-See, VP, Finance
Audry Ropeburn, VP, Administration
Molly Urguhart, Warehouse Manager
...
1-13
Duplicate Rows
•
The default display of queries is all rows
including duplicate rows.
SELECT
•
name
FROM
s_dept;
Eliminate duplicate rows by using DISTINCT in
the SELECT clause.
SELECT DISTINCT name FROM
1-14
s_dept;
DISTINCT with Multiple Columns
• DISTINCT applies to all columns in the
SELECT list.
SELECT DISTINCT dept_id, title
FROM
s_emp;
• When DISTINCT is applied to multiple
columns, the result represents the
distinct combination of the columns.
1-15
Download