Sei sulla pagina 1di 22

ALIASES- SQL ALIASES can be used to create a temporary name for columns or

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).

The syntax to ALIAS A COLUMN in SQL is:- column_name AS alias_name


The syntax to ALIAS A TABLE in SQL is:- table_name alias_name

SET OPERATOR1. UNION


2. UNION ALL
3. INTERSECT
4. MINUS
Union- The SQL UNION operator is used to combine the result sets of 2 or more
SELECT statements. It removes duplicate rows between the various SELECT
statements.
Each SELECT statement within the UNION must have the same number of fields in the
result sets with similar data types.

WHAT IS THE DIFFERENCE BETWEEN UNION AND UNION ALL?

UNION removes duplicate rows.

UNION ALL does not remove duplicate rows.

SYNTAX
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

NOTE

There must be same number of expressions in both SELECT statements.

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;

In this SQL UNION operator example, if a supplier_id appeared in both


the suppliers and orders table, it would appear once in your result set. The
UNION operator removes duplicates. If you do not wish to remove duplicates, try
using the UNION ALL operator.

If you had the suppliers table populated with the following records:

supplier_id

supplier_name

1000

Microsoft

2000

Oracle

3000

Apple

4000

Samsung

And the orders table populated with the following records:

order_id

order_date

supplier_id

2015-08-01

2000

2015-08-01

6000

2015-08-02

7000

2015-08-03

8000

And you executed the following UNION ALL statement:

SELECT supplier_id

FROM suppliers

UNION

SELECT supplier_id

FROM orders

ORDER BY supplier_id;

You would get the following results:

supplier_id
1000
2000
3000
4000
6000
7000
8000

UNION ALL OPERATOR- The SQL UNION ALL operator is used to

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.

SYNTAXSELECT expression1, expression2, ... expression_n


FROM tables
[WHERE conditions]
UNION ALL

SELECT expression1, expression2, ... expression_n


FROM tables
[WHERE conditions];

E.GSELECT supplier_id
FROM suppliers
UNION ALL
SELECT supplier_id
FROM orders
ORDER BY supplier_id;

You would get the following results:


supplier_id
1000
2000
2000
3000
4000
6000
7000
8000

INTERSECT OPERATOR-

The SQL INTERSECT operator is used to


return the results of 2 or more SELECT statements. However, it only returns the rows
selected by all queries or data sets. If a record exists in one query and not in the other, it
will be omitted from the INTERSECT results.

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;

EXAMPLE - WITH SINGLE CONDITION


It is difficult to explain the syntax for the Oracle WHERE clause, so let's look at some
examples.
SELECT *
FROM customers
WHERE last_name = 'Anderson';

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;

EXAMPLE - USING SUM FUNCTION


Let's look at an Oracle GROUP BY query example that uses the SUM function.
This Oracle GROUP BY example uses the SUM function to return the name of the
product and the total sales (for the product).
SELECT product, SUM(sale) AS "Total sales"
FROM order_details
GROUP BY product;

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.

EXAMPLE - USING COUNT FUNCTION


Let's look at how we could use the GROUP BY clause with the COUNT function.
This GROUP BY example uses the COUNT function to return the category and the
number of suppliers (in that category) that have over 45 available_products.
SELECT category, COUNT(*) AS "Number of suppliers"
FROM suppliers
WHERE available_products > 45
GROUP BY category;

EXAMPLE - USING MIN FUNCTION


Let's next look at how we could use the GROUP BY clause with the MIN function.
This GROUP BY example uses the MIN function to return the name of each department
and the minimum salary in the department.
SELECT department, MIN(salary) AS "Lowest salary"
FROM employees
GROUP BY department;

EXAMPLE - USING MAX FUNCTION


Finally, let's look at how we could use the GROUP BY clause with the MAX function.
This GROUP BY example uses the MAX function to return the name of each
department and the maximum salary in the department.
SELECT department, MAX(salary) AS "Highest salary"
FROM employees
GROUP BY department;

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.

EXAMPLE - USING SUM FUNCTION


Let's look at an Oracle HAVING clause example that uses the SUM function.
You could also use the SUM function to return the name of the department and the total
sales (in the associated department). The Oracle HAVING clause will filter the results so
that only departments with sales greater than $25,000 will be returned.

SELECT department, SUM(sales) AS "Total sales"


FROM order_details
GROUP BY department
HAVING SUM(sales) > 25000;

EXAMPLE - USING COUNT FUNCTION


Let's look at how we could use the HAVING clause with the COUNT function.
You could use the COUNT function to return the name of the department and the
number of employees (in the associated department) that make under $49,500 / year.
The Oracle HAVING clause will filter the results so that only departments with more than
10 employees will be returned.
SELECT department, COUNT(*) AS "Number of employees"
FROM employees
WHERE salary < 49500
GROUP BY department
HAVING COUNT(*) > 10;

EXAMPLE - USING MIN FUNCTION


Let's next look at how we could use the HAVING clause with the MIN function.
You could also use the MIN function to return the name of each department and the
minimum salary in the department. The Oracle HAVING clause will return only those
departments where the minimum salary is less than $42,000.
SELECT department, MIN(salary) AS "Lowest salary"
FROM employees
GROUP BY department
HAVING MIN(salary) < 42000;

