0% found this document useful (0 votes)
2 views

Lecture 5 - Introduction to SQL II

This document serves as an introduction to SQL SELECT statements, focusing on operator precedence, sorting data, and the use of substitution variables. It covers various SQL functions, including single row functions, aggregate functions, and conditional expressions, along with examples of their application. Additionally, it explains grouping and aggregation techniques, including the use of the GROUP BY and HAVING clauses to analyze data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 5 - Introduction to SQL II

This document serves as an introduction to SQL SELECT statements, focusing on operator precedence, sorting data, and the use of substitution variables. It covers various SQL functions, including single row functions, aggregate functions, and conditional expressions, along with examples of their application. Additionally, it explains grouping and aggregation techniques, including the use of the GROUP BY and HAVING clauses to analyze data effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

WORKING WITH TABLE

DATA: AN INTRODUCTION
TO SQL SELECT
STATEMENTS (PART 2)
OPIM 5272: BUSINESS PROCESS MODELING AND DATA
MANAGEMENT
UGO ETUDO PH.D
COMPLETE RULES OF PRECEDENCE

Operator Order Operator


1 Arithmetic Operators
2 Concatenation Operators
3 Comparison Conditions
4 IS [NOT] NULL, LIKE, [NOT] IN
5 [NOT] BETWEEN
6 Not equal to (!=, <>)
7 NOT logical condition
8 AND logical condition
9 OR logical condition
COMPLETE RULES OF PRECEDENCE

Say we want to know which employees are both president


(job_id = ‘AD_PRES) and earn more than 12000 or are sales
representatives
Option 1
(job_id = ‘SA_REP’)
Option 2
SELECT first_name, SELECT first_name,
last_name, salary, job_id last_name, salary, job_id
FROM employees FROM employees
WHERE job_id = 'SA_REP' WHERE job_id = 'SA_REP'
AND salary > 12000 OR job_id = 'SA_MAN'
OR job_id = 'SA_MAN'; AND salary > 12000;
SORTING DATA
The ORDER BY clause, when used, is always the last clause in the SELECT
statement.
[ORDER BY {column, expression, numeric_position} [ASC|DESC]]
The keywords ASC and DESC are optional: when neither is used, the clause sorts
data in ascending order.
Sorting by multiple columns is allowed where the limit on the number of sorting
columns is constrained by the tablespace
Example:
SELECT first_name, last_name, salary, job_id
FROM employees
WHERE job_id = 'SA_MAN'
ORDER BY salary DESC;
SORTING DATA
• The ORDER BY clause can be constructed with:
Expressions
SELECT first_name, last_name, salary/12, job_id
FROM employees Note that both
WHERE job_id = 'SA_MAN' queries are
equivalent
ORDER BY salary/12 DESC;

Numeric Column Position Also note that


column aliases
SELECT first_name, last_name, salary/12, job_id can be used in
FROM employees the ORDER BY
clause even
WHERE job_id = 'SA_MAN' though they are
ORDER BY 3 DESC; invalid in the
WHERE clause
SUBSTITUTION VARIABLES (SQL*PLUS
AND SQL DEVELOPER CLIENTS)
Substitution Variables are containers which can be assigned values just before
a query is executed. We use the ampersand to denote them: &, and &&
• Substitution variables allow SELECT (and other statements) to be interactive
and enable straightforward query re-use (important when queries get quite
complex)
• A single ampersand means that the user will be prompted for a value every
time the variable appears
• A double ampersand means that the user will assign a value once that will be
used for each occurrence of the variable
SUBSTITUTION VARIABLES

Example 1
Integral Values
SUBSTITUTION VARIABLES

Example 2
Quoted Values
SUBSTITUTIO
N VARIABLES

