0% found this document useful (0 votes)
57 views35 pages

Albertsons SQL Advance

The document outlines an agenda for an advanced SQL training covering topics such as: altering sessions; expressions; subqueries; summary calculations; logical operators; grouping; ordering; unions; joins; exporting data from SQL Developer. Demo examples will also be provided.

Uploaded by

sai rao
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)
57 views35 pages

Albertsons SQL Advance

The document outlines an agenda for an advanced SQL training covering topics such as: altering sessions; expressions; subqueries; summary calculations; logical operators; grouping; ordering; unions; joins; exporting data from SQL Developer. Demo examples will also be provided.

Uploaded by

sai rao
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/ 35

Advanced SQL

SQL Training
Structured Query Language
August 2018

1
Agenda
 SQL Developer – Alter Session
 Expressions
 Subqueries
 Summary Calculations
 Logical Operators
 Group by
 Order by
 Unions
 Joining Records
 SQL from PS Query to SQL Developer
 Export Data
 Demo

2
SQL Developer – Alter Session

 Alter Session will help set schema for the entire session
– With no schema set for the session, “Sql Developer” is not able to recognize the table name

With schema set to SYSADM for the session, “Sql Developer” is now able to recognize the table name

3
Creating Expressions - Build Expressions

 Expressions are calculations that PeopleSoft Query performs as


part of a query. Use them when you must calculate a value that
PeopleSoft Query doesn't provide by default (for example, to add
the values from two fields together or to multiply a field value by a
constant).
 An expression can be treated as a field query. When selected for
output, you can change its column heading or sort it. In this topic,
you are going to create a query that displays customer information
and calculates the total cost of training units for each customer.
 You will create an expression to determine what each customer
has spent on training units. The expression multiplies the cost of a
training unit by the number of units that each customer owns.
 In this topic, you are going to create a query that displays the other
pay amount to be distributed to employees on a quarterly basis.
The other pay data is an annual amount to be paid to the
employee, but your company will process the amount quarterly.
You want to divide the total amount for each employee by four (the
number of quarters in a year) to view the distribution amount.

4
Expressions
 Using an expression as a field in SQL

5
Using Subqueries - Explaining Subqueries
Subqueries – (using in)
Let’s review first! Remember your algebra equations from school? The expression (x+1)y is
indicating that x+1 needs to be solved before multiplying the result by y. The equation inside the
parentheses needs to be executed, then applied to the operation outside the parentheses.
A subquery, sometimes called a sub-SELECT, is a query whose results are used by another query.
The main query uses the subquery’s result set as a comparison value for a selection criterion.
You create a subquery when you need to compare a field value to the results of a second query.
Suppose, for example, that you want a list of employees with active child support garnishments. For
each employee in the PERSONAL_DATA table, you must determine whether his or her employee ID is
in the GARN_SPEC table. That is, you must compare the value in the PERSONAL_DATA.EMPLID field
to the results of a subquery that selects the EMPLID values from the GARN_SPEC table.

6
Subqueries - Example
Subqueries – (using exists and not exists)
 Using an subquery to find missing job records
– One example is using a subquery in a “not exists” sql. This checks for
records in one table that does not exist in another.
– The below statement is used when the JOB record and the JOB_JR record
are out of sync.

7
Using Subqueries – Creating a Subquery
Different Condition Types explained

8
Logical Operators - Examples
 Using between
– select * from ps_pay_check where emplid = '1235612' and PAY_END_DT between ‘02-Jun-2018' and '30-Jun-
2018';

 Using Greater than


– select * from ps_pay_check where emplid = '1235612' and PAY_END_DT > ‘02-Jun-2018’;

 Using Greater than or equal to


– select * from ps_pay_check where emplid = '1235612' and PAY_END_DT >= ‘02-Jun-2018’;

9
Logical Operators - Examples
 Using Less than
– select * from ps_pay_check where emplid = '1235612' and PAY_END_DT < '02-Feb-2015';

 Using Null
– select * from ps_person where birthdate is null;

 Using Like
– select * from ps_names where name like '%McAdam%’;

10
Summary Calculations - Aggregate
Functions
 In a standard query, each row in the result set corresponds to an
individual row in the table that you are querying. Sometimes,
however, you instead want a summary of the information in
multiple rows. For example, you might want to know how many
customers you have in each state. You can query for this kind of
summary information using aggregate functions.
 An aggregate function is a special type of operator that returns a
