from: oracle sql practice exercises with solutions - YouTube
# Select alternate records
1 | select * |
# Find unique route cost
1 | create table edi_route_details( |
LEAST(SRC,DEST) | GREATEST(SRC,DEST) | COST |
---|---|---|
HYB | MUMB | 2500 |
BNG | CHN | 500 |
PUNE | UP | 4500 |
# Find similar name
1 | create table edi_emp( |
NAME | C |
---|---|
ravi |
4 |
(RAVI) |
4 |
(**RAVI**) |
4 |
Ravi. |
4 |
SMITH*** |
2 |
SMITH |
2 |
# Get diagonal data
1 | with t as |
A | B | C |
---|---|---|
1 | 10 | 100 |
2 | 20 | 200 |
3 | 30 | 300 |
4 | 40 | 400 |
5 | 50 | 500 |
6 | 60 | 600 |
7 | 70 | 700 |
8 | 80 | 800 |
9 | 90 | 900 |
1 | with t as |
A | B | C | ROWNUM | MOD(ROWNUM,3) | DIAGONAL |
---|---|---|---|---|---|
1 | 10 | 100 | 1 | 1 | 1 |
2 | 20 | 200 | 2 | 2 | 20 |
3 | 30 | 300 | 3 | 0 | 300 |
4 | 40 | 400 | 4 | 1 | 4 |
5 | 50 | 500 | 5 | 2 | 50 |
6 | 60 | 600 | 6 | 0 | 600 |
7 | 70 | 700 | 7 | 1 | 7 |
8 | 80 | 800 | 8 | 2 | 80 |
9 | 90 | 900 | 9 | 0 | 900 |
# Multi rows into single row
1 | with t as |
C1 | C2 | C3 | C4 | C5 | C6 |
---|---|---|---|---|---|
A1 | A2 | A3 | A4 | A5 | A6 |
B1 | B2 | B3 | B4 | B5 | B6 |
C1 | C2 | C3 | C4 | C5 | C6 |
D1 | D2 | D3 | D4 | D5 | D6 |
E1 | E2 | E3 | E4 | E5 | E6 |
F1 | F2 | F3 | F4 | F5 | F6 |
G1 | G2 | G3 | G4 | G5 | G6 |
H1 | H2 | H3 | H4 | H5 | H6 |
I1 | I2 | I3 | I4 | I5 | I6 |
1 | with t as ( |
C1 | C2 | C3 | C4 | C5 | C6 |
---|---|---|---|---|---|
A1 | A2 | B3 | B4 | C5 | C6 |
D1 | D2 | E3 | E4 | F5 | F6 |
G1 | G2 | H3 | H4 | I5 | I6 |
# Print each value twice
1 | with t as ( |
1 | with t as ( |
1 | with t as ( |
C |
---|
1 |
1 |
2 |
2 |
# Find max value of columns and rows
1 | create table edi_t ( |
C1 | C2 | C3 |
---|---|---|
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
1 | select greatest(max(c1), max(c2), max(c3)) from edi_t; |
GREATEST |
---|
9 |
1 | with d as ( |
COLUMN_NAME | VAL |
---|---|
C3 | 9 |
1 | with d as ( |
# Print null for recurring value
1 | create table edi_t ( |
ITEM_NO | ADDRESS | ITEM |
---|---|---|
5 | Bala | Pen |
Pencil | ||
Notepad | ||
6 | Ram | Laptop |
Notebook | ||
PC |
1 | with d as ( |
R |
---|
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
# Generate alphanumeric sequence
1 | with d as |
A||N |
---|
A1 |
B1 |
C1 |
D1 |
A2 |
B2 |
C2 |
D2 |
A3 |
B3 |
C3 |
D3 |
A4 |
B4 |
C4 |
D4 |
1 | with d as |
C1 | C2 | C3 | AN |
---|---|---|---|
ABCD | 1 | 1 | A1 |
ABCD | 1 | 2 | B1 |
ABCD | 1 | 3 | C1 |
ABCD | 1 | 4 | D1 |
ABCD | 2 | 1 | A2 |
ABCD | 2 | 2 | B2 |
ABCD | 2 | 3 | C2 |
ABCD | 2 | 4 | D2 |
ABCD | 3 | 1 | A3 |
ABCD | 3 | 2 | B3 |
ABCD | 3 | 3 | C3 |
ABCD | 3 | 4 | D3 |
ABCD | 4 | 1 | A4 |
ABCD | 4 | 2 | B4 |
ABCD | 4 | 3 | C4 |
ABCD | 4 | 4 | D4 |
# Cumulative sum
1 | create table edi_t ( |
ID | TYPE | AMOUNT | CUMULATIVE_SUM |
---|---|---|---|
1 | C | 10000 | 10000 |
2 | D | 2000 | 8000 |
3 | C | 10000 | 18000 |
4 | D | 5000 | 13000 |
5 | D | 4000 | 9000 |
# Character occurence two times only
1 | with t as |
S | C |
---|---|
HELLO | L |
MUSHROOM | O |
MUSHROOM | M |
# Find the number of month and days in given year
1 | with t as |
TO_CHAR(LEVEL,‘00’) | MONTH | DAYS |
---|---|---|
01 | JANUARY | 31 |
02 | FEBRUARY | 29 |
03 | MARCH | 31 |
04 | APRIL | 30 |
05 | MAY | 31 |
06 | JUNE | 30 |
07 | JULY | 31 |
08 | AUGUST | 31 |
09 | SEPTEMBER | 30 |
10 | OCTOBER | 31 |
11 | NOVEMBER | 30 |
12 | DECEMBER | 31 |
# Insert space between chars
1 | select 'HELLO', |
S | SPACE_SEPERATED_CHARS |
---|---|
HELLO | H E L L O |
# Re-arrange digits
1 | with t as |
D | REARRANGED_DIGITS |
---|---|
12453 | 12345 |
5684 | 4568 |
# Mask user data with random value
1 | with t as |
UPPER_CHAR | RANDOM_VALUE | RANDOM_VALUE_STR | MASKED_RESULT |
---|---|---|---|
P | **0.**143166111 | **.**143166111 | KAN8258 |
# Find symmetric set in given x, y coordinates
(x1, y1) and (x2, y2) are symmetric if x1=y2 and y1=x2.
1 | create table edi_t( |
SET_NAME | X | Y |
---|---|---|
P1 | 1 | 2 |
P2 | 2 | 1 |
P3 | 6 | 6 |
P4 | 6 | 6 |
P7 | 8 | 10 |
P8 | 10 | 8 |
# Remove duplicate values within row of data
1 | create table edi_t( |
ID | RESULT |
---|---|
1 | A,B,C |
2 | A,B |
3 | A |
# Find consecutive number
1 | create table edi_t ( |
N |
---|
3 |
4 |
5 |
13 |
14 |
# Find data from One table that are not exists in Another table
1 | create table edi_t1( |
C |
---|
B |
D |
# Generate Symbol
1 | select rpad('*', rownum, '*') output1, |
OUTPUT1 | OUTPUT2 | OUTPUT3 | OUTPUT4 | OUTPUT5 | OUTPUT6 |
---|---|---|---|---|---|
* |
********** |
********** |
* |
* |
* |
** |
********* |
********* |
** |
** |
** |
*** |
******** |
******** |
*** |
*** |
*** |
**** |
******* |
******* |
**** |
**** |
**** |
***** |
****** |
****** |
***** |
***** |
***** |
****** |
***** |
***** |
****** |
****** |
****** |
******* |
**** |
**** |
******* |
******* |
******* |
******** |
*** |
*** |
******** |
******** |
******** |
********* |
** |
** |
********* |
********* |
********* |
********** |
* |
* |
********** |
********** |
********** |
# Extract names from mail
1 | with t as ( |
NAME | FIRST_NAME | MIDDLE_NAME | LAST_NAME | DOMAIN_NAME |
---|---|---|---|---|
Babb.K.Chen@gmail.com | Babb | K | Chen | gmail.com |
1 | with t as ( |
NAME | FIRST_NAME |
---|---|
Babb.K.Chen@gmail.com | Babb |
Babb.K.Chen@gmail.com | K |
Babb.K.Chen@gmail.com | Chen@gmail |
Babb.K.Chen@gmail.com | com |
# Generate date range
1 | with t as ( |
DATE_RANGE |
---|
01-JAN-2019 |
02-JAN-2019 |
03-JAN-2019 |
04-JAN-2019 |
05-JAN-2019 |
06-JAN-2019 |
07-JAN-2019 |
08-JAN-2019 |
09-JAN-2019 |
10-JAN-2019 |
11-JAN-2019 |
12-JAN-2019 |
13-JAN-2019 |
14-JAN-2019 |
15-JAN-2019 |
16-JAN-2019 |
17-JAN-2019 |
18-JAN-2019 |
19-JAN-2019 |
20-JAN-2019 |
21-JAN-2019 |
22-JAN-2019 |
23-JAN-2019 |
24-JAN-2019 |
25-JAN-2019 |
26-JAN-2019 |
27-JAN-2019 |
28-JAN-2019 |
29-JAN-2019 |
30-JAN-2019 |
31-JAN-2019 |
# Find Saturday and Sunday
1 | with t as ( |
T_DATE | T_DAY | T_DY | T_D |
---|---|---|---|
2024-01-06 | SATURDAY | SAT | 7 |
2024-01-07 | SUNDAY | SUN | 1 |
2024-01-13 | SATURDAY | SAT | 7 |
2024-01-14 | SUNDAY | SUN | 1 |
2024-01-20 | SATURDAY | SAT | 7 |
2024-01-21 | SUNDAY | SUN | 1 |
2024-01-27 | SATURDAY | SAT | 7 |
2024-01-28 | SUNDAY | SUN | 1 |
# Find Match Played, Won, Lost count
1 | create table edi_t( |
MATCH_NO | TEAM_A | TEAM_B | WINNER |
---|---|---|---|
1 | ABC | SLP | ABC |
2 | ABC | BUS | ABC |
3 | ABC | XYZ | XYZ |
4 | AHI | XYZ | XYZ |
5 | SNI | XYZ | XYZ |
6 | WDU | ZZC | ZZC |
1 | with t as ( |
TEAM_A | TEAM_B | T_A | T_B |
---|---|---|---|
ABC | SLP | 1 | 0 |
ABC | BUS | 1 | 0 |
ABC | XYZ | 0 | 1 |
AHI | XYZ | 0 | 1 |
SNI | XYZ | 0 | 1 |
WDU | ZZC | 0 | 1 |
1 | with t as ( |
TEAM | PLAYED | WON | LOST |
---|---|---|---|
XYZ | 3 | 3 | 0 |
ABC | 3 | 2 | 1 |
ZZC | 1 | 1 | 0 |
WDU | 1 | 0 | 1 |
BUS | 1 | 0 | 1 |
AHI | 1 | 0 | 1 |
SLP | 1 | 0 | 1 |
SNI | 1 | 0 | 1 |
# Find dice rolled combinations and the third rolled dice is equal to sum of first and second value
1 | with t as ( |
R | R | T1.R+T2.R |
---|---|---|
1 | 1 | 2 |
1 | 2 | 3 |
2 | 1 | 3 |
2 | 2 | 4 |
1 | 3 | 4 |
3 | 1 | 4 |
1 | 4 | 5 |
2 | 3 | 5 |
4 | 1 | 5 |
3 | 2 | 5 |
4 | 2 | 6 |
3 | 3 | 6 |
1 | 5 | 6 |
2 | 4 | 6 |
5 | 1 | 6 |
# Sort the numbers in the commas seperated list
1 | with t as ( |
S | SORTED_RESULT |
---|---|
3,10,5,1,7,100,12 | 1,3,5,7,10,12,100 |
1 | with t as ( |
S | SORTED_RESULT |
---|---|
3 10 5 1 7 100 12 | 1 3 5 7 10 12 100 |
# Reverse string
1 | select reverse('WELCOME') from dual; |
REVERSED_RESULT |
---|
EMOCLEW |
# Consolidate first row and last row, second row and second from last row … as single row
1 | with t as ( |
NO | NAME |
---|---|
1 | A |
2 | B |
3 | C |
4 | D |
5 | E |
6 | F |
7 | G |
8 | H |
9 | I |
10 | J |
11 | K |
1 | with t as ( |
NO | NAME | COUNT(1)OVER()/2 | ROW_NUMBER()OVER(ORDERBYNO) | C |
---|---|---|---|---|
1 | A | 5.5 | 1 | 5 |
2 | B | 5.5 | 2 | 4 |
3 | C | 5.5 | 3 | 3 |
4 | D | 5.5 | 4 | 2 |
5 | E | 5.5 | 5 | 1 |
6 | F | 5.5 | 6 | 0 |
7 | G | 5.5 | 7 | -1 |
8 | H | 5.5 | 8 | -2 |
9 | I | 5.5 | 9 | -3 |
10 | J | 5.5 | 10 | -4 |
11 | K | 5.5 | 11 | -5 |
1 | with t as ( |
NO | NAME | C | NO | NAME | C |
---|---|---|---|---|---|
1 | A | 5 | 11 | K | 5 |
2 | B | 4 | 10 | J | 4 |
3 | C | 3 | 9 | I | 3 |
4 | D | 2 | 8 | H | 2 |
5 | E | 1 | 7 | G | 1 |
6 | F | 0 |
# Fill the missing dates with last not null value
1 | with t as ( |
DT | AMOUNT |
---|---|
2019-07-20 | 100 |
2019-07-21 | |
2019-07-22 | |
2019-07-23 | |
2019-07-24 | |
2019-07-25 | 200 |
2019-07-26 | |
2019-07-27 | |
2019-07-28 | |
2019-07-29 | |
2019-07-30 | 300 |
1 | with t as ( |
DT | AMOUNT | AMOUNT2 |
---|---|---|
2019-07-20 | 100 | 300 |
2019-07-21 | 100 | 300 |
2019-07-22 | 100 | 300 |
2019-07-23 | 100 | 300 |
2019-07-24 | 100 | 300 |
2019-07-25 | 200 | 300 |
2019-07-26 | 200 | 300 |
2019-07-27 | 200 | 300 |
2019-07-28 | 200 | 300 |
2019-07-29 | 200 | 300 |
2019-07-30 | 300 | 300 |
# Compute the start and end value of group in given seq
1 | create type edi_number_list is table of number; |
RANGE_START | RANGE_END | D-ROWNUM | COUNT(*) |
---|---|---|---|
1 | 3 | 0 | 3 |
5 | 5 | 1 | 1 |
7 | 9 | 2 | 3 |
11 | 12 | 3 | 2 |
15 | 16 | 5 | 2 |
20 | 20 | 8 | 1 |
# Expand the range to sequence numbers
1 | with t as |
START_RANGE | END_RANGE | R |
---|---|---|
1 | 3 | 1 |
1 | 3 | 2 |
1 | 3 | 3 |
5 | 5 | 5 |
7 | 9 | 7 |
7 | 9 | 8 |
7 | 9 | 9 |
11 | 12 | 11 |
11 | 12 | 12 |
15 | 16 | 15 |
15 | 16 | 16 |
20 | 20 | 20 |
# Grouping the range data
1 | create table edi_t( |
NAME | START_YEAR | END_YEAR |
---|---|---|
Swift | 2007 | 2009 |
Swift | 2009 | 2011 |
Swift | 2011 | 2013 |
BMW | 2009 | 2011 |
BMW | 2013 | 2015 |
1 | with r as |
NAME | YEAR |
---|---|
BMW | 2009 |
BMW | 2010 |
BMW | 2011 |
BMW | 2013 |
BMW | 2014 |
BMW | 2015 |
Swift | 2007 |
Swift | 2008 |
Swift | 2009 |
Swift | 2010 |
Swift | 2011 |
Swift | 2012 |
Swift | 2013 |
1 | with r as |
NAME | MIN(YEAR) | MAX(YEAR) |
---|---|---|
BMW | 2009 | 2011 |
BMW | 2013 | 2015 |
Swift | 2007 | 2013 |
# Convert list of numbers to table format
1 | with t as ( |
D | X | Y |
---|---|---|
1 | 1 | 1 |
2 | 1 | 2 |
3 | 1 | 3 |
4 | 1 | 4 |
5 | 1 | 5 |
6 | 2 | 1 |
7 | 2 | 2 |
8 | 2 | 3 |
9 | 2 | 4 |
10 | 2 | 5 |
11 | 3 | 1 |
12 | 3 | 2 |
13 | 3 | 3 |
14 | 3 | 4 |
15 | 3 | 5 |
16 | 4 | 1 |
17 | 4 | 2 |
18 | 4 | 3 |
19 | 4 | 4 |
20 | 4 | 5 |
21 | 5 | 1 |
22 | 5 | 2 |
23 | 5 | 3 |
24 | 5 | 4 |
25 | 5 | 5 |
1 | with t as ( |
X | C1 | C2 | C3 | C4 | C5 |
---|---|---|---|---|---|
1 | 1 | 2 | 3 | 4 | 5 |
2 | 6 | 7 | 8 | 9 | 10 |
3 | 11 | 12 | 13 | 14 | 15 |
4 | 16 | 17 | 18 | 19 | 20 |
5 | 21 | 22 | 23 | 24 | 25 |
1 | with t as ( |
Y | C1 | C2 | C3 | C4 | C5 |
---|---|---|---|---|---|
1 | 1 | 6 | 11 | 16 | 21 |
2 | 2 | 7 | 12 | 17 | 22 |
3 | 3 | 8 | 13 | 18 | 23 |
4 | 4 | 9 | 14 | 19 | 24 |
5 | 5 | 10 | 15 | 20 | 25 |
# Fetch emp hierarchical data in Flat format
1 | select trim(',' from sys_connect_by_path(empno, ',')) empno_list, |
EMPNO_LIST | ENAME_LIST | ISLEAF |
---|---|---|
7839 | KING | 0 |
7839,7566 | KING,JONES | 0 |
7839,7566,7788 | KING,JONES,SCOTT | 0 |
7839,7566,7788,7876 | KING,JONES,SCOTT,ADAMS | 1 |
7839,7566,7902 | KING,JONES,FORD | 0 |
7839,7566,7902,7369 | KING,JONES,FORD,SMITH | 1 |
7839,7698 | KING,BLAKE | 0 |
7839,7698,7499 | KING,BLAKE,ALLEN | 1 |
7839,7698,7521 | KING,BLAKE,WARD | 1 |
7839,7698,7654 | KING,BLAKE,MARTIN | 1 |
7839,7698,7844 | KING,BLAKE,TURNER | 1 |
7839,7698,7900 | KING,BLAKE,JAMES | 1 |
7839,7782 | KING,CLARK | 0 |
7839,7782,7934 | KING,CLARK,MILLER | 1 |
1 | select regexp_substr(empno_list, '\w+', 1, 1) lv1_empno, regexp_substr(ename_list, '\w+', 1, 1) lv1_ename, 1 lv1, |
LV1_EMPNO | LV1_ENAME | LV1 | LV2_EMPNO | LV2_ENAME | LV2 | LV3_EMPNO | LV3_ENAME | LV3 | LV4_EMPNO | LV4_ENAME | LV4 | LV5_EMPNO | LV5_ENAME |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
7839 | KING | 1 | 7566 | JONES | 2 | 7788 | SCOTT | 3 | 7876 | ADAMS | 4 | ||
7839 | KING | 1 | 7566 | JONES | 2 | 7902 | FORD | 3 | 7369 | SMITH | 4 | ||
7839 | KING | 1 | 7698 | BLAKE | 2 | 7499 | ALLEN | 3 | |||||
7839 | KING | 1 | 7698 | BLAKE | 2 | 7521 | WARD | 3 | |||||
7839 | KING | 1 | 7698 | BLAKE | 2 | 7654 | MARTIN | 3 | |||||
7839 | KING | 1 | 7698 | BLAKE | 2 | 7844 | TURNER | 3 | |||||
7839 | KING | 1 | 7698 | BLAKE | 2 | 7900 | JAMES | 3 | |||||
7839 | KING | 1 | 7782 | CLARK | 2 | 7934 | MILLER | 3 |
# Group the emplyees into multiple team
1 | create table edi_t( |
1 | select ename, |
ENAME | CITY | R |
---|---|---|
Babb1 | A | 1 |
Babb2 | A | 1 |
Babb3 | A | 2 |
Babb4 | A | 2 |
Babb5 | A | 3 |
Babb11 | B | 1 |
Babb22 | B | 1 |
Babb111 | C | 1 |
1 | --max team members = 2 |
CITY | TEAM_MEMBERS | TEAM_NAME |
---|---|---|
A | Babb1,Babb2 | Team1 |
A | Babb3,Babb4 | Team2 |
A | Babb5 | Team3 |
B | Babb11,Babb22 | Team1 |
C | Babb111 | Team1 |
1 | --max team members = 3 |
CITY | TEAM_MEMBERS | TEAM_NAME |
---|---|---|
A | Babb1,Babb2,Babb3 | Team1 |
A | Babb4,Babb5 | Team2 |
B | Babb11,Babb22 | Team1 |
C | Babb111 | Team1 |
# Fill the missing months with previous month salary
1 | create table edi_t( |
1 | select ename, |
ENAME | SALARY | D | NEXT_D | N |
---|---|---|---|---|
Peter | 5000 | 2019-05-01 | 2019-08-01 | 3 |
Peter | 10000 | 2019-08-01 | 2020-03-01 | 7 |
Peter | 15000 | 2020-03-01 | 1 | |
Solomon | 20000 | 2019-10-01 | 2020-02-01 | 4 |
Solomon | 30000 | 2020-02-01 | 1 |
1 | with t as ( |
ENAME | MONTH_YEAR | SALARY |
---|---|---|
Peter | May-19 | 5000 |
Peter | Jun-19 | 5000 |
Peter | Jul-19 | 5000 |
Peter | Aug-19 | 10000 |
Peter | Sep-19 | 10000 |
Peter | Oct-19 | 10000 |
Peter | Nov-19 | 10000 |
Peter | Dec-19 | 10000 |
Peter | Jan-20 | 10000 |
Peter | Feb-20 | 10000 |
Peter | Mar-20 | 15000 |
Solomon | Oct-19 | 20000 |
Solomon | Nov-19 | 20000 |
Solomon | Dec-19 | 20000 |
Solomon | Jan-20 | 20000 |
Solomon | Feb-20 | 30000 |
# Count number of occurences of vowels in a string
1 | with t as |
NAME | A | E | I | O | U |
---|---|---|---|---|---|
SUBHASIS ORACLE | 2 | 1 | 1 | 1 | 1 |
BABB CHEN | 1 | 1 | 0 | 0 | 0 |
# Get all dates for the given input month and year
1 | with t as |
FIRST_DAY+LEVEL-1 |
---|
2023-12-01 |
2023-12-02 |
2023-12-03 |
2023-12-04 |
2023-12-05 |
2023-12-06 |
2023-12-07 |
2023-12-08 |
2023-12-09 |
2023-12-10 |
2023-12-11 |
2023-12-12 |
2023-12-13 |
2023-12-14 |
2023-12-15 |
2023-12-16 |
2023-12-17 |
2023-12-18 |
2023-12-19 |
2023-12-20 |
2023-12-21 |
2023-12-22 |
2023-12-23 |
2023-12-24 |
2023-12-25 |
2023-12-26 |
2023-12-27 |
2023-12-28 |
2023-12-29 |
2023-12-30 |
2023-12-31 |
# Comma separated string into row of data
1 | with t as ( |
DEPTNO | ENAME | SUB |
---|---|---|
10 | A,B,C,D | A |
10 | A,B,C,D | B |
10 | A,B,C,D | C |
10 | A,B,C,D | D |
20 | AA,BB,CC | AA |
20 | AA,BB,CC | BB |
20 | AA,BB,CC | CC |
30 | AAA,BBB | AAA |
30 | AAA,BBB | BBB |
# Find the set of rows in hierarchical relational data
1 | create table edi_t ( |
ID | OLD_ID |
---|---|
1 | |
2 | 1 |
3 | 2 |
4 | 3 |
5 | 4 |
6 | |
7 | |
8 | 7 |
9 | |
10 | 9 |
11 | 10 |
1 | select id, old_id, connect_by_isleaf l |
ID | OLD_ID | L |
---|---|---|
3 | 2 | 0 |
2 | 1 | 0 |
1 | 1 |
1 | with t as |
ID | OLD_ID |
---|---|
1 | |
2 | 1 |
3 | 2 |
4 | 3 |
5 | 4 |
# Convert data representation in different format
Source
ACTIVATION_DT | DEACTIVATION_DT | RATE |
---|---|---|
2018-01-01 | 2018-01-31 | 10 |
2018-02-01 | 2018-02-27 | 15 |
2018-03-01 | 2018-04-30 | 12 |
1 | create table edi_t( |
Result
DT | RATE | CHANGE_LOG |
---|---|---|
2018-01-01 | 10 | Added |
2018-02-01 | 15 | Modified |
2018-02-27 | 15 | Removed |
2018-03-01 | 12 | Added |
2018-04-30 | 12 | Removed |
1 | with t as ( |
1 | with t as ( |
# Determine whether two strings are anagram
ASCII
Use this method, MAN and TAG also are anagram. This is not right.
1 | with t as ( |
ORA_HASH
1 | with t as ( |
C1 | C2 | SUM(A1) | SUM(A2) | OUTPUT |
---|---|---|---|---|
MAN | TAG | 4199279673 | 5736920293 | No Anagram |
LISTAGG
1 | with t as ( |
C1 | C2 | W1 | W2 | OUTPUT |
---|---|---|---|---|
MAN | TAG | AMN | AGT | No Anagram |
HEART | EARTH | AEHRT | AEHRT | Valid Anagram |
HELLO | HELLOO | EHLLO | EHLLOO | No Anagram |
# Divide the rows into groups
1 | with t as |
GROUP_NO | NO | NAME | MIN_NO | MAX_NO |
---|---|---|---|---|
1 | 1 | A | ||
1 | 2 | B | ||
1 | 3 | C | ||
1 | 4 | D | ||
1 | 1 | 4 | ||
2 | 5 | E | ||
2 | 6 | F | ||
2 | 7 | G | ||
2 | 5 | 7 | ||
3 | 8 | H | ||
3 | 9 | I | ||
3 | 10 | J | ||
3 | 8 | 10 |
# Compute group number for repeating numbers
1 | create table edi_t ( |
C1 | C2 | R |
---|---|---|
1 | 100 | 1 |
2 | 100 | 0 |
3 | 100 | 0 |
4 | 200 | 1 |
5 | 200 | 0 |
6 | 300 | 1 |
7 | 300 | 0 |
8 | 100 | 1 |
9 | 300 | 1 |
10 | 400 | 1 |
11 | 400 | 0 |
12 | 100 | 1 |
1 | select c1, c2, sum(r) over (order by c1) as group_number |
C1 | C2 | GROUP_NUMBER |
---|---|---|
1 | 100 | 1 |
2 | 100 | 1 |
3 | 100 | 1 |
4 | 200 | 2 |
5 | 200 | 2 |
6 | 300 | 3 |
7 | 300 | 3 |
8 | 100 | 4 |
9 | 300 | 5 |
10 | 400 | 6 |
11 | 400 | 6 |
12 | 100 | 7 |
# Compute count of group of repeating values
1 | with t as ( |
NO | C |
---|---|
1 | A |
2 | A |
3 | A |
4 | A |
5 | B |
6 | C |
7 | C |
8 | C |
9 | A |
10 | A |
11 | A |
12 | C |
13 | A |
1 | with t as |
C | R | ROW_NUMBER()OVER(ORDERBYNO) | ROW_NUMBER()OVER(PARTITIONBYCO |
---|---|---|---|
A | 0 | 1 | 1 |
A | 0 | 2 | 2 |
A | 0 | 3 | 3 |
A | 0 | 4 | 4 |
B | 4 | 5 | 1 |
C | 5 | 6 | 1 |
C | 5 | 7 | 2 |
C | 5 | 8 | 3 |
A | 4 | 9 | 5 |
A | 4 | 10 | 6 |
A | 4 | 11 | 7 |
1 | with t as ( |
C | COUNT(*) |
---|---|
A | 3 |
C | 2 |
B | 1 |
# Concate previous row value continuously
1 | with t as ( |
COL1 | COL2 | AGG | R |
---|---|---|---|
A | 1 | 1,2,3 | 1 |
A | 2 | 1,2,3 | 2 |
A | 3 | 1,2,3 | 3 |
B | 100 | 100,200,300,400,500 | 1 |
B | 200 | 100,200,300,400,500 | 2 |
B | 300 | 100,200,300,400,500 | 3 |
B | 400 | 100,200,300,400,500 | 4 |
B | 500 | 100,200,300,400,500 | 5 |
1 | with t as ( |
COL1 | COL2 | OUTPUT |
---|---|---|
A | 1 | 1 |
A | 2 | 1,2 |
A | 3 | 1,2,3 |
B | 100 | 100 |
B | 200 | 100,200 |
B | 300 | 100,200,300 |
B | 400 | 100,200,300,400 |
B | 500 | 100,200,300,400,500 |
# Generate integer series 1…n
# Integer Table Method
1 | create table integers |
INTEGER_VALUE |
---|
-5 |
-4 |
-3 |
-2 |
-1 |
0 |
1 |
2 |
3 |
4 |
5 |
# MODEL Method
1 | select integer_value |
1 | select integer_value |
1 | select integer_value |
INTEGER_VALUE |
---|
-5 |
-3 |
-1 |
1 |
3 |
5 |
1 | select integer_value |
INTEGER_VALUE |
---|
3 |
2 |
1 |
# ROWNUM + a Big Table Method
1 | select rownum |
# CONNECT BY LEVEL Method
1 | select level |
# CUBE Method
1 | select rownum |
# Type Constructor Expression Method
1 | create type integer_table_type as table of integer; |
1 | select column_value |
COLUMN_VALUE |
---|
1 |
2 |
3 |
4 |
5 |
1 | select column_value |
COLUMN_VALUE |
---|
1 |
1 |
4 |
4 |
4 |
8 |
10 |
# Type Constructor + Cartesian Product Method
1 | create type integer_table_type as table of integer; |
# Pipelined Function Method
1 | create type integer_table_type as table of integer; |
# Table Function Method
1 | create or replace function integer_series(p_lower_bound in number, |
# XML Method
1 | select rownum from ( |
# WITH CTE Method
1 | with t(n) as ( |
# Split string to rows
1 | create table edi_t |
ID | DATA | NOTE |
---|---|---|
1 | v1 | One value |
2 | v1, | Two values, one empty |
3 | v1, | Three values, two empty |
4 | ,v2, | Three values, two empty |
5 | ,v3 | Three values, two empty |
6 | v1,v2 | Two values |
7 | v1,v2, | Three values, one empty |
8 | v1,v2,v3 | Three values, none empty |
9 | One empty value | |
10 | , | Two values, two empty |
11 | , | Three values, all empty |
# Collection Method
1 | select id, |
ID | DATA | VAL | POSITION | NOTE |
---|---|---|---|---|
1 | v1 | v1 | 1 | One value |
2 | v1, | v1 | 1 | Two values, one empty |
2 | v1, | 2 | Two values, one empty | |
3 | v1, | v1 | 1 | Three values, two empty |
3 | v1, | 2 | Three values, two empty | |
3 | v1, | 3 | Three values, two empty | |
4 | ,v2, | v2 | 1 | Three values, two empty |
4 | ,v2, | 2 | Three values, two empty | |
4 | ,v2, | 3 | Three values, two empty | |
5 | ,v3 | v3 | 1 | Three values, two empty |
5 | ,v3 | 2 | Three values, two empty | |
5 | ,v3 | 3 | Three values, two empty | |
6 | v1,v2 | v1 | 1 | Two values |
6 | v1,v2 | v2 | 2 | Two values |
7 | v1,v2, | v1 | 1 | Three values, one empty |
7 | v1,v2, | v2 | 2 | Three values, one empty |
7 | v1,v2, | 3 | Three values, one empty | |
8 | v1,v2,v3 | v1 | 1 | Three values, none empty |
8 | v1,v2,v3 | v2 | 2 | Three values, none empty |
8 | v1,v2,v3 | v3 | 3 | Three values, none empty |
9 | 1 | One empty value | ||
10 | , | 1 | Two values, two empty | |
10 | , | 2 | Two values, two empty | |
11 | , | 1 | Three values, all empty | |
11 | , | 2 | Three values, all empty | |
11 | , | 3 | Three values, all empty |
# Model Method
1 | select id, data, val, position, note |
ID | DATA | VAL | POSITION | NOTE |
---|---|---|---|---|
1 | v1 | v1 | 1 | One value |
2 | v1, | v1 | 1 | Two values, one empty |
2 | v1, | 2 | Two values, one empty | |
3 | v1, | v1 | 1 | Three values, two empty |
3 | v1, | 2 | Three values, two empty | |
3 | v1, | 3 | Three values, two empty | |
4 | ,v2, | v2 | 1 | Three values, two empty |
4 | ,v2, | 2 | Three values, two empty | |
4 | ,v2, | 3 | Three values, two empty | |
5 | ,v3 | v3 | 1 | Three values, two empty |
5 | ,v3 | 2 | Three values, two empty | |
5 | ,v3 | 3 | Three values, two empty | |
6 | v1,v2 | v1 | 1 | Two values |
6 | v1,v2 | v2 | 2 | Two values |
7 | v1,v2, | v1 | 1 | Three values, one empty |
7 | v1,v2, | v2 | 2 | Three values, one empty |
7 | v1,v2, | 3 | Three values, one empty | |
8 | v1,v2,v3 | v1 | 1 | Three values, none empty |
8 | v1,v2,v3 | v2 | 2 | Three values, none empty |
8 | v1,v2,v3 | v3 | 3 | Three values, none empty |
9 | 1 | One empty value | ||
10 | , | 1 | Two values, two empty | |
10 | , | 2 | Two values, two empty | |
11 | , | 1 | Three values, all empty | |
11 | , | 2 | Three values, all empty | |
11 | , | 3 | Three values, all empty |
# Pipelined Function Method
1 | create type table_str is table of varchar2(4000); |
ID | DATA | VAL | POSITION | NOTE |
---|---|---|---|---|
1 | v1 | v1 | 1 | One value |
2 | v1, | v1 | 1 | Two values, one empty |
2 | v1, | 2 | Two values, one empty | |
3 | v1, | v1 | 1 | Three values, two empty |
3 | v1, | 2 | Three values, two empty | |
3 | v1, | 3 | Three values, two empty | |
4 | ,v2, | 1 | Three values, two empty | |
4 | ,v2, | v2 | 2 | Three values, two empty |
4 | ,v2, | 3 | Three values, two empty | |
5 | ,v3 | 1 | Three values, two empty | |
5 | ,v3 | 2 | Three values, two empty | |
5 | ,v3 | v3 | 3 | Three values, two empty |
6 | v1,v2 | v1 | 1 | Two values |
6 | v1,v2 | v2 | 2 | Two values |
7 | v1,v2, | v1 | 1 | Three values, one empty |
7 | v1,v2, | v2 | 2 | Three values, one empty |
7 | v1,v2, | 3 | Three values, one empty | |
8 | v1,v2,v3 | v1 | 1 | Three values, none empty |
8 | v1,v2,v3 | v2 | 2 | Three values, none empty |
8 | v1,v2,v3 | v3 | 3 | Three values, none empty |
9 | 1 | One empty value | ||
10 | , | 1 | Two values, two empty | |
10 | , | 2 | Two values, two empty | |
11 | , | 1 | Three values, all empty | |
11 | , | 2 | Three values, all empty | |
11 | , | 3 | Three values, all empty |
# Java Function Method
1 | create type edi_varchar_list_type is table of varchar2(32767); |
ID | DATA | VAL | POSITION | NOTE |
---|---|---|---|---|
1 | v1 | v1 | 1 | One value |
2 | v1, | v1 | 1 | Two values, one empty |
3 | v1, | v1 | 1 | Three values, two empty |
4 | ,v2, | 1 | Three values, two empty | |
4 | ,v2, | v2 | 2 | Three values, two empty |
5 | ,v3 | 1 | Three values, two empty | |
5 | ,v3 | 2 | Three values, two empty | |
5 | ,v3 | v3 | 3 | Three values, two empty |
6 | v1,v2 | v1 | 1 | Two values |
6 | v1,v2 | v2 | 2 | Two values |
7 | v1,v2, | v1 | 1 | Three values, one empty |
7 | v1,v2, | v2 | 2 | Three values, one empty |
8 | v1,v2,v3 | v1 | 1 | Three values, none empty |
8 | v1,v2,v3 | v2 | 2 | Three values, none empty |
8 | v1,v2,v3 | v3 | 3 | Three values, none empty |
# XML Method
1 | with t as |
ID | DATA | VAL | POSITION | NOTE |
---|---|---|---|---|
1 | v1 | v1 | 1 | One value |
2 | v1, | v1 | 1 | Two values, one empty |
2 | v1, | 2 | Two values, one empty | |
3 | v1, | v1 | 1 | Three values, two empty |
3 | v1, | 2 | Three values, two empty | |
3 | v1, | 3 | Three values, two empty | |
4 | ,v2, | 1 | Three values, two empty | |
4 | ,v2, | v2 | 2 | Three values, two empty |
4 | ,v2, | 3 | Three values, two empty | |
5 | ,v3 | 1 | Three values, two empty | |
5 | ,v3 | 2 | Three values, two empty | |
5 | ,v3 | v3 | 3 | Three values, two empty |
6 | v1,v2 | v1 | 1 | Two values |
6 | v1,v2 | v2 | 2 | Two values |
7 | v1,v2, | v1 | 1 | Three values, one empty |
7 | v1,v2, | v2 | 2 | Three values, one empty |
7 | v1,v2, | 3 | Three values, one empty | |
8 | v1,v2,v3 | v1 | 1 | Three values, none empty |
8 | v1,v2,v3 | v2 | 2 | Three values, none empty |
8 | v1,v2,v3 | v3 | 3 | Three values, none empty |
9 | 1 | One empty value | ||
10 | , | 1 | Two values, two empty | |
10 | , | 2 | Two values, two empty | |
11 | , | 1 | Three values, all empty | |
11 | , | 2 | Three values, all empty | |
11 | , | 3 | Three values, all empty |
# Calculate the median
1 | create table movie_ratings ( |
RATING |
---|
11 |
12 |
15 |
19 |
12 |
13 |
16 |
20 |
# ROW_NUMBER() Window Function Method
1 | select avg(m.rating) |
MEDIAN |
---|
14 |
# Having Clause Method
1 | select avg(m.rating) |
# Median Function Method
1 | select median(rating) from movie_ratings; |