Date Gruop Join Notes
Date Gruop Join Notes
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.
Syntax:
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.
Salesdepartment table:
Output
The above example will show the total sales of every individual item.
Here we are creating a table named customers. This table doesn't have any primary key.
Customer table:
Output:
Employees table:
1. SELECT department,
2. MIN(salary) AS "Lowest salary"
3. FROM employees
4. GROUP BY department;
Output:
Syntax:
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.
Salesdepartment table:
Output:
Customer table:
Output:
1. SELECT department,
2. MAX(salary) AS "Highest salary"
3. FROM employees
4. GROUP BY department
5. HAVING MAX(salary) > 30000;
Output:
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.
DESC: It is also an optional parameter that is used to sort records in descending order.
Supplier table:
1. SELECT *
2. FROM supplier
3. ORDER BY last_name;
Output:
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)
Syntax
1. SELECT columns
2. FROM table1
3. INNER JOIN table2
4. ON table1.column = table2.column;
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.
Output
Syntax
1. SELECT columns
2. FROM table1
3. LEFT [OUTER] JOIN table2
4. ON table1.column = table2.column;
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.
Output
Syntax
1. SELECT columns
2. FROM table1
3. RIGHT [OUTER] JOIN table2
4. ON table1.column = table2.column;
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.
Output
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.
Output
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)]
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
Syntax
Output
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.
Output