DBMS
DBMS
O
P
-
7
0
M
O
S
T
I
M
P
O
R
T
A
N
T
S
Q
L
Q
U
E
R
I
E
S
I
N
2
0
2
3
SQL is incredibly powerful, and like every well-made development tool, it
has a few commands which it’s vital for a good developer to know. Here is
a list of SQL queries that are really important for coding & optimization.
Each of the queries in the SQL tutorial is consequential to almost every
system that interacts with an SQL database.
Syntax
1SELECT COALESCE(NULL,NULL,'ByteScout',NULL,'Byte')
Output
ByteScout
Syntax
1SELECT CONVERT(int, 27.64)
Output
27
44. Query_partition_clause
The query_partition_clause breaks the output set into distributions, or
collections, of data. The development of the analytic query is limited to
the confines forced by these partitions, related to the process a GROUP BY
clause modifies the performance of an aggregate function. If the
query_partition_clause is eliminated, the entire output collection is
interpreted as a separate partition.
The following query applies an OVER clause, so the average
displayed is based on all the records of the output set.
1SELECT eno, dno, salary,
2AVG(salary) OVER () AS avg_sal
3FROM employee;
4
5EO DNO SALARY AVG_SAL
6---------- ---------- ---------- ----------
77364 20 900 2173.21428
87494 30 1700 2173.21428
97522 30 1350 2173.21428
7567 20 3075 2173.21428
1
7652 30 1350 2173.21428
07699 30 2950 2173.21428
17783 10 2550 2173.21428
17789 20 3100 2173.21428
17838 10 5100 2173.21428
27845 30 1600 2173.21428
17877 20 1200 2173.21428
37901 30 1050 2173.21428
17903 20 3100 2173.21428
47935 10 1400 2173.21428
1
5
1
6
1
7
1
8
1
9
2
0
For example,
1Select * from Employee A where rownum <=8
2union
3select * from (Select * from Employee A order by rowid desc) where rownum <=8;
The above SQL query will give you the last eight records from the
employee table where rownum is a pseudo column. It indexes the
data in an output set.
46. LAG
The LAG is applied to get data from a prior row. This is an analytical
function. For example, the following query gives the salary from the prior
row to compute the difference between the salary of the current row and
that of the prior row. In this query, the ORDER BY of the LAG
function is applied. The default is 1 if you do not define offset. The
arbitrary default condition is given if the offset moves past the range of
the window. The default is null if you do not define default.
Syntax
1SELECT dtno,
2 eno,
3 emname,
4 job,
5 salary,
6 LAG(sal, 1, 0) OVER (PARTITION BY dtno ORDER BY salary) AS salary_prev
7FROM employee;
Output
1DTNO ENO ENAME JOB SAL SAL_PREV
2---------- ---------- ---------- --------- ---------- ----------
310 7931 STEVE CLERK 1300 0
4
5
6
7
8
9
1
0
110 7783 JOHN MANAGER 2450 1300
110 7834 KING PRESIDENT 5000 2450
120 7364 ROBIN CLERK 800 0
20 7876 BRIAN CLERK 1100 800
2
20 7567 SHANE MANAGER 2975 1100
1
20 7784 SCOTT ANALYST 3000 2975
320 7908 KANE ANALYST 3000 3000
130 7900 JAMES CLERK 950 0
430 7651 CONNER SALESMAN 1250 950
130 7522 MATTHEW SALESMAN 1250 1250
530 7843 VIVIAN SALESMAN 1500 1250
130 7494 ALLEN SALESMAN 1600 1500
630 7695 GLEN MANAGER 2850 1600
47. LEAD
The LEAD is also an analytical query that is applied to get data from rows
extra down the output set. The following query gives the salary from the
next row to compute the deviation between the salary of the prevailing
row and the subsequent row. The default is 1 if you do not define offset.
The arbitrary default condition is given if the offset moves past the range
of the window. The default is null if you do not define default.
1SELECT eno,
2 empname,
3 job,
4 salary,
5 LEAD(salary, 1, 0) OVER (ORDER BY salary) AS salary_next,
6 LEAD(salary, 1, 0) OVER (ORDER BY salary) - salary AS salary_diff
7FROM employee;
8
9ENO EMPNAME JOB SALARY SALARY_NEXT SALARY_DIFF
1---------- ---------- --------- ---------- ---------- ----------
7369 STEVE CLERK 800 950 150
0
7900 JEFF CLERK 950 1100 150
17876 ADAMS CLERK 1100 1250 150
17521 JOHN SALESMAN 1250 1250 0
17654 MARK SALESMAN 1250 1300 50
27934 TANTO CLERK 1300 1500 200
17844 MATT SALESMAN 1500 1600 100
37499 ALEX SALESMAN 1600 2450 850
17782 BOON MANAGER 2450 2850 400
47698 BLAKE MANAGER 2850 2975 125
1
5
1
6
1
7
1
8
1
9
2
0
2
1
2
2
27566 JONES MANAGER 2975 3000 25
37788 SCOTT ANALYST 3000 3000 0
27902 FORD ANALYST 3000 5000 2000
47839 KING PRESIDENT 5000 0 -5000
48. PERCENT_RANK
The PERCENT_RANK analytic query. The ORDER BY clause is necessary for
this query. Excluding a partitioning clause from the OVER clause
determines the entire output set is interpreted as a separate
partition. The first row of the standardized set is indicated 0 and
the last row of the set is indicated 1. For example, the SQL query
example gives the following output.
Syntax
1SELECT
2 prdid, SUM(amount),
3 PERCENT_RANK() OVER (ORDER BY SUM(amount) DESC) AS percent_rank
4 FROM sales
5 GROUP BY prdid
6 ORDER BY prdid;
Output
1PRDID SUM(AMOUNT) PERCENT_RANK
2----------- ----------- ------------
3 1 22623.5 0
4 2 223927.08 1
49. MIN
Utilizing a blank OVER clause converts the MIN into an analytic
function. This is also an analytical query. In this, the entire result set is
interpreted as a single partition. It gives you the minimum salary for all
employees and their original data. For example, the following query is
displaying the use of MIN in the Select query.
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1SELECT eno,
3 empname,
1 dtno,
4 salary,
1 MIN(salary) OVER (PARTITION BY dtno) AS min_result
5FROM employee;
1
6 ENO EMPNAME DTNO SALARY MIN_RESULT
1---------- ---------- ---------- ---------- ---------------
7 7782 CLARK 10 2450 1300
1 7839 KING 10 5000 1300
8 7934 MILLER 10 1300 1300
7566 JONES 20 2975 800
1
7902 FORD 20 3000 800
9
7876 ADAMS 20 1100 800
2
7369 SMITH 20 800 800
0 7788 SCOTT 20 3000 800
2 7521 WARD 30 1250 950
1 7844 TURNER 30 1500 950
2 7499 ALLEN 30 1600 950
2 7900 JAMES 30 950 950
2 7698 BLAKE 30 2850 950
3 7654 MARTIN 30 1250 950
50. MAX
Using a blank row OVER clause converts the MAX into an analytic
function. The lack of a partitioning clause indicates the entire
output set is interpreted as a separate partition. This gives the
maximum salary for all employees and their original data. For example,
the following query displays the use of MAX in the select query.
1
2
3
4
5
6
7
8
9
1
0
1
1
1
2
1SELECT eno,
3 empname,
1 dtno,
4 salary,
1 MAX(salary) OVER () AS max_result
5FROM employee;
1
6 ENO EMPNAME DTNO SALARY MAX_RESULT
1---------- ---------- ---------- ---------- ----------
7 7369 SMITH 20 800 3000
1 7499 ALLEN 30 1600 3000
8 7521 WARD 30 1250 3000
7566 JONES 20 2975 3000
1
7654 MARTIN 30 1250 3000
9
7698 BLAKE 30 2850 3000
2
7782 CLARK 10 2450 3000
0 7788 SCOTT 20 3000 3000
2 7839 KING 10 5000 3000
1 7844 TURNER 30 1500 3000
2 7876 ADAMS 20 1100 3000
2 7900 JAMES 30 950 3000
2 7902 FORD 20 3000 3000
3 7934 MILLER 10 1300 3000
Example
1SELECT empid,
2 name,
3 dno,
4 salary,
5 job,
6 CORR(SYSDATE - joiningdate, salary) OVER () AS my_corr_val
7FROM employee;
Example
1SELECT empid,
2 name,
3 dno,
4 salary,
5 NTILE(6) OVER (ORDER BY salary) AS container_no
6FROM employee;
54. VARIANCE, VAR_POP, and VAR_SAMP Query
The VARIANCE, VAR_POP, and VAR_SAMP are aggregate functions. These
are utilized to determine the variance, group variance, and sample
variance of a collection of data individually. As aggregate queries or
functions, they decrease the number of rows, therefore the expression
“aggregate”. If the data isn’t arranged we change the total rows in the
Employee table to a separate row with the aggregated values. For
example, the following query is displaying the use of these functions:
1SELECT VARIANCE(salary) AS var_salary,
2 VAR_POP(salary) AS pop_salary,
3 VAR_SAMP(salary) AS samp_salary
4FROM employee;
5
6VAR_SALARY POP_SALARY SAMP_SALARY
7------------ ----------- ------------
81479414.97 1588574.81 1388717.27
Syntax
1MEASURES STRT.tstamp AS initial_tstamp,
2 LAST(UP.tstamp) AS first_tstamp,
3 LAST(DOWN.tstamp) AS finished_tstamp
Example
1DEFINE
2 UP AS UP.products_sold > PREV(UP.products_sold),
3 FLAT AS FLAT.products_sold = PREV(FLAT.products_sold),
4 DOWN AS DOWN.products_sold < PREV(DOWN.products_sold)
57. FIRST_VALUE
The simplest way to get analytic functions is to begin by studying
aggregate functions. An aggregate function collects or gathers data from
numerous rows into a unique result row. For instance, users might apply
the AVG function to get an average of all the salaries in the EMPLOYEE
table. Let’s take a look at how First_Value can be used. The primary
explanation for the FIRST_VALUE analytic function is displayed below.
Syntax:
1FIRST_VALUE
2 { (expr) [NULLS ]
3 | (expr [NULLS ])
4 }
5 OVER (analytic clause)
Example
1SELECT eno,
2 dno,
3 salary,
4 FIRST_VALUE(salary) IGNORE NULLS
OVER (PARTITION BY dno ORDER BY salary) AS lowest_salary_in_dept
5
6FROM employee;
The above query will ignore null values.
58. LAST_VALUE
The primary explanation for the LAST_VALUE analytic query or function is
displayed below.
1Syntax: LAST_VALUE
2 { (expr) [ { NULLS ]
3 | (expr [ NULLS ])
4 OVER (analytic clause)
The LAST_VALUE analytic query is related to the LAST analytic function.
The function enables users to get the last output from an organized
column. Applying the default windowing to the output can be surprising.
For example,
1SELECT eno,
2 dno,
3 salary,
4 LAST_VALUE(salary) IGNORE NULLS
5 OVER (PARTITION BY dno ORDER BY salary) AS highest_salary_in_dept
6FROM employee;
59. Prediction
The design sample foretells the gender and age of clients who are most
expected to adopt an agreement card (target = 1). The PREDICTION
function takes the price matrix correlated with the design and applies for
marital status, and house size as predictors. The syntax of the
PREDICTION function can also apply a piece of arbitrary GROUPING
information when getting a partitioned model.
1
2
3
SELECT client_gender, COUNT(*) AS ct, ROUND(AVG(age)) AS average_age
4
FROM mining_data_shop
5
WHERE PREDICTION(sample COST MODEL
6 USING client_marital_status, house_size) = 1
7 GROUP BY client_gender
8 ORDER BY client_gender;
9
1CUST_GENDER CNT AVG_AGE
0------------ ---------- ----------
1F 270 40
1M 585 41
60. CLUSTER_SET
CLUSTER_SET can get the data in one of the couple steps: It can use a
mining type object to the information, or it can mine the data by
performing an analytic clause that creates and uses one or more moving
mining patterns.
This example enumerates the properties that have the biggest influence
on cluster distribution for client ID 1000. The query requests the
CLUSTER_DETAILS and CLUSTER_SET functions, which use the clustering
model my_sample.
Example
1
SELECT S.cluster_id, prob,
2 CLUSTER_DETAILS(my_sample, S.cluster_id, 7 USING T.*) kset
3FROM
4 (SELECT v.*, CLUSTER_SET(my_sample, USING *) nset
5 FROM mining_data
6 WHERE client_id = 1000) T,
7 TABLE(T.nset) Q
8ORDER BY 2 DESC;
A cluster is a group table that distributes the corresponding data blocks
i.e. all the tables are actually put together. For example, EMPLOYEE and
DEPARTMENT tables are connected to the DNO column. If you cluster
them, it will actually store all rows in the same data blocks.
Syntax
1WITH all_emp
2AS
3(
4SELECT empId, BossId, FirstName, LastName
5FROM Emp
6WHERE BossId is NULL
7
UNION ALL
8
9
1
0
1
1
1
2SELECT e.empId, e.BossId, e.FirstName, e.LastName
1FROM Emp e INNER JOIN all_emp r
3ON e.BossId = r.Id
1)
4SELECT * FROM all_emp
62. NANVL
This function is utilized to deliver an optional value n1 if the inserted value
n2 is NaN (not a number), and gives n2 if n2 is not a number. This
function is used only for type BINARY_FLOAT. The following query is
displaying its use:
Example
1SELECT bin_float, NANVL(bin_float,0)
2FROM my_demo_table;
63. WIDTH_BUCKET
This function is used to obtain the bucket number. In this, it gives the
value of the expression that would come under after being assessed. The
following query is displaying its use:
Example
1SELECT emp_id, first_name,last_name,dept_id,mgr_id,
2WIDTH_BUCKET(department_id,20,40,10) "Exists in Dept"
3FROM emp
4WHERE mgr_id < 300
5ORDER BY "Exists in Dept";
64. COSH
This function is used to deliver the hyperbolic cosine of a number. It
accepts all numeric or non-numeric data types as an argument. The
following query is displaying its use:
Example
1SELECT COSH(0) "COSH of 0" FROM DUAL;
65. SOUNDEX
The SOUNDEX function delivers a character string comprising the
description of char. It allows users to match words that are spelled
antagonistically, but sound similar in English. It does not support CLOB.
The following query is displaying its use:
Example
1SELECT last_name, first_name
2FROM hr.emp
3WHERE SOUNDEX(last_name)
4= SOUNDEX('SCOTTY');
66. TZ_OFFSET
The TZ_OFFSET gives the time zone offset identical to the case based on
the date the statement is given. The following query is displaying its use:
Example
1SELECT TZ_OFFSET('US/Eastern') FROM DUAL;
67. CARDINALITY
CARDINALITY is utilized to obtain the number of components in a nested
table. It is supported in different versions. The following query is
displaying its use:
Example
1SELECT product_id, CARDINALITY(ad_mydocs_get)
2FROM my_media_table;
68. DUMP
DUMP is one of the important string/char functions. It is utilized to get a
VARCHAR2 value. The value delivered defines the data type code. The
following query is displaying its use:
Example
1SELECT DUMP('pqr', 1033)
2FROM DUAL;
69. PATH
PATH is applied simply with the UNDER_PATH and EQUALS_PATH
requirements. It gives the corresponding path that points to the resource
defined in the main state. The following query is displaying its use:
Example
1SELECT ANY_PATH FROM RESOURCE_VIEW
2WHERE EQUALS_PATH(res, '/sys/schemas/OE/www.pqr.com')=3;
70. UNISTR
UNISTR accepts an expression that determines character data and
delivers it in the general character set. It gives support to the Unicode
string literals by allowing users to define the Unicode value. The following
query is displaying its use:
Example
1SELECT UNISTR('pqr\00e4\00f3\00f9') FROM DUAL;
T
O
P
-
7
0
M
O
S
T
I
M
P
O
R
T
A
N
T
S
Q
L
Q
U
E
R
I
E
S
I
N
2
0
2
3
SQL is incredibly powerful, and like every well-made development tool, it
has a few commands which it’s vital for a good developer to know. Here is
a list of SQL queries that are really important for coding & optimization.
Each of the queries in the SQL tutorial is consequential to almost every
system that interacts with an SQL database.
Syntax
1SELECT COALESCE(NULL,NULL,'ByteScout',NULL,'Byte')
Output
ByteScout
Syntax
1SELECT CONVERT(int, 27.64)
Output
27
For example,
1Select * from Employee A where rownum <=8
2union
3select * from (Select * from Employee A order by rowid desc) where rownum <=8;
The above SQL query will give you the last eight records from the
employee table where rownum is a pseudo column. It indexes the
data in an output set.
46. LAG
The LAG is applied to get data from a prior row. This is an analytical
function. For example, the following query gives the salary from the prior
row to compute the difference between the salary of the current row and
that of the prior row. In this query, the ORDER BY of the LAG
function is applied. The default is 1 if you do not define offset. The
arbitrary default condition is given if the offset moves past the range of
the window. The default is null if you do not define default.
Syntax
1SELECT dtno,
2 eno,
3 emname,
4 job,
5 salary,
6 LAG(sal, 1, 0) OVER (PARTITION BY dtno ORDER BY salary) AS salary_prev
7FROM employee;
Output
1DTNO ENO ENAME JOB SAL SAL_PREV
2---------- ---------- ---------- --------- ---------- ----------
310 7931 STEVE CLERK 1300 0
410 7783 JOHN MANAGER 2450 1300
510 7834 KING PRESIDENT 5000 2450
620 7364 ROBIN CLERK 800 0
720 7876 BRIAN CLERK 1100 800
820 7567 SHANE MANAGER 2975 1100
20 7784 SCOTT ANALYST 3000 2975
9
20 7908 KANE ANALYST 3000 3000
130 7900 JAMES CLERK 950 0
030 7651 CONNER SALESMAN 1250 950
130 7522 MATTHEW SALESMAN 1250 1250
130 7843 VIVIAN SALESMAN 1500 1250
1
2
1
3
1
4
1
5
130 7494 ALLEN SALESMAN 1600 1500
630 7695 GLEN MANAGER 2850 1600
47. LEAD
The LEAD is also an analytical query that is applied to get data from rows
extra down the output set. The following query gives the salary from the
next row to compute the deviation between the salary of the prevailing
row and the subsequent row. The default is 1 if you do not define offset.
The arbitrary default condition is given if the offset moves past the range
of the window. The default is null if you do not define default.
1SELECT eno,
2 empname,
3 job,
4 salary,
5 LEAD(salary, 1, 0) OVER (ORDER BY salary) AS salary_next,
6 LEAD(salary, 1, 0) OVER (ORDER BY salary) - salary AS salary_diff
7FROM employee;
8
9ENO EMPNAME JOB SALARY SALARY_NEXT SALARY_DIFF
1---------- ---------- --------- ---------- ---------- ----------
7369 STEVE CLERK 800 950 150
0
7900 JEFF CLERK 950 1100 150
17876 ADAMS CLERK 1100 1250 150
17521 JOHN SALESMAN 1250 1250 0
17654 MARK SALESMAN 1250 1300 50
27934 TANTO CLERK 1300 1500 200
17844 MATT SALESMAN 1500 1600 100
37499 ALEX SALESMAN 1600 2450 850
17782 BOON MANAGER 2450 2850 400
47698 BLAKE MANAGER 2850 2975 125
17566 JONES MANAGER 2975 3000 25
57788 SCOTT ANALYST 3000 3000 0
17902 FORD ANALYST 3000 5000 2000
67839 KING PRESIDENT 5000 0 -5000
1
7
1
8
1
9
2
0
2
1
2
2
2
3
2
4
48. PERCENT_RANK
The PERCENT_RANK analytic query. The ORDER BY clause is necessary for
this query. Excluding a partitioning clause from the OVER clause
determines the entire output set is interpreted as a separate
partition. The first row of the standardized set is indicated 0 and
the last row of the set is indicated 1. For example, the SQL query
example gives the following output.
Syntax
1SELECT
2 prdid, SUM(amount),
3 PERCENT_RANK() OVER (ORDER BY SUM(amount) DESC) AS percent_rank
4 FROM sales
5 GROUP BY prdid
6 ORDER BY prdid;
Output
1PRDID SUM(AMOUNT) PERCENT_RANK
2----------- ----------- ------------
3 1 22623.5 0
4 2 223927.08 1
49. MIN
Utilizing a blank OVER clause converts the MIN into an analytic
function. This is also an analytical query. In this, the entire result set is
interpreted as a single partition. It gives you the minimum salary for all
employees and their original data. For example, the following query is
displaying the use of MIN in the Select query.
1SELECT eno,
2 empname,
3 dtno,
4 salary,
MIN(salary) OVER (PARTITION BY dtno) AS min_result
5
6
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5FROM employee;
1
6 ENO EMPNAME DTNO SALARY MIN_RESULT
1---------- ---------- ---------- ---------- ---------------
7 7782 CLARK 10 2450 1300
1 7839 KING 10 5000 1300
8 7934 MILLER 10 1300 1300
7566 JONES 20 2975 800
1
7902 FORD 20 3000 800
9
7876 ADAMS 20 1100 800
2
7369 SMITH 20 800 800
0 7788 SCOTT 20 3000 800
2 7521 WARD 30 1250 950
1 7844 TURNER 30 1500 950
2 7499 ALLEN 30 1600 950
2 7900 JAMES 30 950 950
2 7698 BLAKE 30 2850 950
3 7654 MARTIN 30 1250 950
50. MAX
Using a blank row OVER clause converts the MAX into an analytic
function. The lack of a partitioning clause indicates the entire
output set is interpreted as a separate partition. This gives the
maximum salary for all employees and their original data. For example,
the following query displays the use of MAX in the select query.
1SELECT eno,
2 empname,
3 dtno,
4 salary,
5 MAX(salary) OVER () AS max_result
6FROM employee;
7
8
9
1
0
1
1
1
2
1
3
1
4
1
5
1
6 ENO EMPNAME DTNO SALARY MAX_RESULT
1---------- ---------- ---------- ---------- ----------
7 7369 SMITH 20 800 3000
1 7499 ALLEN 30 1600 3000
8 7521 WARD 30 1250 3000
7566 JONES 20 2975 3000
1
7654 MARTIN 30 1250 3000
9
7698 BLAKE 30 2850 3000
2
7782 CLARK 10 2450 3000
0 7788 SCOTT 20 3000 3000
2 7839 KING 10 5000 3000
1 7844 TURNER 30 1500 3000
2 7876 ADAMS 20 1100 3000
2 7900 JAMES 30 950 3000
2 7902 FORD 20 3000 3000
3 7934 MILLER 10 1300 3000
Example
1SELECT empid,
2 name,
3 dno,
4 salary,
5 job,
6 CORR(SYSDATE - joiningdate, salary) OVER () AS my_corr_val
7FROM employee;
Example
1SELECT empid,
2 name,
3 dno,
4 salary,
5 NTILE(6) OVER (ORDER BY salary) AS container_no
6FROM employee;
Syntax
1MEASURES STRT.tstamp AS initial_tstamp,
2 LAST(UP.tstamp) AS first_tstamp,
3 LAST(DOWN.tstamp) AS finished_tstamp
Example
1DEFINE
2 UP AS UP.products_sold > PREV(UP.products_sold),
3 FLAT AS FLAT.products_sold = PREV(FLAT.products_sold),
4 DOWN AS DOWN.products_sold < PREV(DOWN.products_sold)
57. FIRST_VALUE
The simplest way to get analytic functions is to begin by studying
aggregate functions. An aggregate function collects or gathers data from
numerous rows into a unique result row. For instance, users might apply
the AVG function to get an average of all the salaries in the EMPLOYEE
table. Let’s take a look at how First_Value can be used. The primary
explanation for the FIRST_VALUE analytic function is displayed below.
Syntax:
1FIRST_VALUE
2 { (expr) [NULLS ]
3 | (expr [NULLS ])
4 }
5 OVER (analytic clause)
Example
1SELECT eno,
2 dno,
3 salary,
4 FIRST_VALUE(salary) IGNORE NULLS
5 OVER (PARTITION BY dno ORDER BY salary) AS lowest_salary_in_dept
6FROM employee;
The above query will ignore null values.
58. LAST_VALUE
The primary explanation for the LAST_VALUE analytic query or function is
displayed below.
1Syntax: LAST_VALUE
2 { (expr) [ { NULLS ]
3 | (expr [ NULLS ])
4 OVER (analytic clause)
The LAST_VALUE analytic query is related to the LAST analytic function.
The function enables users to get the last output from an organized
column. Applying the default windowing to the output can be surprising.
For example,
1SELECT eno,
2 dno,
3 salary,
4 LAST_VALUE(salary) IGNORE NULLS
5 OVER (PARTITION BY dno ORDER BY salary) AS highest_salary_in_dept
6FROM employee;
59. Prediction
The design sample foretells the gender and age of clients who are most
expected to adopt an agreement card (target = 1). The PREDICTION
function takes the price matrix correlated with the design and applies for
marital status, and house size as predictors. The syntax of the
PREDICTION function can also apply a piece of arbitrary GROUPING
information when getting a partitioned model.
1
2
3
SELECT client_gender, COUNT(*) AS ct, ROUND(AVG(age)) AS average_age
4
FROM mining_data_shop
5
WHERE PREDICTION(sample COST MODEL
6 USING client_marital_status, house_size) = 1
7 GROUP BY client_gender
8 ORDER BY client_gender;
9
1CUST_GENDER CNT AVG_AGE
0------------ ---------- ----------
1F 270 40
1M 585 41
60. CLUSTER_SET
CLUSTER_SET can get the data in one of the couple steps: It can use a
mining type object to the information, or it can mine the data by
performing an analytic clause that creates and uses one or more moving
mining patterns.
This example enumerates the properties that have the biggest influence
on cluster distribution for client ID 1000. The query requests the
CLUSTER_DETAILS and CLUSTER_SET functions, which use the clustering
model my_sample.
Example
1
SELECT S.cluster_id, prob,
2 CLUSTER_DETAILS(my_sample, S.cluster_id, 7 USING T.*) kset
3FROM
4 (SELECT v.*, CLUSTER_SET(my_sample, USING *) nset
5 FROM mining_data
6 WHERE client_id = 1000) T,
7 TABLE(T.nset) Q
8ORDER BY 2 DESC;
A cluster is a group table that distributes the corresponding data blocks
i.e. all the tables are actually put together. For example, EMPLOYEE and
DEPARTMENT tables are connected to the DNO column. If you cluster
them, it will actually store all rows in the same data blocks.
Syntax
1
2
3
4
5
6
7WITH all_emp
8AS
(
9
SELECT empId, BossId, FirstName, LastName
1
FROM Emp
0WHERE BossId is NULL
1
1UNION ALL
1
2SELECT e.empId, e.BossId, e.FirstName, e.LastName
1FROM Emp e INNER JOIN all_emp r
3ON e.BossId = r.Id
1)
4SELECT * FROM all_emp
62. NANVL
This function is utilized to deliver an optional value n1 if the inserted value
n2 is NaN (not a number), and gives n2 if n2 is not a number. This
function is used only for type BINARY_FLOAT. The following query is
displaying its use:
Example
1SELECT bin_float, NANVL(bin_float,0)
2FROM my_demo_table;
63. WIDTH_BUCKET
This function is used to obtain the bucket number. In this, it gives the
value of the expression that would come under after being assessed. The
following query is displaying its use:
Example
1SELECT emp_id, first_name,last_name,dept_id,mgr_id,
2WIDTH_BUCKET(department_id,20,40,10) "Exists in Dept"
3FROM emp
4WHERE mgr_id < 300
5ORDER BY "Exists in Dept";
64. COSH
This function is used to deliver the hyperbolic cosine of a number. It
accepts all numeric or non-numeric data types as an argument. The
following query is displaying its use:
Example
1SELECT COSH(0) "COSH of 0" FROM DUAL;
65. SOUNDEX
The SOUNDEX function delivers a character string comprising the
description of char. It allows users to match words that are spelled
antagonistically, but sound similar in English. It does not support CLOB.
The following query is displaying its use:
Example
1SELECT last_name, first_name
2FROM hr.emp
3WHERE SOUNDEX(last_name)
4= SOUNDEX('SCOTTY');
66. TZ_OFFSET
The TZ_OFFSET gives the time zone offset identical to the case based on
the date the statement is given. The following query is displaying its use:
Example
1SELECT TZ_OFFSET('US/Eastern') FROM DUAL;
67. CARDINALITY
CARDINALITY is utilized to obtain the number of components in a nested
table. It is supported in different versions. The following query is
displaying its use:
Example
1SELECT product_id, CARDINALITY(ad_mydocs_get)
2FROM my_media_table;
68. DUMP
DUMP is one of the important string/char functions. It is utilized to get a
VARCHAR2 value. The value delivered defines the data type code. The
following query is displaying its use:
Example
1SELECT DUMP('pqr', 1033)
2FROM DUAL;
69. PATH
PATH is applied simply with the UNDER_PATH and EQUALS_PATH
requirements. It gives the corresponding path that points to the resource
defined in the main state. The following query is displaying its use:
Example
1SELECT ANY_PATH FROM RESOURCE_VIEW
2WHERE EQUALS_PATH(res, '/sys/schemas/OE/www.pqr.com')=3;
70. UNISTR
UNISTR accepts an expression that determines character data and
delivers it in the general character set. It gives support to the Unicode
string literals by allowing users to define the Unicode value. The following
query is displaying its use:
Example
1SELECT UNISTR('pqr\00e4\00f3\00f9') FROM DUAL;