Example 3
Double-Ampersand
Column Names
Expressions
DEFINE
• We can use the define command to set global values for our session
ALTER SESSION SET NLS_DATE_FORMAT = 'dd-mon-yy';
DEFINE h_date = TO_DATE('02/05/2008', 'mm/dd/yyyy');
SELECT first_name, last_name, hire_date
FROM employees
WHERE hire_date > &h_date;
UNDEFINE h_date;
• I show the special case of date here because defining a date has to be
done with the TO_DATE(literal, format) function
FUNCTIONS

• Broadly, there are two types:


• Single Row Functions
• Multiple Row Functions (I commonly refer to these as aggregate functions)

Comma
Delimited Function’s
Output
Argument Logic
List
• Functions typically return scalar values although they need not take
arguments
SINGLE ROW FUNCTIONS

• Act on each row that is returned from a SELECT statement


• Accept arguments and return one value per row
• Can be nested
• Can be used to modify the datatype of a column or columns
• Arguments may be columns or expressions
SINGLE ROW FUNCTIONS

5 Types of Single Row Function:


• Character (case conversion and character manipulation)
• General
• Number (manipulating numbers)
• Conversion (convert data types – we’ve seen these with
TO_DATE())
• Date (manipulating dates)
CHARACTER FUNCTIONS
Function Usage
LOWER(column| Converts alpha character values to lowercase
expression)
UPPER(column| Converts alpha character values to uppercase
expression)
INITCAP(column| Converts the first character of a word to uppercase
expression) leaving all others as lowercase
CONCAT(column1| Concatenates the first character value to the second
expression1, column2| character value
expression2)
SUBSTR(column| Returns a subset of characters beginning with character
expression, m[,n]) at position m and ending at the end of the string or
when specified, position n; where m is negative we
count backwards from the end of the character string.
NUMBER FUNCTIONS

Function Purpose
ROUND(column| Rounds the column, expression, or value to n decimal
expression,n) places when n is given or no decimal places when it is not
given, or rounds numbers to the left of the decimal point
when n is negative
MOD(m,n) Modulus: givies the remainder of m after division by n
TRUNC(column| Truncates (slashes) the column expression or value to n
expression,n) decimal places when n is given (behaves similarly but not
identical to round when negative) and if n is not given its
given a default of 0
DATE FUNCTIONS
First, it is important to note that we can add and subtract integers from dates.
By default, when we do this, we are adding or subtracting days. Taking the
arithmetic difference between two dates will result in the number of days
between them.
SELECT first_name, last_name, (sysdate - hire_date) "days since hire"
What is
FROM employees; sysdate?

SELECT first_name, last_name, (sysdate - hire_date)/7 "weeks since hire"


FROM employees;

SELECT first_name, last_name, (sysdate - hire_date)/365 "years since hire"


FROM employees;
DATE FUNCTIONS

Function Result
MONTHS_BETWEEN(date1, Gives the number of months between two dates
date2)
ADD_MONTHS(date, n) Add n months to a particular date
NEXT_DAY(date, ‘char’) Returns the next day of the week that corresponds to
char. If char is ‘FRIDAY’ it returns the next Friday
LAST_DAY(date) Returns the last day of the month
CONVERSION FUNCTIONS

• We use conversion functions to explicitly convert from one


datatype to another
• In a limited set of cases Oracle can do this implicitly. In all
other cases Conversion functions are warranted.
• In the table below, the From and To columns are
From To
interchangeable
VARCHAR2 or CHAR NUMBER
VARCHAR2 or CHAR DATE
CONVERSION FUNCTIONS

TO_CHAR with Dates:


SELECT last_name, TO_CHAR(hire_date, 'fmDD Month YYYY') as "Hired On"
FROM employees;

SELECT last_name, TO_CHAR(hire_date, 'fmDdspth "of" Month YYYY') as "Hired On"


FROM employees;

SELECT last_name, TO_CHAR(hire_date, 'fmddspth "of" Month YYYY') as "Hired On"


FROM employees;
CONVERSION FUNCTIONS
TO_CHAR with Numbers:
SELECT last_name, TO_CHAR(salary, '$999,999.00') as "Salary"
FROM employees;

