SQL JOIN, Group-by and Sub

Introduction to
Database Systems,
CS420
SQL JOIN, Group-by and
Sub-query Clauses
1
Agenda

Functions

Group functions

Outer Join
 Sub-queries
2
SQL Statements
DML
(Data Manipulation Language)
DDL
(Data Definition Language)
DCL and Transaction Control
SELECT
INSERT
UPDATE
DELETE
CREATE
ALTER
DROP
GRANT
REVOKE
COMMIT
ROLLBACK
3
REVIEW: SQL SELECT
4
BETWEEN Operator
Use the BETWEEN operator to display
rows based on a range of values
 SELECT
last_name, salary
FROM
employees
WHERE
salary
BETWEEN
2500 AND 3500 ;

5
Membership Condition Using IN

Use the IN operator to test for values in a
list

SELECT last_name, salary, manager_id
FROM
employees
WHERE manager_id IN (100, 101, 201) ;
6
Using NULL Conditions
Test for nulls with IS NULL operator
 SELECT
last_name, manager_id
FROM
employees
WHERE manager_id IS NULL ;


Note: you cannot test with =

A null is not equal, or unequal to any value
7
ORDER BY Clause

Sort retrieved rows with ORDER BY clause



ASC: Ascending order, default
DESC: Descending order
The ORDER BY clause comes last in the
SELECT statement
SELECT
last_name, department_id, hire_date
FROM
employees
ORDER BY hire_date;
8
Sorting

Sorting in descending order
SELECT
last_name, department_id, hire_date
FROM
employees
ORDER BY hire_date DESC ;

Sorting by column alias
SELECT
last_name, salary*12
FROM
employees
ORDER BY annsal ;
annsal
9
Sorting (cont.)

Sorting using column’s numeric position
SELECT
last_name, job_id, hire_date, salary
FROM
employees
ORDER BY 3;

Sorting by multiple columns
SELECT
last_name, job_id, salary
FROM
employees
ORDER BY job_id, salary DESC ;
10
Functions
11
Case-Conversion Functions
Function
Result
LOWER(‘SQL Course’)
sql course
UPPER(‘SQL Course’)
SQL COURSE
INITCAP(‘SQL Course’) Sql Course
12
Example: Case-Conversion
SELECT
FROM
WHERE
last_name, job_id, salary
employees
last_name = ‘peng’;
0 rows returned.
SELECT
FROM
WHERE
last_name, job_id, salary
employees
LOWER(last_name) = ‘peng’;
1 rows returned.
13
Character Manipulation Functions
Function
Result
SUBSTR(‘HelloWorld’, 1,5)
Hello
LENGTH(‘HelloWorld’)
10
INSTR(‘HelloWorld’, ‘W’)
6
LPAD(salary, 10, ‘*’)
*****24000
RPAD(salary, 10, ‘*’)
24000*****
14
Number Functions
Function
Result
ROUND(45.926, 2)
45.93
TRUNC(45.926, 2)
45.92
Remainder = MOD(1600, 300)
100
15
Group Functions
Function
Description
AVG
Average
COUNT
Number of rows
SUM
Sum values
MAX/MIN
Maximum / Minimum value
16
GROUP Functions and Null Value

Group functions ignore null values in the column
SELECT
FROM
AVG(commision_pct)
employees;
17
Creating Groups of Data

You can divide rows in a table into smaller
groups using the GROUP BY clause
SELECT
FROM
[WHREE
[GROUP BY
[ORDER BY
column, group_function(column)
table
condition]
group_by_expression]
column];
18
Example: GROUP BY

All columns in the SELECT list that are not in
group functions must be in the GROUP BY
clause
SELECT
department_id, AVG(salary)
FROM
employees
GROUP BY department_id;
19
Illegal Queries with Group Functions
SELECT
department_id, COUNT(name)
FROM
employees;
A GROUP_BY clause must be
added to count the name for
each dept!!
SELECT
department_id, job_id, COUNT(name)
FROM
employees
Either remove job_id, or
GROUP BY department_id; Add job_id in the GROUP_BY
20
Illegal Queries with Group Functions

You cannot use the WHERE clause to restrict groups
SELECT
FROM
WHERE
GROUP BY

department_id, AVG(salary)
employees
AVG(salary) > 8000
department_id;
Use the HAVING clause to restrict groups
SELECT
FROM
GROUP BY
HAVING
department_id, AVG(salary)
employees
department_id
AVG(salary) > 8000
21
Restricting Group Results with the
HAVING clause

When you use the HAVING clause,
Oracle server restricts groups as follows
Rows are grouped
2. The group function is applied
3. Groups matching the HAVING clause are
displayed
1.
22
Outer Join
23
Left Outer Join
SELECT
FROM
ON

e.last_name, d.department_name
employees e LEFT OUTER JOIN
departments d
(e.department_id = d.department_id)
Display all the rows in the EMPLOYEES table, which is
the left table, even if there is no match in the
DEPARTMENTS table
24
Right Outer Join
SELECT
FROM
ON

e.last_name, d.department_name
employees e RIGHT OUTER JOIN
departments d
(e.department_id =
d.department_id)
Display all the rows in the DEPARTMENTS table, which
is the right table, even if there is no match in the
EMPLOYEES table
25
Full Outer Join
SELECT
FROM
ON

e.last_name, d.department_name
employees e FULL OUTER JOIN
departments d
(e.department_id =
d.department_id)
Display all the rows in the EMPLOYEES table, even if
there is no match in the DEPARTMENTS table. Also
display all the rows in the DEPARTMENTS table, even if
there is no match in the EMPLOYEES table.
26
Cartesian Products

A Cartesian product is formed when
A
join condition is omitted
 A join condition is invalid
 All rows in the first table are joined to all rows
in the second table

To avoid a Cartesian product, always
include a valid join condition
27
Subquery
28
Subquery (Nested SELECT)
SELECT
FROM
WHERE


select_list
table
expr operator
(SELECT select_list
FROM
table)
The subquery (inner query) executes before the
main query (outer query)
The result of the subquery is used by the main
query
29
Using Group Functions in a Subquery
SELECT
FROM
WHERE

last_name, job_id, salary
employees
salary =
(SELECT MIN(salary)
FROM
employees);
Note the subquery returns a single value, say
2500, to the outer query.
30
What’s Wrong with this Statement?
SELECT
FROM
WHERE

last_name, salary
employees
salary =
(SELECT
MIN(salary)
FROM
employees
GROUP BY department_id) ;
The subquery returns multiple values, one for each
group. The = operator is a single-row comparison
operator that expects only one value.
31
Multi-Row Subqueries
SELECT
FROM
WHERE

last_name, salary
employees
salary IN
(SELECT
MIN(salary)
FROM
employees
GROUP BY department_id);
The subquery returns multiple values, one for each
group. We use IN operator here, which is a multi-row
operator that expects one or more values.
32
Summary

Functions

String function
 Numeric functions
 Group functions
Outer Join
 Sub-query

33
END
34