info 2102 l4 basic select statement lab1

34
International Islamic University Malaysia Department of Information Systems Kulliyyah of Information & Communication Technology LECTURE 4: BASIC SELECT STATEMENTS Dr. Mira Kartiwi

Upload: iium

Post on 13-Feb-2017

97 views

Category:

Health & Medicine


2 download

TRANSCRIPT

Page 1: Info 2102 l4   basic select statement lab1

International Islamic University MalaysiaDepartment of Information Systems

Kulliyyah of Information & Communication Technology

LECTURE 4: BASIC SELECT STATEMENTS

Dr. Mira Kartiwi

Page 2: Info 2102 l4   basic select statement lab1

Capabilities of SQL SELECT Statements

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Selection (Restriction) – Allows for the retrieval of rows that satisfy certain specified condition (predicate).

Projection – Allows for the retrieval of specified columns (attributes).

Joining – Allows for the linking of data in different tables.

Page 3: Info 2102 l4   basic select statement lab1

More About SELECT

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT is technically a Data Manipulation Language (DML). However, Oracle does not classify it as such.

You can write SELECT statements on multiple lines. However, you are not allowed to split or abbreviate keywords.

SELECT statements are not case sensitive.

Page 4: Info 2102 l4   basic select statement lab1

More About SELECT

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Clauses are usually placed on separate lines.

Indents are used to enhance readability.

Page 5: Info 2102 l4   basic select statement lab1

Basic SELECT Statement

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

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

SELECT identifies what column FROM identifies which table

Page 6: Info 2102 l4   basic select statement lab1

Selecting All Columns

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT *FROM doctor;

Page 7: Info 2102 l4   basic select statement lab1

Selecting Specific Columns

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_name, areaFROM doctor;

Page 8: Info 2102 l4   basic select statement lab1

Using Arithmetic Operations

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_name, (annual_bonus+ 500)FROM doctor; Four arithmetic operations according to

precedence: (*, /), (+, -)

Operators of the same priority are evaluated from left to right.

Parentheses are used to force prioritized evaluation and to clarify statements.

Page 9: Info 2102 l4   basic select statement lab1

Operator Precedence and Parantheses

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_name, annual_bonus, 10*annual_bonus+500 FROM doctor;

SELECT doc_name, annual_bonus, 10*(annual_bonus+500) FROM doctor;

--What is the difference????

Page 10: Info 2102 l4   basic select statement lab1

NULL Values

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

How many doctor has Null value in the result???Why??

Arithmetic expressions containing a null value evaluate to null

Page 11: Info 2102 l4   basic select statement lab1

Column Alias

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Renames a column heading. Useful with calculations. Immediately follows the column name – there can

also be the optional AS keyword between column name and alias.

Requires double quotation marks if it contains spaces or special characters or is case sensitive.

Page 12: Info 2102 l4   basic select statement lab1

Column Alias

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_name AS name, annual_bonusFROM doctor

SELECT doc_name AS “Name”, annual_bonus AS “Bonus”FROM doctor

SELECT doc_name, annual_bonus AS “Bonus Upgrade”FROM doctor

Page 13: Info 2102 l4   basic select statement lab1

Concatenation Operator

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_name||area AS “Doctor”FROM doctor; Concatenates columns or character

strings to other columns. Is represented by two vertical bars (||). Creates a resultant column that is a

character expression.

Page 14: Info 2102 l4   basic select statement lab1

Literal Character String

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_name || ' is from ' || area AS “DOCTOR”FROM doctor; A literal is a character, a number, or a date

included in the SELECT statement. Date and character literal values must be

enclosed within single quotation marks. Each character string is output once for each row

returned.

Page 15: Info 2102 l4   basic select statement lab1

Eliminating Duplicate Rows

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT DISTINCT areaFROM doctor;

-- how many area??????

Page 16: Info 2102 l4   basic select statement lab1

Limiting the Rows Selected

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT *| { [DISTINCT] column | expression[alias],...}

FROM tableWHERE condition(s);

Page 17: Info 2102 l4   basic select statement lab1

The Where Clause

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

The WHERE clause can be added to the SELECT statement to restrict the results to rows that satisfy a specified condition.

Rows that do not meet the condition will not be included in the results.