SELECT last_name, TO_CHAR(salary, '$9,999.00') as "Salary"


FROM employees;

SELECT last_name, TO_CHAR(salary, '$99,999') as "Salary"


FROM employees;
CONDITIONAL EXPRESSIONS

CASE expr WHEN comparison_expr1 THEN return_expr1


[WHEN comparison_expr2 THEN return_expr2
WHEN comparison_expr3 THEN return_expr3
ELSE else_expr]
CONDITIONAL EXPRESSIONS

• Let’s assign raises to folks in certain job categories!


Example 1 Example 2

SELECT last_name, salary, SELECT last_name, salary,


(CASE job_id WHEN 'SA_MAN' (CASE job_id WHEN 'SA_MAN'
THEN 1.05*salary THEN 1.05*salary
WHEN 'AC_MGR' THEN WHEN 'AC_MGR' THEN
1.1*salary 1.1*salary
WHEN 'AD_VP' THEN WHEN 'AD_VP' THEN
1.08*salary 1.08*salary
ELSE 1.02*salary END) "New END) "New Salary"
Salary" FROM employees;
FROM employees;
CONDITIONAL EXPRESSIONS

• Let’s assign a label to each of set of salary ranges (example of a searched


case):
SELECT last_name, salary,
(CASE WHEN salary < 5000 THEN 'high paid'
WHEN salary < 10000 THEN 'well paid'
WHEN salary < 20000 THEN 'highly paid'
ELSE 'Whoa!' END) "salary grades"
FROM employees;
GROUPING AND AGGREGATION

Group Functions (Aggregate Functions) return a single value for each of a


collection of input rows, ignoring nulls
• AVG
• COUNT
• MAX (works on numeric, character (alphabetical ordering) and date
(temporal ordering) datatypes)
• MIN (works on numeric, character (alphabetical ordering) and date
(temporal ordering) datatypes)
• STDDEV (square root of variance, aka dispersion)
• SUM
• VARIANCE (sum of squared distances from the mean)
GROUPING AND AGGREGATION

• When using aggregate functions without a GROUP BY (more


on this clause later) only aggregate functions may make up
the SELECT clause
Good Not Good

SELECT SELECT last_name,


ROUND(AVG(salary),2), AVG(salary), MIN(salary),
MIN(salary), MAX(salary), MAX(salary), SUM(salary),
SUM(salary), VARIANCE(salary)
ROUND(VARIANCE(salary), FROM employees;
2)
FROM employees;
GROUPING AND AGGREGATION
• The GROUP BY clause specifies a list of columns that will be used to form
groups on which aggregate functions are to be applied.
Say we want to know the average salary of individuals working in each
department:
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id;
• Not that we are using an aggregate function but we also have a non
aggregating expression in SELECT clause
• GROUP BY SELECT statements must always have aggregate functions
• You may also include in the SELECT clause column names that are used in the
GROUP BY clause
GROUPING AND AGGREGATION

We can group by more than one column where repeating combinations of the
respective field values delineate groups:
Here, we will know the total salary burden of each job category within each
department.

SELECT department_id, job_id, sum(salary)


FROM employees
GROUP BY department_id, job_id;
ORDER BY department_id;
GROUPING AND AGGREGATION

• We can also restrict the groups that are returned by passing conditions
on each group using the HAVING clause
Say we only want data for departments with an average salary greater
than 10000:
You cannot
SELECT department_id, AVG(salary) use a
FROM employees column alias
in the
GROUP BY department_id having
clause
HAVING AVG(salary) > 10000;
GROUPING AND AGGREGATION

• We can nest group operations (nesting cannot go beyond the


depth shown in the tables below)

Good Not Good

SELECT MAX(AVG(salary)) SELECT department_id,


FROM employees MAX(AVG(salary))
GROUP BY department_id; FROM employees
GROUP BY department_id;

You might also like