Lecture 4 - Introduction to SQL I
Lecture 4 - Introduction to SQL I
DATA: AN INTRODUCTION
TO SQL SELECT
STATEMENTS (PART 1)
OPIM 5272: BUSINESS PROCESS MODELING AND DATA
MANAGEMENT
UGO ETUDO PH.D
WHAT IS A TABLE?
• You may notice that clause names, and SQL reserved keywords tend to be
capitalized – this is a stylistic choice, I advise that you follow it
• Try to place clauses on new lines
• Always terminate your SQL statements with a semicolon. This is especially import
when you are running SQL scripts (ordered collection of SQL statements)
PROJECTION ARITHMETIC (EXPRESSIONS)
Operator Operation
** exponentiation
+, - identity, negation
*, / multiplication,
division
+, -, || addition,
subtraction,
concatenation
comparison
NOT logical negation
AND conjunction
OR inclusion
PROJECTION ARITHMETIC
• Assume that the salary column is a monthly salary. Let us calculate annual
salary of employees plus a $100 bonus
SELECT last_name, first_name, salary, salary*12+100
FROM employees;
SELECT last_name, first_name, salary, salary*(12+100)
FROM employees;
SELECT last_name, first_name, salary, 12*(salary+100)
FROM employees;
NULL
• The longer our expressions become, the more unwieldy our column
headings (in the virtual table) become
• As a result, we may want to rename column headings in the output of a
SELECT statement
• We can use the AS operator or a space to indicate that the keyword
following an expression or column name is an alias
• When an alias contains reserved terms (for instance an alias USER),
spaces or needs to be case sensitive we can wrap it in double quotes
• Single and double quotes serve different purposes in Oracle
ALIAS ON COLUMNS
• No Alias
SELECT first_name, last_name, salary, ROUND(salary/12, 2)
FROM employees;
ALIAS ON COLUMNS
• With Alias
SELECT first_name "First", last_name "Last", salary,
ROUND(salary/12, 2) AS "Monthly Salary"
FROM employees;
ALIAS ON COLUMNS
• At this point, I’m usually being asked “how do I put both the first and last
name of an employee in the same field”
• The concatenation operator, ||, allows us to define expressions that link
strings (and other data types) drawn from two or more columns into a single
column
• Let’s try to place the first and last names of employees, along with their
salaries into a single column
SELECT first_name || last_name || salary AS earnings
FROM employees;
CONCATENATION
SELECT first_name || last_name || salary AS earnings
FROM employees;
• What’s wrong with this picture?
• The concatenation operator also allows us to work with literals!
• We may rewrite the query above:
SELECT first_name ||' '|| last_name ||' earns a salary of '|| salary AS
earnings
FROM employees;
CONCATENATION
• The angle brackets are delimiters of my choice. (other options include {}, []) You can
choose some other delimiter. Those delimiters enable the SQL interpreter
differentiate between a quote signifying a literal and a literal quote!
DISTINCT
• The DISTINCT keyword ensures that each record returned from the
query is unique. Say you would like to have a list of manager_ids and
you do not want this list to contain duplicates:
SELECT DISTINCT manager_id
FROM employees;
• The DISTINCT keyword must appear immediately after the SELECT
keyword. If you specify multiple columns after the DISTINCT keyword,
only unique combinations of those columns will be returned.
THE WHERE CLAUSE
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
BETWEEN ____ AND ____ Between two values (inclusive
IN (__ , __ , __) Matches any item within the list of
values
LIKE Contiguous match on character pattern
IS NULL Is a null value
LOGICAL OPERATORS
Operator Meaning
AND Evaluates as TRUE if both component conditions are true
OR Evaluates as TRUE if either component (or both) conditions is
true
NOT Evaluates as TRUE if the condition is false