Page 18: Info 2102 l4   basic select statement lab1

Comparisons

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

The two expressions must be of the same type

Character string literals should be enclosed in single quotes

Date literals should be of the form 'DD-MON-YY'

Numeric literals should consist of digits and optionally, a decimal and/or sign (no commas or dollar signs)

Page 19: Info 2102 l4   basic select statement lab1

Comparison Conditions

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Equal (=) Greater than (>) Greater than or equal to (>=) Less than (<) Less than or equal to (<=) Not equal to (<>, !=, ^=)

Page 20: Info 2102 l4   basic select statement lab1

WHERE Clause

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_nameFROM doctorWHERE chgperappt >= 40;

--What does it means????? -- how many doctor name James????? -- What area is Stevenson?????

Page 21: Info 2102 l4   basic select statement lab1

Other Comparison Conditions

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Between two values (inclusive) –BETWEEN... AND...

IN (set) LIKE IS NULL IS NOT NULL

Page 22: Info 2102 l4   basic select statement lab1

BETWEEN Condition

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Several special operators serve as shortcuts for longer expressions.One is the BETWEEN operator.

It is used to determine whether or not a value lies within a specific range (including the end points)

General syntax:Expression1 BETWEEN Expression2 ANDExpression 3

Page 23: Info 2102 l4   basic select statement lab1

BETWEEN Condition

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_name, annual_bonusFROM doctorWHERE annual_bonus BETWEEN 2000 AND 4000;

-- how many people?

Page 24: Info 2102 l4   basic select statement lab1

The IN Operator

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Used to see if a value occurs in a set of possible values

The set of possible values is specified within parentheses with commas between values

Page 25: Info 2102 l4   basic select statement lab1

IN Condition

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT doc_id, doc_name, areaFROM doctorWHERE doc_id IN (100,356, 558);

-- what area are they in???????

Page 26: Info 2102 l4   basic select statement lab1

The IS NULL Operator

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Any comparison to a null value that uses the standard comparison operators will not yield a match.

If a check for null values is needed, the IS NULL operator must be used.

Page 27: Info 2102 l4   basic select statement lab1

The IS NULL Operator

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Display the IDs of doctors that do not receive annual bonuses.

SELECT doc_idFROM doctorWHERE annual_bonus IS NULL;

--What is the name?---

Page 28: Info 2102 l4   basic select statement lab1

The LIKE Operator

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Used in conjunction with wildcard characters to match character string patterns

% is used to match zero or more characters

_ is used to match a single character Wildcards cannot be used without the

LIKE operator The LIKE operator should not be used

without wildcards

Page 29: Info 2102 l4   basic select statement lab1

LIKE Condition

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT *FROM doctorWHERE doc_name LIKE 'J%';-- change to lowercase j

Note:– Represents any sequence of zero or more

characters (%)– Represents a single character (_)

Page 30: Info 2102 l4   basic select statement lab1

The LIKE Operator

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Display the full name and phone number for customers whose phone number begins with 549-67SELECT pt_lname || ', ' || pt_fname "FULL NAME"FROM patientWHERE ptdob LIKE ’13-MAY-__' ;

Page 31: Info 2102 l4   basic select statement lab1

Logical Operators

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

3 standard logical operators (AND, OR, andNOT) are used to combine expressions

AND will return a value of true only if both expressions are true

OR will return a value of true if either or both of the expressions are true

NOT will return the opposite value of the expression.

Order of precedence: NOT, AND, OR

Page 32: Info 2102 l4   basic select statement lab1

Other NOT Operators

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

The NOT operator can also be used in conjunction with the special operators as follows: NOT BETWEEN NOT IN IS NOT NULL NOT LIKE

Page 33: Info 2102 l4   basic select statement lab1

NULL and NOT NULL Conditions

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

SELECT docid, docname, annual_bonus FROM doctor WHERE annual_bonus IS NULL;

TO NEGATE ..

SELECT docid, docname, annual_bonus FROM doctor WHERE annual_bonus IS NOT NULL;

Page 34: Info 2102 l4   basic select statement lab1

Lab Assignment 1

International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems

Run the hospital script and create a question in English as well as its SQL query that generates: NULL values Values not within a specific range List of names that started from or ended

with specific letter