0% found this document useful (0 votes)
10 views20 pages

Date Gruop Join Notes

The document provides an overview of various Oracle SQL functions and clauses, including date manipulation functions, GROUP BY, HAVING, ORDER BY, and different types of joins. It outlines the syntax and provides examples for each function and clause, demonstrating their usage in SQL queries. Additionally, it explains the concepts of inner joins, outer joins, equijoins, self joins, and cross joins with practical examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

Date Gruop Join Notes

The document provides an overview of various Oracle SQL functions and clauses, including date manipulation functions, GROUP BY, HAVING, ORDER BY, and different types of joins. It outlines the syntax and provides examples for each function and clause, demonstrating their usage in SQL queries. Additionally, it explains the concepts of inner joins, outer joins, equijoins, self joins, and cross joins with practical examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Function Example Result Description

ADD_MONTHS ADD_MONTHS 31-MAR-16 Add a number of months (n) to a


( DATE '2016-02-29', 1 )
date and return the same day
which is n of months away.
CURRENT_DATE SELECT CURRENT_DATE FROM 06-AUG-2017 Return the current date and time
dual 19:43:44 in the session time zone
EXTRACT EXTRACT(YEAR FROM 2017 Extract a value of a date time
SYSDATE)
field e.g., YEAR, MONTH,
DAY, … from a date time value.
LAST_DAY LAST_DAY 29-FEB-16 Gets the last day of the month of
(DATE '2016-02-01')
a specified date.
MONTHS_BETWEEN MONTHS_BETWEEN( DATE 6 Return the number of months
'2017-07-01', DATE
between two dates.
'2017-01-01' )

