0% found this document useful (0 votes)
19 views26 pages

Ch 1 Basic SQL Statement

Uploaded by

abdulrehman1gis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views26 pages

Ch 1 Basic SQL Statement

Uploaded by

abdulrehman1gis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Writing Basic

SQL Statements
Objectives
• After completing this lesson, you
should be able to do the following:
–– List
List the
the capabilities
capabilities of
of SQL
SQL SELECT
SELECT
statements
statements
–– Execute
Execute aa basic
basic SELECT
SELECT statement
statement
Capabilities of SQL SELECT
Selection
Statements
Projection

Table 1 Table 1
Join

Table 1 Table 2
Basic SELECT Statement
SELECT
SELECT [DISTINCT]
[DISTINCT] {*,
{*, column
column [alias],...}
[alias],...}
FROM
FROM table;
table;

–– SELECT
SELECT identifies
identifies what
what columns.
columns.
–– FROM
FROM identifies
identifies which
which table.
table.
Writing SQL Statements
–– SQL
SQL statements
statements are
are not
not case
case sensitive.
sensitive.
–– SQL
SQL statements
statements can
can bebe on
on one
one or
or
more
more lines.
lines.
–– Keywords
Keywords cannot
cannot be
be abbreviated
abbreviated or
or split
split
across
across lines.
lines.
–– Clauses
Clauses are are usually
usually placed
placed on
on separate
separate
lines.
lines.
–– Tabs
Tabs and
and indents
indents are
are used
used to
to enhance
enhance
readability.
readability.
Selecting All Columns
SQL> SELECT *
2 FROM dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Selecting Specific Columns
SQL> SELECT deptno, loc
2 FROM dept;

DEPTNO LOC
--------- -------------
10 NEW YORK
20 DALLAS
30 CHICAGO
40 BOSTON
Column Heading Defaults
–– Default
Default justification
justification
•• Left:
Left: Date
Date and
and character
character data
data
•• Right:
Right: Numeric
Numeric data
data
–– Default
Default display:
display: Uppercase
Uppercase
Arithmetic Expressions
• Create expressions on NUMBER and
DATE data by using arithmetic
operators.
Operator Description

+ Add

- Subtract

* Multiply

/ Divide
Using Arithmetic Operators
SQL> SELECT ename, sal, sal+300
2 FROM emp;

ENAME SAL SAL+300


---------- --------- ---------
KING 5000 5300
BLAKE 2850 3150
CLARK 2450 2750
JONES 2975 3275
MARTIN 1250 1550
ALLEN 1600 1900
...
14 rows selected.
Operator Precedence
_
* / +
–– Multiplication
Multiplication and
and division
division take
take priority
priority
over
over addition
addition and
and subtraction.
subtraction.
–– Operators
Operators ofof the
the same
same priority
priority are
are
evaluated
evaluated from
from left
left to
to right.
right.
–– Parentheses
Parentheses are are used
used to to force
force prioritized
prioritized
evaluation
evaluation and
and to
to clarify
clarify statements.
statements.
Operator Precedence
SQL> SELECT ename, sal, 12*sal+100
FROM emp;

ENAME SAL 12*SAL+100


---------- --------- ----------
KING 5000 60100
BLAKE 2850 34300
CLARK 2450 29500
JONES 2975 35800
MARTIN 1250 15100
ALLEN 1600 19300
...
14 rows selected.
Using Parentheses
SQL> SELECT ename, sal, 12*(sal+100)
2 FROM emp;

ENAME SAL 12*(SAL+100)


---------- --------- -----------
KING 5000 61200
BLAKE 2850 35400
CLARK 2450 30600
JONES 2975 36900
MARTIN 1250 16200
...
14 rows selected.
Defining a Null Value
–– A
A null
null is
is aa value
value that
that is
is unavailable,
unavailable,
unassigned,
unassigned, unknown,
unknown, or or inapplicable.
inapplicable.
–– A
A null
null is
is not
not the
the same
same as as zero
zero or
or aa blank
blank
space.
space.
SQL> SELECT ename, job, comm
2 FROM emp;

ENAME JOB COMM