EXAMPLE - USING MAX FUNCTION

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.

EXAMPLE - WITH SINGLE EXPRESSION

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'.

EXAMPLE - WITH MULTIPLE EXPRESSIONS


Let's look at how you might use the Oracle DISTINCT clause to remove duplicates from
more than one field in your SELECT statement.
For example:
SELECT DISTINCT city, state
FROM customers
WHERE total_orders > 10
ORDER BY city;

This Oracle DISTINCT clause example would return each


unique city and state combination from the customers table where the total_orders is
greater than 10. The results are sorted in ascending order by city.
In this case, the DISTINCT applies to each field listed after the DISTINCT keyword, and
therefore returns distinct combinations

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.

EXAMPLE - SORTING WITHOUT USING ASC/DESC ATTRIBUTE


The SQL ORDER BY clause can be used without specifying the ASC or DESC value.
When this attribute is omitted from the SQL ORDER BY clause, the sort order is
defaulted to ASC or ascending order.
For example:
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city;

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;

Most programmers omit the ASC attribute if sorting in ascending order.

EXAMPLE - SORTING IN DESCENDING ORDER


When sorting your result set in descending order, you use the DESC attribute in your
ORDER BY clause as follows:
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC;

This SQL ORDER BY example would return all records sorted by the supplier_city field
in descending order.

EXAMPLE - SORTING BY RELATIVE POSITION


You can also use the SQL ORDER BY clause to sort by relative position in the result
set, where the first field in the result set is 1. The next field is 2, and so on.
For example:
SELECT supplier_city
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY 1 DESC;

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'

ORDER BY supplier_city DESC;

EXAMPLE - USING BOTH ASC AND DESC ATTRIBUTES


When sorting your result set using the SQL ORDER BY clause, you can use the ASC
and DESC attributes in a single SQL SELECT statement.
For example:
SELECT supplier_city, supplier_state
FROM suppliers
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC, supplier_state ASC;

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.

RANK FUNCTION SYNTAX #1 - USED AS AN AGGREGATE


FUNCTION
As an Aggregate function, the RANK function returns the rank of a row within a group
of rows.
The syntax for the RANK function when used as an Aggregate function is:

RANK( expr1 [, expr2, ... expr_n ] ) WITHIN GROUP ( ORDER BY expr1 [,


expr_2, ... expr_n ] )
Parameters or Arguments
expr1
First expression which identifies a unique row in the group.
expr2, ... expr_n
Optional. Additional expressions which identifies a unique row in the group.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE (AS AN AGGREGATE FUNCTION)


Let's look at some Oracle RANK function examples and explore how to use the RANK
function in Oracle/PLSQL.
For example:
select RANK(1000, 500) WITHIN GROUP (ORDER BY salary, bonus)
from employees;

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.

RANK FUNCTION SYNTAX #2 - USED AS AN ANALYTIC


FUNCTION
As an Analytic function, the RANK function returns the rank of each row of a query with
respective to the other rows.
The syntax for the RANK function when used as an Analytic function is:
rank() OVER ( [ query_partition_clause] ORDER BY clause )

APPLIES TO
The RANK function can be used in the following versions of Oracle/PLSQL:

Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE (AS AN ANALYTIC FUNCTION)


select employee_name, salary,
RANK() OVER (PARTITION BY department ORDER BY salary)
from employees
where department = 'Marketing';

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.

The DENSE_RANK function can be used two ways - as an Aggregate function or as an


Analytic function.

DENSE_RANK FUNCTION SYNTAX #1 - USED AS AN


AGGREGATE FUNCTION
As an Aggregate function, the DENSE_RANK function returns the dense rank of a row
within a group of rows.
The syntax for the DENSE_RANK function when used as an Aggregate function is:
DENSE_RANK( expression1, ... expression_n ) WITHIN GROUP ( ORDER BY
expression1, ... expression_n )
Parameters or Arguments
expression1 .. expression_n
One or more expressions which identify a unique row in the group.

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:

Oracle 12c, Oracle 11g, Oracle 10g, Oracle 9i

EXAMPLE (AS AN AGGREGATE FUNCTION)


Let's look at some Oracle DENSE_RANK function examples and explore how to use the
DENSE_RANK function in 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.

DENSE_RANK FUNCTION SYNTAX #2 - USED AS AN ANALYTIC


FUNCTION
As an Analytic function, the DENSE_RANK function returns the rank of each row of a
query with respective to the other rows.
The syntax for the DENSE_RANK function when used as an Analytic function is:
DENSE_RANK() OVER ( [ query_partition_clause] ORDER BY clause )

APPLIES TO
The DENSE_RANK function can be used in the following versions of Oracle/PLSQL:

Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

EXAMPLE (AS AN ANALYTIC FUNCTION)


Let's look at some Oracle DENSE_RANK function examples and explore how to use the
DENSE_RANK function in Oracle/PLSQL.
For example:
select employee_name, salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary)
from employees
where department = 'Marketing';

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.

Potrebbero piacerti anche