NEXT_DAY NEXT_DAY( DATE '2000-01- 02-JAN-00 Get the first weekday that is later
01', 'SUNDAY' )
than a specified date.
ROUND ROUND(DATE '2017-07-16', 01-AUG-17 Return a date rounded to a
'MM')
specific unit of measure.
SYSDATE SYSDATE 01-AUG-17 Return the current system date
and time of the operating system
where the Oracle Database
resides.
SYSTIMESTAMP SELECT SYSTIMESTAMP FROM 01-AUG-17 Return the system date and time
dual; 01.33.57.929000000
that includes fractional seconds
PM -07:00
and time zone.
TO_CHAR TO_CHAR( DATE'2017-01- Sunday, January Convert a DATE or
01', 'DL' ) 01, 2017 an INTERVAL value to a
character string in a specified
format.
TO_DATE TO_DATE( '01 Jan 2017', 01-JAN-17 Convert a date which is in the
'DD MON YYYY' )
character string to a DATE value.

TRUNC TRUNC(DATE '2017-07-16', 01-JUL-17 Return a date truncated to a


'MM') specific unit of measure.

Oracle GROUP BY Clause


In Oracle GROUP BY clause is used with SELECT statement to collect data from multiple records and group
the results by one or more columns.

Syntax:

1. SELECT expression1, expression2, ... expression_n,


2. aggregate_function (aggregate_expression)
3. FROM tables
4. WHERE conditions
5. GROUP BY expression1, expression2, ... expression_n;

Parameters:
expression1, expression2, ... expression_n: It specifies the expressions that are not encapsulated
within aggregate function. These expressions must be included in GROUP BY clause.

aggregate_function: It specifies the aggregate functions i.e. SUM, COUNT, MIN, MAX or AVG functions.

aggregate_expression: It specifies the column or expression on that the aggregate function is based on.

tables: It specifies the table from where you want to retrieve records.

conditions: It specifies the conditions that must be fulfilled for the record to be selected.

Oracle GROUP BY Example: (with SUM function)


Let's take a table "salesdepartment"

Salesdepartment table:

1. CREATE TABLE "SALESDEPARTMENT"


2. ( "ITEM" VARCHAR2(4000),
3. "SALE" NUMBER,
4. "BILLING_ADDRESS" VARCHAR2(4000)
5. )
Execute this query:

1. SELECT item, SUM(sale) AS "Total sales"


2. FROM salesdepartment
3. GROUP BY item;

Output

The above example will show the total sales of every individual item.

Oracle GROUP BY Example: (with COUNT function)


Let's take a table "customers"

Here we are creating a table named customers. This table doesn't have any primary key.

Customer table:

1. CREATE TABLE "CUSTOMERS"


2. ( "NAME" VARCHAR2(4000),
3. "AGE" NUMBER,
4. "SALARY" NUMBER,
5. "STATE" VARCHAR2(4000)
6. )

Execute this query:

1. SELECT state, COUNT(*) AS "Number of customers"


2. FROM customers
3. WHERE salary > 10000
4. GROUP BY state;

Output:

Oracle GROUP BY Example: (with MIN function)


Let?s take a table "employees"

Employees table:

1. CREATE TABLE "EMPLOYEES"


2. ( "EMP_ID" NUMBER,
3. "NAME" VARCHAR2(4000),
4. "AGE" NUMBER,
5. "DEPARTMENT" VARCHAR2(4000),
6. "SALARY" NUMBER
7. )

Execute this query:

1. SELECT department,
2. MIN(salary) AS "Lowest salary"
3. FROM employees
4. GROUP BY department;

Output:

Oracle HAVING Clause


In Oracle, HAVING Clause is used with GROUP BY Clause to restrict the groups of returned rows where
condition is TRUE.

Syntax:

1. SELECT expression1, expression2, ... expression_n,


2. aggregate_function (aggregate_expression)
3. FROM tables
4. WHERE conditions
5. GROUP BY expression1, expression2, ... expression_n
6. HAVING having_condition;

Parameters:
expression1, expression2, ... expression_n: It specifies the expressions that are not encapsulated
within aggregate function. These expressions must be included in GROUP BY clause.

aggregate_function: It specifies the aggregate functions i.e. SUM, COUNT, MIN, MAX or AVG functions.

aggregate_expression: It specifies the column or expression on that the aggregate function is based on.

tables: It specifies the table from where you want to retrieve records.

conditions: It specifies the conditions that must be fulfilled for the record to be selected.

having_conditions: It specifies the conditions that are applied only to the aggregated results to restrict
the groups of returned rows.

Oracle HAVING Example: (with GROUP BY SUM function)


Let's take a table "salesdepartment"

Salesdepartment table:

1. CREATE TABLE "SALESDEPARTMENT"


2. ( "ITEM" VARCHAR2(4000),
3. "SALE" NUMBER,
4. "BILLING_ADDRESS" VARCHAR2(4000)
5. )

Execute this query:


1.
2. SELECT item, SUM(sale) AS "Total sales"
3. FROM salesdepartment
4. GROUP BY item
5. HAVING SUM(sale) < 1000;

Output:

Oracle HAVING Example: (with GROUP BY COUNT function)


Let's take a table "customers"

Customer table:

1. CREATE TABLE "CUSTOMERS"


2. ( "NAME" VARCHAR2(4000),
3. "AGE" NUMBER,
4. "SALARY" NUMBER,
5. "STATE" VARCHAR2(4000)
6. )
Execute this query:

1. SELECT state, COUNT(*) AS "Number of customers"


2. FROM customers
3. WHERE salary > 10000
4. GROUP BY state
5. HAVING COUNT(*) >= 2;

Output:

Oracle HAVING Example: (with GROUP BY MAX function)


Execute this query:

1. SELECT department,
2. MAX(salary) AS "Highest salary"
3. FROM employees
4. GROUP BY department
5. HAVING MAX(salary) > 30000;

Output:

Oracle ORDER BY Clause


In Oracle, ORDER BY Clause is used to sort or re-arrange the records in the result set. The ORDER BY
clause is only used with SELECT statement.

Syntax:

1. SELECT expressions
2. FROM tables
3. WHERE conditions
4. ORDER BY expression [ ASC | DESC ];

Parameters:
expressions: It specifies columns that you want to retrieve.
tables: It specifies the table name from where you want to retrieve records.

conditions: It specifies the conditions that must be fulfilled for the records to be selected.

ASC: It is an optional parameter that is used to sort records in ascending order.

DESC: It is also an optional parameter that is used to sort records in descending order.

Oracle ORDER BY Example: (without ASC/DESC attribute)


Let's take a table "supplier"

Supplier table:

1. CREATE TABLE "SUPPLIER"


2. ( "SUPPLIER_ID" NUMBER,
3. "FIRST_NAME" VARCHAR2(4000),
4. "LAST_NAME" VARCHAR2(4000)
5. )

Execute this Query:

1. SELECT *
2. FROM supplier
3. ORDER BY last_name;

Output:

Oracle ORDER BY Example: (sorting in descending order)


If you want to sort your result in descending order, you should use the DESC attribute in your ORDER BY
clause:
Execute this Query:

1. SELECT *
2. FROM supplier
3. ORDER BY last_name DESC;

Oracle Joins
Join is a query that is used to combine rows from two or more tables, views, or materialized views. It retrieves
data from multiple tables and creates a new table.

Join Conditions
There may be at least one join condition either in the FROM clause or in the WHERE clause for joining two
tables. It compares two columns from different tables and combines pair of rows, each containing one row from
each table, for which join condition is true.

Types of Joins
o Inner Joins (Simple Join)
o Outer Joins
o Left Outer Join (Left Join)
o Right Outer Join (Right Join)
o Full Outer Join (Full Join)
o Equijoins
o Self Joins
o Cross Joins (Cartesian Products)

Oracle INNER JOIN


Inner Join is the simplest and most common type of join. It is also known as simple join. It returns all rows
from multiple tables where the join condition is met.

Syntax

1. SELECT columns
2. FROM table1
3. INNER JOIN table2
4. ON table1.column = table2.column;

Image representation of Inner Join

Oracle INNER JOIN Example


Let's take an example to perform Inner Join on two tables "Suppliers" and "Order1".

Suppliers

Order1
This example will return all rows from "suppliers" and "order1" table where there is a matching supplier_id
value in both the suppliers and order1 tables.

Execute the following query


1. SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number
2. FROM suppliers
3. INNER JOIN order1
4. ON suppliers.supplier_id = order1.supplier_id;

Output

Oracle OUTER JOIN


An outer join is similar to equijoin but it gets also the non-matched rows from the table. It is categorized in
Left Outer Join, Right Outer Join and Full Outer Join by Oracle 9i ANSI/ISO 1999 standard.

Left Outer Join


Left Outer Join returns all rows from the left (first) table specified in the ON condition and only those rows
from the right (second) table where the join condition is met.

Syntax

1. SELECT columns
2. FROM table1
3. LEFT [OUTER] JOIN table2
4. ON table1.column = table2.column;

Image representation of left outer join


Example

In this example, we are performing left outer join on the already created tables ?suppliers? and ?order1?.

The following example would return all records from table ?suppliers? and only those records from table ?
order1? where the join fields are equal.

Execute this query

1. SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number


2. FROM suppliers
3. LEFT OUTER JOIN order1
4. ON suppliers.supplier_id = order1.supplier_id;

Output

Right Outer Join


The Right Outer Join returns all rows from the right-hand table specified in the ON condition and only those
rows from the other table where the join condition is met.

Syntax
1. SELECT columns
2. FROM table1
3. RIGHT [OUTER] JOIN table2
4. ON table1.column = table2.column;

Image representation of Right Outer Join

Example

In this example, we are performing right outer join on the already created tables ?suppliers? and ?order1?.

The following example would return all rows from the order1 table and only those rows from the suppliers
table where the join condition is met.

Execute this query

1. SELECT order1.order_number, order1.city, suppliers.supplier_name


2. FROM suppliers
3. RIGHT OUTER JOIN order1
4. ON suppliers.supplier_id = order1.supplier_id;

Output

Full Outer Join


The Full Outer Join returns all rows from the left hand table and right hand table. It places NULL where the
join condition is not met.

Syntax

1. SELECT columns
2. FROM table1
3. FULL [OUTER] JOIN table2
4. ON table1.column = table2.column;
Image representation of Full Outer Join

Example

In this example, we are performing full outer join on the already created tables ?suppliers? and ?order1?.

The following example will return all rows from the ?suppliers? table and all rows from the ?order1? table
and whenever the join condition is not met, it places the NULL value.

Execute this query

1. SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number


2. FROM suppliers
3. FULL OUTER JOIN order1
4. ON suppliers.supplier_id = order1.supplier_id;

Output

Oracle EQUI JOIN


Oracle Equi join returns the matching column values of the associated tables. It uses a comparison
operator in the WHERE clause to refer equality.

Syntax
1. SELECT column_list
2. FROM table1, table2....
3. WHERE table1.column_name =
4. table2.column_name;

Equijoin also can be performed by using JOIN keyword followed by ON keyword and then specifying names
of the columns along with their associated tables to check equality.

Syntax

1. SELECT *
2. FROM table1
3. JOIN table2
4. [ON (join_condition)]

Oracle EQUI JOIN Example


Let' take two tables "agents" and "customer".

Agents table

Agent data

CUSTOMER
Execute this query

1. SELECT agents.agent_city,customer.last_name,
2. customer.first_name
3. FROM agents,customer
4. WHERE agents.agent_id=customer.customer_id;

Output

Oracle SELF JOIN


Self Join is a specific type of Join. In Self Join, a table is joined with itself (Unary relationship). A self join
simply specifies that each rows of a table is combined with itself and every other row of the table.

Syntax

1. SELECT a.column_name, b.column_name...


2. FROM table1 a, table1 b
3. WHERE a.common_filed = b.common_field;

Oracle SELF JOIN Example


Let's take a table "customers".
Join this table using SELF JOIN as follows:

1. SELECT a.name, b.age, a.SALARY


2. FROM CUSTOMERS a, CUSTOMERS b
3. WHERE a.SALARY < b.SALARY;

Output

Oracle Cross Join (Cartesian Products)


The CROSS JOIN specifies that all rows from first table join with all of the rows of second table. If there are
"x" rows in table1 and "y" rows in table2 then the cross join result set have x*y rows. It normally happens
when no matching join columns are specified.

In simple words you can say that if two tables in a join query have no join condition, then the Oracle
returns their Cartesian product.

Syntax

1. SELECT *
2. FROM table1
3. CROSS JOIN table2;

Or
1. SELECT * FROM table1, table2

Both the above syntax are same and used for Cartesian product. They provide similar result after
execution.

Image representation of cross join

Oracle Cross Join Example


Let's take two tables "customer" and "supplier".

Customer table detail

1. CREATE TABLE "CUSTOMER"


2. ( "CUSTOMER_ID" NUMBER,
3. "FIRST_NAME" VARCHAR2(4000),
4. "LAST_NAME" VARCHAR2(4000)
5. )

Supplier table detail

1. CREATE TABLE "SUPPLIER"


2. ( "SUPPLIER_ID" NUMBER,
3. "FIRST_NAME" VARCHAR2(4000),
4. "LAST_NAME" VARCHAR2(4000)
5. )

Execute this query

1. SELECT * FROM customer,supplier

Output

You might also like