Sie sind auf Seite 1von 25

Chapter 3

Writing Basic SQL Statements

CS165 Structured Query Language

Objectives
Understand

the capabilities of SQL

statements
Construct a simple SELECT statement
Using SELECT clause to select field(s) to be
displayed
Understands the usage of arithmetic
operators
Usefulness of column aliases and
concatenation operator
Learn to remove duplicate rows in the result
CS165 Structured Query Language

Capabilities of SQL SELECT


Statements
Restriction

Projection

Table 1

Table 1

Join

Table 1

Table 2
CS165 Structured Query Language

Basic SELECT statement syntax


SELECT
FROM
[WHERE
[GROUP BY
[ORDER BY

[DISTINCT] {*, column AS[alias],...}


table
condition(s)]
group_by_expression]
column];

SELECT identifies

the columns to be

displayed.
FROM identifies the table that contains the
columns.
CS165 Structured Query Language

Writing SQL Statements


Place

a semicolon (;) at the end of the last clause.


SQL statements are not case sensitive.
SQL statements can be on one or
more lines.
Keywords cannot be abbreviated or split across
lines.
Clauses are usually placed on
separate lines.
Tabs and indents are used to enhance readability.
CS165 Structured Query Language

SELECT clause
The

SELECT clause is the first part of a


query.
The SELECT clause says :
which

columns of information you want


what order you want them in
what you want them to be called.

CS165 Structured Query Language

SELECTING fields
Select All

Fields

SELECT *
FROM course;
Output:
CourseDesp

CourseID

MentorID

DCS

Diploma In Computer Studies

1006

DGAT

Diploma in Gaming and Animation Techniques

1004

DIC

Diploma In Computing

1006

DICT

Diploma In Info-Comm Technology

1002

DIT

Diploma In Information Technology

1001

DNC

Diploma in Network and CyberSecurity

1003

CS165 Structured Query Language

SELECTING fields
Select

One Field ( Projection)

SELECT CourseDesp
FROM course;
Output:
CourseDesp
Diploma In Computer Studies
Diploma in Gaming and Animation Techniques
Diploma In Computing
Diploma In Info-Comm Technology
Diploma In Information Technology
Diploma in Network and CyberSecurity

CS165 Structured Query Language

SELECTING fields
Select

More Than One Field

SELECT LastName, GPA


FROM student;
Output:
LastName

GPA

Bartell

3.21

Kebel

2.71

Lee

3.82

Lewis

2.51

Law

3.05

Mikulski

1.89

Take note on the ordering


of the column

.
.
Chan

3.12

Jann

CS165 Structured Query Language


3.76

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

Operator

Description

Add

Subtract

Multiply

Divide

CS165 Structured Query Language

10

Using Arithmetic Operators


SELECT LastName, DateEnrolled, GPA + 0.05
FROM
student;

Output:
Lastname

DateEnrolled

Expr1002

Bartell

15-Feb-02

3.26

Kebel

23-Jun-01

2.76

Lee

05-Jan-02

3.87

Lewis

03-Mar-00

2.56

Law

01-Apr-01

3.1

Note: The column name


after applying arithmetic

...
CS165 Structured Query Language

11

Operator Precedence
* / +

Multiplication

and division take priority over


addition and subtraction.
Operators of the same priority are
evaluated from left to right.
Parentheses are used to force prioritized
evaluation and to clarify statements.

CS165 Structured Query Language

12

Operator Precedence
SELECT LastName, DateEnrolled, GPA * 4 + 0.05
FROM student;
Output:
LastName

DateEnrolled

Expr1002

Bartell

15-Feb-02

12.89

Kebel

23-Jun-01

10.89

Lee

05-Jan-02

15.33

Lewis

03-Mar-00

10.09

Law

01-Apr-01

12.25

Mikulski

12-Sep-03

7.61

Tham

19-Sep-03

15.61

..
CS165 Structured Query Language

13

