0% found this document useful (0 votes)
4 views

CIT365 Lab Practice Tutorial 01 Basic SQL Statements(1)

This document provides an overview of basic SQL statements, focusing on the capabilities of SQL SELECT statements and how to execute them. It covers selecting all or specific columns, using arithmetic expressions, defining null values, and eliminating duplicate rows. Additionally, it includes instructions for displaying table structures and utilizing SQL*Plus for executing and editing SQL statements.

Uploaded by

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

CIT365 Lab Practice Tutorial 01 Basic SQL Statements(1)

This document provides an overview of basic SQL statements, focusing on the capabilities of SQL SELECT statements and how to execute them. It covers selecting all or specific columns, using arithmetic expressions, defining null values, and eliminating duplicate rows. Additionally, it includes instructions for displaying table structures and utilizing SQL*Plus for executing and editing SQL statements.

Uploaded by

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

1

Writing Basic
SQL Statements

Copyright  Oracle Corporation, 1999. All rights reserved.


Objectives

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

1-2 Copyright  Oracle Corporation, 1999. All rights reserved.


Capabilities of SQL SELECT
Statements
Selection Projection

Table 1 Table 1
Join

Table 1 Table 2
1-3 Copyright  Oracle Corporation, 1999. All rights reserved.
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.

1-4 Copyright  Oracle Corporation, 1999. All rights reserved.


Writing SQL Statements

•• SQL
SQL statements
statements are
are not
not case
case sensitive.
sensitive.
•• SQL
SQL statements
statements can
can be
be on
on one
one or
or
more
more lines.
lines.
•• Keywords
Keywords cannot
cannot be
be abbreviated
abbreviated oror split
split
across
across lines.
lines.
•• Clauses
Clauses areare 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.
1-5 Copyright  Oracle Corporation, 1999. All rights reserved.
Selecting All Columns

SQL> SELECT *
2 FROM dept;

DEPTNO DNAME LOC


--------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

1-6 Copyright  Oracle Corporation, 1999. All rights reserved.


Selecting Specific Columns

SQL> SELECT deptno, loc


2 FROM dept;

DEPTNO LOC
--------- -------------
10 NEW YORK
20 DALLAS
30 CHICAGO
40 BOSTON

1-7 Copyright  Oracle Corporation, 1999. All rights reserved.


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

1-8 Copyright  Oracle Corporation, 1999. All rights reserved.


Arithmetic Expressions
Create
Create expressions
expressions on
on NUMBER
NUMBER andand DATE
DATE
data
data by
by using
using arithmetic
arithmetic operators.
operators.

Operator Description

+ Add

- Subtract

* Multiply

/ Divide

1-9 Copyright  Oracle Corporation, 1999. All rights reserved.


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.

1-10 Copyright  Oracle Corporation, 1999. All rights reserved.


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 areare used
used toto force
force
prioritized
prioritized evaluation
evaluation and
and toto clarify
clarify
statements.
statements.

1-11 Copyright  Oracle Corporation, 1999. All rights reserved.


Operator Precedence

SQL> SELECT ename, sal, 12*sal+100


2 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.

1-12 Copyright  Oracle Corporation, 1999. All rights reserved.


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.

1-13 Copyright  Oracle Corporation, 1999. All rights reserved.


Defining a Null Value
•• A
A null
null is
is aa value
value that
that is
is unavailable,
unavailable,
unassigned,
unassigned, unknown,
unknown, oror inapplicable.
inapplicable.
•• A
A null
null is
is not
not the
the same
same asas zero
zero or
or aa blank
blank
space.
space.
SQL> SELECT ename, job, sal, comm
2 FROM emp;

ENAME JOB SAL COMM


---------- --------- --------- ---------
KING PRESIDENT 5000
BLAKE MANAGER 2850
...
TURNER SALESMAN 1500 0
...
14 rows selected.
1-14 Copyright  Oracle Corporation, 1999. All rights reserved.
Null Values
in Arithmetic Expressions
Arithmetic
Arithmetic expressions
expressions containing
containing aa null
null
value
value evaluate
evaluate to
to null.
null.

SQL> select ename, 12*sal+comm


2 from emp
3 WHERE ename='KING';

ENAME 12*SAL+COMM
---------- -----------
KING

1-15 Copyright  Oracle Corporation, 1999. All rights reserved.


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 if
if it
it
contains
contains spaces
spaces or or special
special characters
characters
or
or is
is case
case sensitive
sensitive

1-16 Copyright  Oracle Corporation, 1999. All rights reserved.


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


------------- -------------
...
1-17 Copyright  Oracle Corporation, 1999. All rights reserved.
Concatenation Operator

•• Concatenates
Concatenates columns
columns or
or character
character
strings
strings to
to other
other columns
columns

•• Is
Is represented
represented by
by two
two vertical
vertical bars
bars (||)
(||)

•• Creates
Creates aa resultant
resultant column
column that
that is
is aa
character
character expression
expression

1-18 Copyright  Oracle Corporation, 1999. All rights reserved.


Using the Concatenation
Operator
SQL> SELECT ename||job AS "Employees"
2 FROM emp;

Employees
-------------------
KINGPRESIDENT
BLAKEMANAGER
CLARKMANAGER
JONESMANAGER
MARTINSALESMAN
ALLENSALESMAN
...
14 rows selected.

1-19 Copyright  Oracle Corporation, 1999. All rights reserved.


Literal Character Strings
•• A
A literal
literal is
is aa character,
character, aa number,
number, or
or aa
date
date included
included in in 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 is
is output
output once
once for
for
each
each row
row returned.
returned.

1-20 Copyright  Oracle Corporation, 1999. All rights reserved.


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.

1-21 Copyright  Oracle Corporation, 1999. All rights reserved.


Duplicate Rows
The
The default
default display
display of
of queries
queries is
is all
all rows,
rows,
including
including duplicate
duplicate rows.
rows.
SQL>
SQL> SELECT
SELECT deptno
deptno
22 FROM
FROM emp;
emp;

DEPTNO
---------
10
30
10
20
...
14 rows selected.

1-22 Copyright  Oracle Corporation, 1999. All rights reserved.


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

1-23 Copyright  Oracle Corporation, 1999. All rights reserved.


Displaying Table Structure

Use
Use the
the SQL*Plus
SQL*Plus DESCRIBE
DESCRIBE command
command to
to
display
display the
the structure
structure of
of aa table.
table.
DESC[RIBE]
DESC[RIBE] tablename
tablename

1-24 Copyright  Oracle Corporation, 1999. All rights reserved.


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)

1-25 Copyright  Oracle Corporation, 1999. All rights reserved.


Summary

SELECT
SELECT [DISTINCT]
[DISTINCT] {*,column
{*,column [alias],...}
[alias],...}
FROM
FROM table;
table;

Use
Use SQL*Plus
SQL*Plus as
as an
an environment
environment to:
to:
•• Execute
Execute SQL
SQL statements
statements
•• Edit
Edit SQL
SQL statements
statements

1-26 Copyright  Oracle Corporation, 1999. All rights reserved.


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

1-27 Copyright  Oracle Corporation, 1999. All rights reserved.

You might also like