1. 4. Grants, filters, groups and Sort.
• Grants –Accessing someone else’s objects
• Filters – Getting only the necessary rows
• Groups – Aggregating rows to see totals,
counts and other metrics.
• Sort – Order your results as you need.
http://adata.guru
2. Grants (privileges)
• Accessing data that you don’t own.
http://adata.guru
User/Schema : SCOTT
DEPT
EMP
SAL_GRADE
User/Schema : OE
CUSTOMERS
WAREHOUSES
ORDERS
Select * from oe.customers
Error : No privileges
Select * from oe.customers
Grant select on oe.customers to scott
3. Grants (contd).
By the end of this section..
1. Each of our 5 users should be able to see each
other’s objects.
2. You should understand what (user) prefixes
mean and why they are important.
3. You should understand what object name
resolution is and how oracle figures out the
name
http://adata.guru
4. How to check grants
Oracle (data dictionary tables)
select *
From all_tab_privs
Where table_name = 'CUSTOMERS'; --- case matters!
http://adata.guru
SQL Developer:
Shift + F4
Grants Tab
5. Filters – The where clause
Select [* | column_list ]
From <table_name>
Where <filter_conditions>
Group by <group columns>
Having <filters on groups>
Order by <sort on columns or aggregates>
http://adata.guru
6. Filters – The where clause
Select [* | column_list ]
From <table_name>
Where ( condition_1 or
condition_2 or
condition_3 …
)
Group by <group columns>
Having <filters on groups>
Order by <sort on columns or aggregates>
http://adata.guru
7. Where clause : Just the rows you need
Example : Big ticket items
Items priced more than 200 or have a profit (list_price-min_price) of at least 50.
http://adata.guru
Only a few
columns Specific rows
8. Parts of a filter
<expression> <operator> <expression>
• Sal > 500 (salary > 500)
• Last_name like ‘%Cooper%’
• List_price > 1.2 * min_price
• Cust_last_name in (‘Lemmon’,’Collins’)
http://adata.guru
9. List of operators
Operator Description example
= Equal to Name = ‘King’
<>, != Not equal to Deptno <> 20
<, <=
>, >=
Less than, less than or equal to
Greater than, greater than or equal to
Sal < 2000
Sal >= 2000
IN One in a list of values Deptno in (10,20)
Like Matches a pattern Name like ‘Scott%’
! Not (of any operator or condition) !(name = ‘King’)
between Between two values Sal between 1000 and 2000
IS NULL A given expression is null Comm is null
IS NOT NULL Given value is not null Comm is not null
http://adata.guru
10. Groups & Aggregates
Select [* | column_list ]
From <table_name>
Where <filter_conditions>
Group by <group columns>
Having <filters on groups>
Order by <sort on columns or aggregates>
http://adata.guru
12. Group functions
Aggregate functions return a single result row based on groups of
rows, rather than on single rows. Commonly used in-built functions.
AVG
SUM
COUNT
MIN
MAX
Complete list of aggregate functions for 11g R2:
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions003.htm#SQLRF20035
http://adata.guru
13. When to group data?
• The problem statement or data being requested is at a summary level
– Total sales in a given month for a given product
– Average mileage for a car after 50,000 miles
– Least time for any participant to finish the marathon
• Data returned should be one record per a common attribute
– Product and the first sale date (one record per product)
– Department and number of employees in that dept (one record per
department)
– Running time world record for each race (100m, 200m, 400m) : ( One
record per race type)
http://adata.guru
14. Examples : Extreme cases
Grouping by no column : one value for all records in the table
Grouping by a unique column like the primary key – almost never useful.
Returns one row per employee as before
http://adata.guru
15. Common example
One record per grouped column(s):
Number of employees in each country/territory.
http://adata.guru
Grouping column
Aggregate function
17. Aggregate functions and nulls
Oracle aggregate functions ignore nulls.
Max(sal) => max(sal) where sal is not null
** Important when you look at averages
http://adata.guru
Total / 14
18. Group by : Common error
http://adata.guru
Each column should be in either
in the group by list or should be
aggregated. In this case, it looks
like we need one row per
(country+marital_status), so we
need to add marital_status to
the group by column to avoid
this error.
19. Having clause : Filtering on groups
Select [* | column_list ]
From <table_name>
Where <filter_conditions>
Group by <group columns>
Having <filters on groups>
Order by <sort on columns or aggregates>
http://adata.guru
20. Common scenarios
Requirement usually indicates a grouping column in the requirement.
For example:
1. List of all departments where the average salary in that
deparment is more than 5000
2. products and total sales of items where total sales is more than
5000.
3. Recipes where the total calorie content is less than 1000.
4. Jobs where the average age of the employee is more less than 30.
http://adata.guru
21. Example
Get the product id and the total sales of all products where the total
sales exceeds 5000. Rename the total sales heading appropriately.
http://adata.guru
Grouped column
Same column/expression as above
Aggregation (can be on an expression)
22. Good Practices
Use “having” only for aggregate columns
http://adata.guru
This is fine as you only have
access to max(salary) after the
aggregation and your condition is
on the aggregation
23. Sorting data – Order by Clause.
Select [* | column_list ]
From <table_name>
Where <filter_conditions>
Group by <group columns>
Having <filters on groups>
Order by <sort on columns or aggregates>
http://adata.guru
25. Sorting data – Nulls.
• Nulls have the “highest” value when sorting. You can change this
behavior by specifying “nulls first” or “nulls last”
http://adata.guru
Override
“NULL”
behavior
Default
“NULL”
behavior
26. Sorting data – Best practises.
1. Sort the data only if you need it. It is an resource-intensive
operation for large data sets.
2. Sort only on the last outer query. The sort in the inner query
usually does not matter.
3. If your application logic needs data to be ordered in a certain
way, sort explicitly. Do not assume the default order that data will
be returned in. Oracle makes no guarantees about the order in
which data is returned unless you specify it explicitly.
4. You can sort when you see the data as part of your analysis. If
your logic doesn’t need it, comment it before deploying the code.
http://adata.guru