Using Parenthesis
SELECT LastName, DateEnrolled, (GPA + 0.05) * 4
FROM student;
Output:
LastName

DateEnrolled

Expr1002

Bartell

15-Feb-02

13.04

Kebel

23-Jun-01

11.04

Lee

05-Jan-02

15.48

Lewis

03-Mar-00

10.24

Law

01-Apr-01

12.4

Mikulski

12-Sep-03

7.76

Tham

19-Sep-03

15.76

CS165 Structured Query Language

14

Defining a Column Alias


Renames

a column heading
Is useful with calculations
Immediately follows column name; MUST
have AS keyword between column name
and alias
Requires square brackets ( [ ] ) if it is
contains spaces or special characters
The alias case will follow exactly what is
entered.
CS165 Structured Query Language

15

Using Column Aliases


Example 1
SELECT LastName, DateEnrolled, GPA + 0.05 AS NEWGPA
FROM student;
Output:
LastName

DateEnrolled

NEWGPA

Bartell

15-Feb-02

3.26

Kebel

23-Jun-01

2.76

Lee

05-Jan-02

3.87

Lewis

03-Mar-00

2.56

Law

01-Apr-01

3.1

Mikulski

12-Sep-03

1.94

Tham

19-Sep-03

3.94

CS165 Structured Query Language

16

Using Column Aliases


Example 2
SELECT LastName AS [Family Name], DateEnrolled,
GPA + 0.05 AS [New GPA]
FROM student;
Output:

Family Name

DateEnrolled

New GPA

Bartell

15-Feb-02

3.26

Kebel

23-Jun-01

2.76

Lee

05-Jan-02

3.87

Lewis

03-Mar-00

2.56

Law

01-Apr-01

3.1

Mikulski

12-Sep-03

1.94

Tham

19-Sep-03

3.94

CS165 Structured Query Language

17

Concatenation Operator
Concatenates

columns or character strings


to other columns
Is represented by an ampersand &
Creates a result column that is a character
expression
As a result it will join one or more column
become a single-column

CS165 Structured Query Language

18

Using Concatenation Operator


SELECT LastName & GPA AS [Student GPA]
FROM student;
Output:
Student GPA
Bartell3.21
Kebel2.71
Lee3.82
Lewis2.51
Law3.05
Mikulski1.89
Tham3.89

..
CS165 Structured Query Language

19

Literals
A literal

is a constant value of character,


expression, or number that can be included
in the SELECT list.
Date and character literal values must be
enclosed in single quotation marks.
Each character string is outputted once for
each row returned.

CS165 Structured Query Language

20

Using Literal Character String


SELECT LastName & ' earns ' & GPA & ' points ' AS
[Student GPA]
FROM student;
Output:
Student GPA
Bartell earns 3.21 points
Kebel earns 2.71 points
Lee earns 3.82 points
Lewis earns 2.51 points
Law earns 3.05 points
Mikulski earns 1.89 points
Tham earns 3.89 points

CS165 Structured Query Language

21

Duplicate Rows
SELECT CourseID
FROM student;
Output:

CourseID
DCS
DIC
DIT
DICT

Duplicate
rows

DIT
DCS
DCS
DIC
DICT
DIC
DIT
DICT
DIT
DCS
DIC
DNC

CS165 Structured Query Language

22

Eliminating Duplicate Rows(1)


Eliminate

duplicate rows by using the


DISTINCT keyword in the SELECT clause.
Only can be applied to one column to find
all of its values and list each of them only
once.

CS165 Structured Query Language

23

Eliminating Duplicate Rows(2)


SELECT Distinct CourseID
FROM student;
Output:
CourseID
DCS
DIC
DICT
DIT
DNC

CS165 Structured Query Language

24

Summary
Select

field(s) to be displayed in a SQL


statement
Performed calculation and to rename a
column
Retrieve unique record from a table

CS165 Structured Query Language

25

Das könnte Ihnen auch gefallen