single value based on multiple rows of data. When your query
includes one or more aggregate functions, PeopleSoft Query
collects related rows and displays a single row that summarizes
their contents.

The following table lists the aggregate functions that you can apply to a field using PeopleSoft Query.

11
Summary Calculations - Example
 Using the aggregate function to sum

• Using the aggregate function to average

 Using the aggregate functions min and max


select min(total_gross), max(total_gross) from ps_pay_check where paygroup = '029'
and PAY_END_DT = '11-Feb-2018';

12
Summary Calculations - Example
 Using the aggregate function to count the number of labor agreements
in a paygroup

13
Summary Calculations - Example
 Using the aggregate function to count distinct number of checks for pay
group 099 and pay_end_dt = 03-Feb-2018
 select count(distinct emplid) from ps_pay_check where paygroup = '099' and pay_end_dt = '03-Feb-2018’;

14
Group by- Example
 Using the group by in an aggregate function to separate the
aggregation into groups

15
Group by- Example
 Using the group by in an aggregate function to separate the
aggregation into groups

16
Group by / Order by- Example
 Using the group by order by in an aggregate function to separate the
aggregation into groups and sort

17
Order by- Example
 Using the group by order by in an aggregate function to separate the
aggregation into groups and sort
 DESC vs ASC

18
Unions
 The SQL UNION clause/operator is used to combine the results of two or more SELECT
statements without returning any duplicate rows.
 To use this UNION clause, each SELECT statement must have
– The same number of columns selected
– The same number of column expressions
– The same data type and
– Have them in the same order
 But they need not have to be in the same length.

19
Unions - Syntax
The basic syntax of a UNION clause is as follows:
Select column1 [, column2 ]
From table1 [, table2 ]
[where condition]
union
Select column1 [, column2 ]
From table1 [, table2 ]
[where condition]

Here, the given condition could be any given expression based on your
requirement.

20
Union Example
The basic syntax of a UNION

21
Joins
 The SQL Joins clause is used to combine records from two or more tables in a
database. A JOIN is a means for combining fields from two tables by using values
common to each.
 The join is performed in the WHERE clause. Several operators can be used to join
tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can all be used
to join tables. However, the most common operator is the equal to symbol.
 There are different types of joins available in SQL −
– INNER JOIN − returns rows when there is a match in both tables.
– LEFT JOIN − returns all rows from the left table, even if there are no matches in the right table.
– RIGHT JOIN − returns all rows from the right table, even if there are no matches in the left table.
– FULL JOIN − returns rows when there is a match in one of the tables.

22
Inner Joins (Standard)
 Joins each row of the first table with each row from the second table for
which the condition matches
– SELECT ... FROM tab1 [ INNER ] JOIN tab2 ON condition;

 Or using the traditional comma-join notation:


– SELECT ... FROM tab1, tab2 WHERE condition;

23
Outer Joins
 Joins each row from the first table with each row from the second table for
which the condition matches. Furthermore, nonmatching rows are added to
the result.
– LEFT JOIN all rows from the left table
 Rows without a join partner are filled up with null values.
 Syntax
– SELECT ... FROM tab1 LEFT [ OUTER ] JOIN tab2
ON condition;

24
Outer Joins
– RIGHT JOIN all rows from the right table
 Rows without a join partner are filled up with null values.
 Syntax
– SELECT ... FROM tab1 RIGHT [ OUTER ] JOIN tab2
ON condition;

25
Outer Joins
– FULL JOIN all rows from both tables
 Rows without a join partner are filled up with null values.
 Syntax
– SELECT ... FROM tab1 FULL [ OUTER ] JOIN tab2
ON condition;

26
SQL from PS Query to SQL Developer
 Select View SQL tab and copy sql to SQL Developer

27
Exporting Data
 Data can be exported from SQL Developer
– Run a Query
– Right click in the result area
– Click Export

28
Exporting Data
– Click Format Dropdown
– Select XLS for excel

29
Exporting Data
– Click Browse to select where to save the file
– Name the export File
– Click Save

30
Exporting Data
– Click Next

31
Exporting Data
– Click Finish

32
Exporting Data
– Data will be exported to an excel spreadsheet

33
Demo

34
Questions

35

You might also like