---------- --------- ---------
KING PRESIDENT
BLAKE MANAGER
...
TURNER SALESMAN 0
...
14 rows selected.
Null Values
in Arithmetic Expressions
• Arithmetic expressions containing a
null value evaluate to null.
SQL> select ename, 12*sal+comm
2 from emp
3 WHERE ename='KING';

ENAME 12*SAL+COMM
---------- -----------
KING
Defining a Column Alias
–– Renames
Renames aa column
column heading
heading
–– Is
Is useful
useful with
with calculations
calculations
–– Immediately
Immediately follows
follows column
column name;
name;
optional
optional AS
AS keyword
keyword between
between column
column
name
name and
and alias
alias
–– Requires
Requires double
double quotation
quotation marks
marks ifif itit
contains
contains spaces
spaces or or special
special characters
characters or or
is
is case
case sensitive
sensitive
Using Column Aliases
SQL> SELECT ename AS name, sal salary
2 FROM emp;

NAME SALARY
------------- ---------
...

SQL> SELECT ename "Name",


2 sal*12 "Annual Salary"
3 FROM emp;

Name Annual Salary


------------- -------------
...
Concatenation Operator
–– Concatenates
Concatenates columns
columns oror character
character
strings
strings to
to other
other columns
columns
–– Is
Is represented
represented byby two
two vertical
vertical bars
bars (||)
(||)
–– Creates
Creates aa resultant
resultant column
column that
that is
is aa
character
character expression
expression
Using the Concatenation Operator

SQL> SELECT ename||job AS "Employees"


2 FROM emp;

Employees
-------------------
KINGPRESIDENT
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER
MARTINSALESMAN
ALLENSALESMAN
...
14 rows selected.
Literal Character Strings
–– A
A literal
literal is
is aa character,
character, expression,
expression, oror
number
number included
included inin the
the SELECT
SELECT list.
list.
–– Date
Date and
and character
character literal
literal values
values must
must
be
be enclosed
enclosed within
within single
single quotation
quotation
marks.
marks.
–– Each
Each character
character string
string isis output
output once
once forfor
each
each row
row returned.
returned.
Using Literal Character
Strings
SQL> SELECT ename ||' '||'is a'||' '||job
2 AS "Employee Details"
3 FROM emp;

Employee
Employee Details
Details
-------------------------
-------------------------
KING
KING is
is aa PRESIDENT
PRESIDENT
BLAKE
BLAKE is
is aa MANAGER
MANAGER
CLARK
CLARK is
is aa MANAGER
MANAGER
JONES
JONES is
is aa MANAGER
MANAGER
MARTIN
MARTIN is
is aa SALESMAN
SALESMAN
...
...
14
14 rows
rows selected.
selected.
Duplicate Rows
• The default display of queries is all
rows, including duplicate rows.
SQL>
SQL> SELECT
SELECT deptno
deptno
22 FROM
FROM emp;
emp;

DEPTNO
---------
10
30
10
20
...
14 rows selected.
Eliminating Duplicate Rows
Eliminate duplicate rows by using the
DISTINCT keyword in the SELECT clause.
SQL> SELECT DISTINCT deptno
2 FROM emp;

DEPTNO
---------
10
20
30
Displaying Table Structure
• Use the SQL*Plus DESCRIBE
command to display the structure of a
table.
DESC[RIBE]
DESC[RIBE] tablename
tablename
Displaying Table Structure
SQL>
SQL> DESCRIBE
DESCRIBE dept
dept

Name
Name Null?
Null? Type
Type
-----------------
----------------- --------
-------- ------------
------------
DEPTNO
DEPTNO NOT
NOT NULL
NULL NUMBER(2)
NUMBER(2)
DNAME
DNAME VARCHAR2(14)
VARCHAR2(14)
LOC
LOC VARCHAR2(13)
VARCHAR2(13)
Practice Overview
–– Selecting
Selecting all
all data
data from
from different
different tables
tables
–– Describing
Describing the
the structure
structure of
of tables
tables
–– Performing
Performing arithmetic
arithmetic calculations
calculations and
and
specifying
specifying column
column names
names
–– Using
Using SQL*Plus
SQL*Plus editor
editor

You might also like