Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
tables.
COLUMN ALIASES are used to make column headings in your result set easier
to read.
TABLE ALIASES are used to shorten your SQL to make it easier to read or
when you are performing a self join (ie: listing the same table more than once in
the FROM clause).
SYNTAX
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
NOTE
The corresponding expressions must have the same data type in the SELECT
statements. For example: expression1must be the same data type in both the first and
second SELECT statement.
For example:
SELECT supplier_id
FROM suppliers
UNION
SELECT supplier_id
FROM orders
ORDER BY supplier_id;
If you had the suppliers table populated with the following records:
supplier_id
supplier_name
1000
Microsoft
2000
Oracle
3000
Apple
4000
Samsung
order_id
order_date
supplier_id
2015-08-01
2000
2015-08-01
6000
2015-08-02
7000
2015-08-03
8000
SELECT supplier_id
FROM suppliers
UNION
SELECT supplier_id
FROM orders
ORDER BY supplier_id;
supplier_id
1000
2000
3000
4000
6000
7000
8000
combine the result sets of 2 or more SELECT statements. It does not remove duplicate
rows between the various SELECT statements (all rows are returned).
Each SELECT statement within the UNION ALL must have the same number of fields in
the result sets with similar data types.
E.GSELECT supplier_id
FROM suppliers
UNION ALL
SELECT supplier_id
FROM orders
ORDER BY supplier_id;
INTERSECT OPERATOR-
Explanation: The INTERSECT query will return the records in the blue shaded area.
These are the records that exist in both Dataset1 and Dataset2.
Each SQL statement within the SQL INTERSECT must have the same number of fields
in the result sets with similar data types.
SYNTAX
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
INTERSECT
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
Example- Slect name from cricket_team
Intersect
Slect name from football_team;
MINUS OPERATORThe SQL MINUS operator is used to return all rows in the first SELECT statement that
are not returned by the second SELECT statement. Each SELECT statement will define
a dataset. The MINUS operator will retrieve all records from the first dataset and then
remove from the results all records from the second dataset.
Minus Query
Explanation: The MINUS query will return the records in the blue shaded area. These
are the records that exist in Dataset1 and not in Dataset2.
Each SELECT statement within the MINUS query must have the same number of fields
in the result sets with similar data types.
TIP: The MINUS operator is not supported in all SQL databases. It can used in
databases such as Oracle.
For databases such as SQL Server, PostgreSQL, and SQLite, use the EXCEPT
operator to perform this type of query.
SYNTAX
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
MINUS
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
Difference B/W Minus & IntersectMinus- Eliminate the common rows or record from the result set.
Intersect- Returns common rows or records from the result set.
WHERE CLAUSEThe Oracle WHERE clause is used to filter the results from
a SELECT, INSERT, UPDATE, or DELETE statement.
SYNTAX
WHERE conditions;
In this Oracle WHERE clause example, we've used the WHERE clause to filter our
results from the customers table. The SELECT statement above would return all rows
from the customers table where the last_name is Anderson. Because the * is used in the
SELECT, all fields from the customers table would appear in the result set.
GROUP BY CLAUSE
The Oracle GROUP BY clause is used in a SELECT statement to collect data across
multiple records and group the results by one or more columns.
SYNTAX
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;
Because you have listed one column (the product field) in your SELECT statement that
is not encapsulated in the SUM function, you must use the GROUP BY clause.
The product field must, therefore, be listed in the GROUP BY clause.
HAVING CLAUSE
The Oracle HAVING clause is used in combination with the GROUP BY clause to
restrict the groups of returned rows to only those whose the condition is TRUE.
SYNTAX
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
HAVING having_condition;
Parameters or Arguments
expression1, expression2, ... expression_n
The expressions that are not encapsulated within an aggregate function and must be
included in the GROUP BY clause.
aggregate_function
It can be a function such as SUM, COUNT, MIN, MAX, or AVG functions.
aggregate_expression
This is the column or expression that the aggregate_function will be used against.
tables
The tables that you wish to retrieve records from. There must be at least one table listed
in the FROM clause.
WHERE conditions
Optional. These are the conditions for the records to be selected.
having_condition
This is a further condition applied only to the aggregated results to restrict the groups of
returned rows. Only those groups whose condition evaluates to TRUE will be included in
the result set.
Finally, let's look at how we could use the HAVING clause with the MAX function.
For example, you could also use the MAX function to return the name of each
department and the maximum salary in the department. The Oracle HAVING clause will
return only those departments whose maximum salary is greater than $45,000.
SELECT department, MAX(salary) AS "Highest salary"
FROM employees
GROUP BY department
HAVING MAX(salary) > 45000;
DISTINCT CLAUSE
The Oracle DISTINCT clause is used to remove duplicates from the result set. The
DISTINCT clause can only be used withSELECT statements.
SYNTAX
SELECT DISTINCT expressions
FROM tables
[WHERE conditions];
NOTE
When only one expression is provided in the DISTINCT clause, the query will return the
unique values for that expression.
When more than one expression is provided in the DISTINCT clause, the query will
retrieve unique combinations for the expressions listed.
In Oracle, the DISTINCT clause doesn't ignore NULL values. So when using the
DISTINCT clause in your SQL statement, your result set will include NULL as a distinct
value.
Let's look at the simplest Oracle DISTINCT clause example. We can use the Oracle
DISTINCT clause to return a single field that removes the duplicates from the result set.
For example:
SELECT DISTINCT state
FROM customers
WHERE last_name = 'Smith';
This Oracle DISTINCT example would return all unique state values from
the customers table where the customer'slast_name is 'Smith'.
ORDER BY CLAUSE
The SQL ORDER BY clause is used to sort the records in the result set for a SELECT
statement.
SYNTAX
SELECT expressions
FROM tables
[WHERE conditions]
ORDER BY expression [ ASC | DESC ];
NOTE
If the ASC or DESC modifier is not provided in the ORDER BY clause, the results will be
sorted by expression in ascending order. This is equivalent to ORDER
BY expression ASC.
This SQL ORDER BY example would return all records sorted by the supplier_city field
in ascending order and would be equivalent to the following SQL ORDER BY clause:
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city ASC;
This SQL ORDER BY example would return all records sorted by the supplier_city field
in descending order.
This SQL ORDER BY would return all records sorted by the supplier_city field in
descending order, since the supplier_city field is in position #1 in the result set and
would be equivalent to the following SQL ORDER BY clause:
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
This SQL ORDER BY would return all records sorted by the supplier_city field in
descending order, with a secondary sort by supplier_state in ascending order.
RANK FUNCTION
The Oracle/PLSQL RANK function returns the rank of a value in a group of values. It is
very similar to the DENSE_RANK function. However, the rank function can cause nonconsecutive rankings if the tested values are the same. Whereas, theDENSE_RANK
function will always result in consecutive rankings.
The RANK function can be used two ways - as an Aggregate function or as an Analytic
function.
NOTE
There must be the same number of expressions in the first expression list as there is in
the ORDER BY clause.
The expression lists match by position so the data types must be compatible between
the expressions in the first expression list as in the ORDER BY clause.
APPLIES TO
The RANK function can be used in the following versions of Oracle/PLSQL:
The SQL statement above would return the rank of an employee with a salary of $1,000
and a bonus of $500 from within the employees table.
APPLIES TO
The RANK function can be used in the following versions of Oracle/PLSQL:
The SQL statement above would return all employees who work in the Marketing
department and then calculate a rank for each unique salary in the Marketing
department. If two employees had the same salary, the RANK function would return the
same rank for both employees. However, this will cause a gap in the ranks (ie: nonconsecutive ranks). This is quite different from the DENSE_RANK function which
generates consecutive rankings.
DENSE_RANK FUNCTION
The Oracle/PLSQL DENSE_RANK function returns the rank of a row in a group of rows.
It is very similar to the RANK function. However, the RANK function can cause nonconsecutive rankings if the tested values are the same. Whereas, the DENSE_RANK
function will always result in consecutive rankings.
NOTE
There must be the same number of expressions in the first expression list as there is in
the ORDER BY clause.
The expression lists match by position so the data types must be compatible between
the expressions in the first expression list as in the ORDER BY clause.
APPLIES TO
The DENSE_RANK function can be used in the following versions of Oracle/PLSQL:
For example:
select DENSE_RANK(1000, 500) WITHIN GROUP (ORDER BY salary, bonus)
from employees;
The SQL statement above would return the dense rank of an employee with a salary of
$1,000 and a bonus of $500 from within the employees table.
APPLIES TO
The DENSE_RANK function can be used in the following versions of Oracle/PLSQL:
The SQL statement above would return all employees who work in the Marketing
department and then calculate a rank for each unique salary in the Marketing
department. If two employees had the same salary, the DENSE_RANK function would
return the same rank for both employees.