International Islamic University Malaysia
Department of Information Systems
Kulliyyah of Information & Communication Technology
LECTURE 4:
BASIC SELECT
STATEMENTS
Dr. Mira Kartiwi
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.
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.
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.
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
Selecting All Columns
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
SELECT *
FROM doctor;
Selecting Specific Columns
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
SELECT doc_name, area
FROM doctor;
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.
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????
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
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.
Column Alias
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
 SELECT doc_name AS name, annual_bonus
FROM doctor
 SELECT doc_name AS “Name”, annual_bonus AS
“Bonus”
FROM doctor
 SELECT doc_name, annual_bonus AS “Bonus
Upgrade”
FROM doctor
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.
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.
Eliminating Duplicate Rows
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
SELECT DISTINCT area
FROM doctor;
-- how many area??????
Limiting the Rows Selected
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
SELECT *| { [DISTINCT] column | expression
[alias],...}
FROM table
WHERE condition(s);
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.
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)
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 (<>, !=, ^=)
WHERE Clause
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
SELECT doc_name
FROM doctor
WHERE chgperappt >= 40;
 --What does it means?????
 -- how many doctor name James?????
 -- What area is Stevenson?????
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
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 AND
Expression 3
BETWEEN Condition
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
SELECT doc_name, annual_bonus
FROM doctor
WHERE annual_bonus BETWEEN 2000 AND
4000;
-- how many people?
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
IN Condition
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
SELECT doc_id, doc_name, area
FROM doctor
WHERE doc_id IN (100,356, 558);
-- what area are they in???????
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.
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_id
FROM doctor
WHERE annual_bonus IS NULL;
--What is the name?---
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
LIKE Condition
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
SELECT *
FROM doctor
WHERE doc_name LIKE 'J%';
-- change to lowercase j
Note:
– Represents any sequence of zero or more characters
(%)
– Represents a single character (_)
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-67
SELECT pt_lname || ', ' || pt_fname "FULL NAME"
FROM patient
WHERE ptdob LIKE ’13-MAY-__' ;
Logical Operators
International Islamic University Malaysia
Kuliyyah of ICT – Department of Information Systems
 3 standard logical operators (AND, OR, and
NOT) 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
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
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;
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

Info 2102 l4 basic select statement lab1

  • 1.
    International Islamic UniversityMalaysia Department of Information Systems Kulliyyah of Information & Communication Technology LECTURE 4: BASIC SELECT STATEMENTS Dr. Mira Kartiwi
  • 2.
    Capabilities of SQLSELECT 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.
  • 3.
    More About SELECT InternationalIslamic 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.
  • 4.
    More About SELECT InternationalIslamic University Malaysia Kuliyyah of ICT – Department of Information Systems  Clauses are usually placed on separate lines.  Indents are used to enhance readability.
  • 5.
    Basic SELECT Statement InternationalIslamic University Malaysia Kuliyyah of ICT – Department of Information Systems  SELECT *| { [DISTINCT] column | expression [alias],...} FROM table;  SELECT identifies what column  FROM identifies which table
  • 6.
    Selecting All Columns InternationalIslamic University Malaysia Kuliyyah of ICT – Department of Information Systems SELECT * FROM doctor;
  • 7.
    Selecting Specific Columns InternationalIslamic University Malaysia Kuliyyah of ICT – Department of Information Systems SELECT doc_name, area FROM doctor;
  • 8.
    Using Arithmetic Operations InternationalIslamic 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.
  • 9.
    Operator Precedence and Parantheses InternationalIslamic 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????
  • 10.
    NULL Values International IslamicUniversity 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
  • 11.
    Column Alias International IslamicUniversity 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.
  • 12.
    Column Alias International IslamicUniversity Malaysia Kuliyyah of ICT – Department of Information Systems  SELECT doc_name AS name, annual_bonus FROM doctor  SELECT doc_name AS “Name”, annual_bonus AS “Bonus” FROM doctor  SELECT doc_name, annual_bonus AS “Bonus Upgrade” FROM doctor
  • 13.
    Concatenation Operator International IslamicUniversity 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.
  • 14.
    Literal Character String InternationalIslamic 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.
  • 15.
    Eliminating Duplicate Rows InternationalIslamic University Malaysia Kuliyyah of ICT – Department of Information Systems SELECT DISTINCT area FROM doctor; -- how many area??????
  • 16.
    Limiting the RowsSelected International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems SELECT *| { [DISTINCT] column | expression [alias],...} FROM table WHERE condition(s);
  • 17.
    The Where Clause InternationalIslamic 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.
  • 18.
    Comparisons International Islamic UniversityMalaysia 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)
  • 19.
    Comparison Conditions International IslamicUniversity 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 (<>, !=, ^=)
  • 20.
    WHERE Clause International IslamicUniversity Malaysia Kuliyyah of ICT – Department of Information Systems SELECT doc_name FROM doctor WHERE chgperappt >= 40;  --What does it means?????  -- how many doctor name James?????  -- What area is Stevenson?????
  • 21.
    Other Comparison Conditions InternationalIslamic University Malaysia Kuliyyah of ICT – Department of Information Systems  Between two values (inclusive) – BETWEEN... AND...  IN (set)  LIKE  IS NULL  IS NOT NULL
  • 22.
    BETWEEN Condition International IslamicUniversity 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 AND Expression 3
  • 23.
    BETWEEN Condition International IslamicUniversity Malaysia Kuliyyah of ICT – Department of Information Systems SELECT doc_name, annual_bonus FROM doctor WHERE annual_bonus BETWEEN 2000 AND 4000; -- how many people?
  • 24.
    The IN Operator InternationalIslamic 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
  • 25.
    IN Condition International IslamicUniversity Malaysia Kuliyyah of ICT – Department of Information Systems SELECT doc_id, doc_name, area FROM doctor WHERE doc_id IN (100,356, 558); -- what area are they in???????
  • 26.
    The IS NULLOperator 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.
  • 27.
    The IS NULLOperator International Islamic University Malaysia Kuliyyah of ICT – Department of Information Systems  Display the IDs of doctors that do not receive annual bonuses. SELECT doc_id FROM doctor WHERE annual_bonus IS NULL; --What is the name?---
  • 28.
    The LIKE Operator InternationalIslamic 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
  • 29.
    LIKE Condition International IslamicUniversity Malaysia Kuliyyah of ICT – Department of Information Systems SELECT * FROM doctor WHERE doc_name LIKE 'J%'; -- change to lowercase j Note: – Represents any sequence of zero or more characters (%) – Represents a single character (_)
  • 30.
    The LIKE Operator InternationalIslamic University Malaysia Kuliyyah of ICT – Department of Information Systems Display the full name and phone number for customers whose phone number begins with 549-67 SELECT pt_lname || ', ' || pt_fname "FULL NAME" FROM patient WHERE ptdob LIKE ’13-MAY-__' ;
  • 31.
    Logical Operators International IslamicUniversity Malaysia Kuliyyah of ICT – Department of Information Systems  3 standard logical operators (AND, OR, and NOT) 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
  • 32.
    Other NOT Operators InternationalIslamic 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
  • 33.
    NULL and NOTNULL 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;
  • 34.
    Lab Assignment 1 InternationalIslamic 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