• Tidak ada hasil yang ditemukan

Relational Database Management Systems for Epidemiologists: SQL Part I

N/A
N/A
Protected

Academic year: 2019

Membagikan "Relational Database Management Systems for Epidemiologists: SQL Part I"

Copied!
51
0
0

Teks penuh

(1)

Relational Database Management

Systems for Epidemiologists:

(2)

Outline

SQL Basics

Retrieving Data from a Table

(3)

What is SQL?

SQL is the standard programming language to create, update, delete, and retrieve data stored in a RDMS.

SQL is defined by rules of syntax (words and symbols allowed) and semantics (meaning).

Three versions:

ANSI-89 SQL ANSI-92 SQL

(4)

SQL Syntax

SELECT fname, lname

FROM casetble

WHERE status='alive'

ORDER BY lname, fname;

(5)

SQL Syntax

SELECT fname, lname

FROM casetble

WHERE status='alive'

ORDER BY lname, fname;

Clauses

Columns Tables

(6)

SQL Syntax

SELECT fname, lname

FROM casetble

WHERE status='alive'

ORDER BY lname, fname;

(7)

SQL Syntax

SELECT fname, lname

FROM casetble

WHERE status='alive'

ORDER BY lname, fname;

(8)

SQL Syntax

SELECT fname, lname

FROM casetble

WHERE status='alive'

ORDER BY lname, fname;

(9)

Elements of SQL Style

SQL statements

can be in uppercase or lowercase (case insensitive). can extend across multiple lines, so long as you do not split words or quoted strings in two.

To improve readability and maintenance,

Begin each SQL statement on a new line.

Use uppercase for SQL keywords (eg, SELECT, NULL, CHARACTER).

(10)

SELECT

SELECT retrieves rows, columns, and derived values from one or more tables.

(11)

SELECT Example

SELECT fname, lname

(12)

SELECT Example

SELECT *

(13)

AS

The AS statement can be used to create a column alias (an alternative name/identifier) that you

specify to control how column headings are displayed in a result.

Syntax:

SELECT column1 AS alias1, column2 AS alias2, ...

(14)

AS Example

SELECT fname AS “First Name”, lname AS “Last Name”

(15)

DISTINCT

Results of queries oftentimes contain duplicate values for a particular column.

The DISTINCT keyword eliminates duplicate rows from a result.

Syntax:

(16)

DISTINCT Example

SELECT DISTINCT fname, lname

(17)

Notes on DISTINCT

If the SELECT DISTINCT clause contains more than one column, the values of all the columns specified determined the uniqueness of rows.

All NULLS are considered duplicates of each other (even though they technically never equal each other since the values are unknown).

(18)

ORDER BY

Rows in a query result are unordered and should be viewed as arbitrary.

The ORDER BY clause can be used to sort rows by a specified column (or columns) in

ascending/descending order.

Syntax:

SELECT DISTINCT column(s) FROM table

ORDER BY sort_column1 [ASC | DESC],

...

(19)

ORDER BY Example

SELECT fname, lname, city, state

FROM patients

ORDER BY state ASC,

(20)

ORDER BY Example

SELECT fname, lname, city, state

FROM patients

ORDER BY 4 ASC,

3 DESC;

(21)

ORDER BY Example

SELECT fname AS “First Name”, lname AS “Last Name”,

(22)

Notes on ORDER BY

The default for sorting is ascending order.

The sorting column(s) do not need to appear in the resulting query.

If the ORDER BY columns do not identify each row uniquely, rows with duplicate values will be listed in arbitrary order.

A DBMS uses a collating sequence (or collation) to determine the order in which characters are

(23)

WHERE

The WHERE clause can be used to filter

unwanted rows in a result (ie, yield a subset of all rows in the result with a specified condition).

Syntax:

SELECT column(s) FROM table

(24)

Types of Conditions

Condition SQL Operators

Comparison =, <>, <, <=, >, >=

Pattern Matching LIKE

Range Filtering BETWEEN

List Filtering IN

(25)

Comparison Operators

Operator Descriptors

= Equal to

<> Not equal to

< Less than

<= Less than or equal to

> Greater than

(26)

WHERE Example

SELECT caseid, fname, lname

FROM patients

(27)

