Professional Documents
Culture Documents
December 2018
Agenda
This Presentation will provide you an overview of the following:
SQL Joins
SQL Sub Queries
NULL in SQL
SQL Functions
Grouping and Aggregation
Number functions
Text functions
Analytic functions
Conditional functions
Date functions
SQL Joins
3
3
SQL JOINS Continued…
4
4
Inner Join
Product Company
Product
Company
PName Price Category Manufacturer
Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks
GizmoWorks 25 USA
Powergizmo $29.99 Gadgets GizmoWorks
Canon 65 Japan
SingleTouch $149.99 Photography Canon
Hitachi 15 Japan
MultiTouch $203.99 Household Samsung
6
6
Right Outer Join
Product
Company
PName Price Category Manufacturer Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks GizmoWorks 25 USA
7
7
Full Outer Join
Product
Company
PName Price Category Manufacturer Cname StockPrice Country
Gizmo $19.99 Gadgets GizmoWorks GizmoWorks 25 USA
Company(name, city)
Product(pname, maker)
Purchase(id, product, buyer)
SELECT Company.city
FROM Company
WHERE Company.name IN
(SELECT Product.maker
FROM Purchase , Product
WHERE Product.pname=Purchase.product
AND Purchase .buyer = ‘Joe Blow‘);
10
Correlated Queries
correlation
11
NULLS in SQL
Whenever we don’t have a value, we can put a NULL
Can mean many things:
Value does not exists
Value exists but is unknown
Value not applicable
Etc.
The schema specifies for each attribute if can be null (nullable
attribute) or not
How does SQL cope with tables that have NULLs ?
12
Null Values
Can test for NULL explicitly:
x IS NULL
x IS NOT NULL
SELECT *
FROM Person
WHERE age < 25 OR age >= 25 OR age IS NULL
13
Example
Emp_Id EMP_Nam Salary Dept_Id Age
e
1 Mark 1000 10 34
2 John 2000 (null) 33
3 Charlie 2345 20 32
4 Kathy 2240 30 40
Find employees which are not linked to any
department?
Functions
Single-row Multiple-row
functions functions
15
Single-Row Functions
Character
General Number
Single-row
functions
Conversion Date
16
Single-Row Functions
Manipulate data items
Can be nested
17
SQL Functions
Aggregation
18
Aggregation: Count
We probably want:
19
Aggregation: Grouping
20
20
Grouping and Aggregation
21
Grouping and Aggregation
Product TotalSales
Bagel 50
Banana 15
22
HAVING Clause
23
Exercise
EMPLOYEES EMPLOYEE_DETAILS
EMP_I DEPT_ EMP_ID SALARY Job_Stat
D ID us
1 10 1 10000 P
2 10 2 20000 T
3 10 3 15000 P
4 20 8 12500 T
5 20 4 30000 P
6 30 6 25000 T
Find sum of salary for every department for which employees
exists and job status is permanent.
Find sum of salary for every department which has sum of salary
greater than 30000 and job status is temporary.
Find minimum and maximum salary for every department where
employee exists.
24
24
Ordering the Results
SELECT ename, job, sal, deptno FROM emp ORDER BY
sal, ename
ENAME JOB SAL DEPTNO
SMITH CLERK 800 20
JAMES CLERK 950 30
ADAMS CLERK 1100 20
MARTIN SALESMAN 1250 30
WARD SALESMAN 1250 30
MILLER CLERK 1300 10
TURNER SALESMAN 1500 30
ALLEN SALESMAN 1600 30
Ties are broken by the second attribute on the ORDER BY list, etc.
25
The LIKE operator
26
Eliminating Duplicates
JOB
SELECT DISTINCT JOB CLERK
FROM emp SALESMAN
MANAGER
Compared to:
JOB
CLERK
SELECT JOB
SALESMAN
FROM Emp
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
27
Built-in Functions
28
28
Number Functions : ROUND
The ROUND function rounds the value you want to modify.
Example:
SELECT pname, pprice,
ROUND (pprice,0) AS ROUND0,
ROUND (pprice,1) AS ROUND1,
ROUND (pprice,2) AS ROUND2
FROM products
29
29
Number Functions : TRUNC
The TRUNC function truncates precision from a number.
Example:
30
30
Number Functions : MOD, POWER
MOD(m,n): Modulus function returns remainder when m is divided
by n.
POWER(m,n): Power function returns the value of the specified
expression(m) to the specified power(n).
Example : Employees (Emp_ID, Emp_Name, Salary)
Example:
Select sign(-17.36) col1,
sign(17.36) col2, Col1 Col2 Col3
sign(0) col3, From dual ; ----------------------------------
-----
-1 1 0
abs(): Returns the absolute value of a number passed as argument.
Example:
ABS(-17.36)
--------------------------------------
17.36
32
32
Text Functions : UPPER, LOWER & INITCAP
These three functions change the case of the text you give them.
UPPER(Column_Name) : Changes parameter text font to uppercase.
LOWER(Column_Name) : Changes parameter text font to lowercase.
INITCAP(Column_Name) : Changes parameter text font to lowercase.
Example : City (City_Name)
SELECT City_Name,
UPPER(City_Name) UPR,
LOWER(City_Name) LRW,
INITCAP(City_Name) INTCP
From city;
City_Name UPR LWR INTCP
Philedelphia PHILEDELPHIA philedelphia Philedelphia
New york NEW YORK new york New York
new Delhi NEW DELHI new delhi New Delhi
loNdon LONDON london London
33
33
Text Functions : LENGTH
To determine the lengths of the data stored in a database column.
35
35
Text Functions : INSTR
Used to find starting location of a pattern in the string.
Syntax: INSTR(Column, pattern, [starting position, [occurrence]])
Find starting location of pattern from starting position and for nth occurrence in
column.
Example: Items (Item_id, Item_desc)
Example: employee(name)
Select Name,
Replace(name,‘ar',‘a') name_edit1
Replace(name,‘a',‘@') name_edit2
Replace(name,‘a',‘') name_edit2
from employee;
Example: employees(emp_id,emp_name)
ADD_MONTHS(starting_date,
Add_months(d, n) Adds n months to date d.
number_of_months)
Difference in months
Months_between(f, s) MONTHS_BETWEEN(later_date, earlier_date)
between date f and date s.
39
39
Data Conversion Functions
Function Description
40
40
Analytic Functions : LEAD,LAG
LEAD function is used to return data from the next row
LAG function is used to access data from a previous row
41
41
Analytic Functions : RANK()
Rank() will assign ranks to the values in a given set
Note: Rank() can cause non-consecutive rankings if the tested values are the same 42
42
Analytic Functions : DENSE RANK()
Dense Rank() acts like the RANK function except that it assigns consecutive ranks.
43
43
Conditional Functions : CASE,DECODE
SQL equivalence of IF..THEN..ELSE conditional procedural statement
CASE and DECODE statement are very similar but CASE is an extended version of DECODE
44
44
Thank You
45