WHERE Example

SELECT caseid, fname, lname

FROM patients

(28)

WHERE Example

SELECT caseid, fname, lname

FROM patients

(29)

WHERE Example

SELECT caseid,

fname, lname,

MONTH(datevar) AS “Month”, DAY(datevar) AS “Day”

FROM patients

(30)

Notes on WHERE

Occasionally, you may need to specify multiple conditions in a single WHERE clause.

You can use the AND, OR or NOT operators to combine two or more conditions into a compound condition.

AND, OR, and NOT operators are known as Boolean operators; they are designed to work with “truth”

(31)

AND Example

SELECT caseid,

fname, lname,

MONTH(datevar) AS “Month”

FROM patients

(32)

Truth Table for Two Conditions

AND TRUE FALSE UNKNOWN TRUE TRUE FALSE UNKNOWN FALSE FALSE FALSE FALSE

(33)
(34)

Truth Table for Two Conditions

OR TRUE FALSE UNKNOWN

TRUE TRUE TRUE TRUE

(35)

NOT Example

SELECT caseid,

fname, lname,

MONTH(datevar) AS “Month”

FROM patients

(36)

NOT Example

SELECT caseid,

fname, lname,

MONTH(datevar) AS “Month”

FROM patients

(37)

NOT Truth Table

Condition NOT Condition TRUE FALSE

(38)

Equivalent Conditions

This condition Is equivalent to:

NOT (p AND q) (NOT p) AND (NOT q)

NOT (p OR q) (NOT p) OR (NOT q)

(39)

Order of Evaluation

When you use multiple logical operators in a compound condition, the order of evaluation is:

(1) NOT

(40)

LIKE

You can use the LIKE operator to retrieve partial information for a character string (not numbers or date/times) rather than an exact value.

LIKE uses a pattern that values are matched against.

Pattern: quoted string that contains the literal characters to match and any combination of wildcards.

Wildcards: special characters used to match parts of a value.

(41)

Wildcard Operators

Operator Matches %

_ Any one character

(42)
(43)

BETWEEN

Use the BETWEEN clause to determine whether a given value falls within a specified range.

BETWEEN works with character strings, numbers, and date/times.

The range contains a low and high value, separated by AND (inclusive).

(44)
(45)

Equivalent Statements

SELECT fname, lname

FROM patients

WHERE zip BETWEEN 94510 AND 94515;

SELECT fname, lname

FROM patients

(46)

List Filtering with IN

The IN clause can be used to display records with any value in a specified list for a particular

(47)

IN Example

Example:

SELECT fname, lname, state FROM patients

(48)

IS NULL

Recall: NULLs represent missing or unknown values.

IS NULL can be used to find records with NULL values

Syntax:

SELECT columns

FROM table

(49)

IS NULL Example

SELECT caseid,

fname, lname

FROM patients

(50)

Additional Operators & Functions

Arithmetic Operations (+. -, *, /) Concatenation (||)

Extracting Text (SUBSTRING())

Changing Case (UPPER() and LOWER()) Trimming Characters (TRIM())

Length of a String (CHARACTER_LENGTH() or LEN())

(51)

Next Time

Summarizing and Grouping Data

Aggregate functions (MIN, MAX, SUM, AVG, COUNT)

Grouping rows with GROUP BY Filtering groups with HAVING

Joins

Referensi

Dokumen terkait

[r]

Dengan ini diumumkan Pemenang Pelelangan dan Pemenang Cadangan, adalah sebagai berikut

[r]

[r]

Hasil penelitian menunjukkan bahwa balita yang konsumsi energinya memiliki risiko tinggi berisiko 8,413 kali menderita gizi kurang dibandingkan dengan balita yang

Sehubungan dengan proses Lelang pada Sekretariat Daerah dan Kantor Pemberdayaan Perempuan dan KB Tahun Anggaran 2013 untuk Paket Pekerjaan : Pengadaan Mesin

2.4.4 Profitabilitas, Leverage, dan Ukuran Perusahaan Berpengaruh Secara Simultan Terhadap Nilai Perusahaan

Form variable perhitungan awal ini di gunakan untuk menampung data – data variable yang digunakan sebagai parameter untuk perhitungan dalam proses simulasi, data –