SQL Queries
SQL Queries
---oracle funct
select * from abc where contains(name,'%a%e' )>0 ----where name column conatins
this regular exp. It is helpfull if we create context index with name column
Hierachical Query
SELECT
CONCAT
(
LPAD
(
' ',
LEVEL*3-3
),
ENAME
) ENAME
FROM
EMP
CONNECT BY
PRIOR EMPNO = MGR
START WITH
MGR IS NULL
ORDER SIBLINGS BY
EMP.ENAME;
output
ENAME
----------------
KING
BLAKE
ALLEN
JAMES
MARTIN
TURNER
WARD
CLARK
MILLER
JONES
FORD
SMITH
SCOTT
ADAMS
---------------------------------
SELECT empno,
ename,
job,
mgr,
hiredate,
level
FROM emp
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
Gets me this result
7839 KING PRESIDENT 17-Nov-81 1
7566 JONES MANAGER 7839 2-Apr-81 2
7788 SCOTT ANALYST 7566 19-Apr-87 3
7876 ADAMS CLERK 7788 23-May-87 4
7902 FORD ANALYST 7566 3-Dec-81 3
7369 SMITH CLERK 7902 17-Dec-80 4
7698 BLAKE MANAGER 7839 1-May-81 2
7499 ALLEN SALESMAN 7698 20-Feb-81 3
7521 WARD SALESMAN 7698 22-Feb-81 3
7654 MARTIN SALESMAN 7698 28-Sep-81 3
7844 TURNER SALESMAN 7698 8-Sep-81 3
7900 JAMES CLERK 7698 3-Dec-81 3
7782 CLARK MANAGER 7839 9-Jun-81 2
7934 MILLER CLERK 7782 23-Jan-82 3
SELECT empno,
ename,
job,
mgr,
hiredate,
LEVEL
FROM emp
WHERE LEVEL <= 2
START WITH mgr IS NULL
CONNECT BY PRIOR empno = mgr
empno ename job mgr hiredate level
7839 KING PRESIDENT 17-Nov-81 1
7566 JONES MANAGER 7839 2-Apr-81 2
7698 BLAKE MANAGER 7839 1-May-81 2
7782 CLARK MANAGER 7839 9-Jun-81 2
KING
----------JONES
--------------------SCOTT
------------------------------ADAMS
--------------------FORD
------------------------------SMITH
----------BLAKE
--------------------ALLEN
--------------------WARD
--------------------MARTIN
--------------------TURNER
--------------------JAMES
----------CLARK
--------------------MILLER
CTE ORACLE
----------------
Cte is a named tempopary result set which can be used multiple times within the
scope of the single statement
with
cte1 as (select a,b from table1),
cte2 as (select c,d from table2)
Inline view
----------------
Derived TABLE
---------------------------
https://logicalread.com/when-to-apply-sql-server-derived-tables-mc03/#.Wa-A2MgjHIU
Json in oracle
====================================
{ "PONumber" : 1600,
"Reference" : "ABULL-20140421",
"Requestor" : "Alexis Bull",
"User" : "ABULL",
"CostCenter" : "A50",
"ShippingInstructions" : { "name" : "Alexis Bull",
"Address": { "street" : "200 Sporting Green",
"city" : "South San Francisco",
"state" : "CA",
"zipCode" : 99236,
"country" : "United States of America" },
"Phone" : [ { "type" : "Office", "number" : "909-555-
7307" },
{ "type" : "Mobile", "number" : "415-555-
1234" } ] },
"Special Instructions" : null,
"AllowPartialShipment" : false,
"LineItems" : [ { "ItemNumber" : 1,
"Part" : { "Description" : "One Magic
Christmas",
"UnitPrice" : 19.95,
"UPCCode" : 13131092899 },
"Quantity" : 9.0 },
{ "ItemNumber" : 2,
"Part" : { "Description" : "Lethal Weapon",
"UnitPrice" : 19.95,
"UPCCode" : 85391628927 },
"Quantity" : 5.0 } ] }
SELECT jt.*
FROM j_purchaseorder,
JSON_TABLE(po_document, '$.ShippingInstructions.Phone[*]'
COLUMNS (row_number FOR ORDINALITY,
phone_type VARCHAR2(10) PATH '$.type',
phone_num VARCHAR2(20) PATH '$.number'))
AS jt;
ROW_NUMBER PHONE_TYPE PHONE_NUM
---------- ---------- --------------------
1 Office 909-555-7307
2 Mobile 415-555-1234
Cohesion: all the procedures and functions relating to a specfic sub-system are in
one program unit. This is just good design practice but it's also easier to manage,
e.g. in source control.
Constants, sub-types and other useful things: there's more to PL/SQL than stored
procedures. Anything we can define in a package spec can be shared with other
programs, for instance user-defined exceptions.
Overloading: the ability to define a procedure or function with the same name but
different signatures.
Security: defining private procedures in the package body which can only be used by
the package because they aren't exposed in the specification.
Sharing common code: another benefit of private procedures.
We only need to grant EXECUTE on a package rather than on several procedures.
COUNT(*) returns the number of items in a group. This includes NULL values and
duplicates.
COUNT(1) returns the number of items in a group. This includes NULL values and
duplicates.
COUNT(ALL expression) evaluates expression for each row in a group and returns the
number of nonnull values.
COUNT(DISTINCT expression) evaluates expression for each row in a group and returns
the number of unique, nonnull values.
https://www.oracletutorial.com/oracle-basics/oracle-subquery/
Natural Join
======================================================
https://www.w3resource.com/oracle/joins/natural-join.php
https://stackoverflow.com/questions/8696383/difference-between-natural-join-and-
inner-join
Delete duplicate record
==================================================================
With CTE_Duplicates as
(select empid,name , row_number() over(partition by empid,name order by
empid,name ) rownumber
from EmpDup )
delete from CTE_Duplicates where rownumber!=1
Merge in oracle
=========================================================================
ID NAME SCORE
---------- --------------- ----------
1 Jack 540
2 Rose
3 William 650
4 Caledon 620
5 Fabrizio 600
6 Thomas
7 Ruth 680
8 Spacer 555
8 rows selected.
ID NAME SCORE
---------- --------------- ----------
7 Ruth 690
8 Spicer 620
9 Wallace 600
10 Lizzy
11 Brock 705
As you can see, the following actions are required on table STUDENT:
1 row for id#7 to be corrected for score: Ruth had scored 690, not 680.
1 row for id#8 to be corrected for name: the student is called Spicer, not Spacer.
3 new rows (ids#9,10,11) to be inserted into STUDENT table.
5 rows should get processed in all.
5 rows merged.
ID NAME SCORE
---------- --------------- ----------
1 Jack 540
2 Rose
3 William 650
4 Caledon 620
5 Fabrizio 600
6 Thomas
7 Ruth 690
11 Brock 705
10 Lizzy
9 Wallace 600
8 Spicer 620
11 rows selected.
Sure enough, 5 rows have got merged as expected � 2 updates + 3 inserts.
You cannot update any of the columns you are merging on. If you try updating a
student�s id in the example above, this error will show up in 10G:
select sum (case when acolumn >= 0 then acolumn else 0 end) as positive,
sum (case when acolumn < 0 then acolumn else 0 end) as negative
from table
;WITH Psum_CTE
AS
( SELECT SUM(num) AS PositiveSum
FROM sample
WHERE num>=0
)
,Nsum_CTE
AS
(
SELECT SUM(num) AS NegativeSum
FROM sample
WHERE num<0
)
SELECT PositiveSum,NegativeSum
FROM Psum_CTE,Nsum_CTE
Find all ID's for rows where the row's Address and State field matched another
row's Address and State field.
SELECT A.*
FROM YourTable A
INNER JOIN (SELECT Address, State
FROM YourTable
GROUP BY Address, State
HAVING COUNT(*) > 1) B
ON A.Address = B.Address AND A.State = B.State
====================================================
will tell you how many rows are in dept1 not in dept2 + in dept2 not in dept1
https://oracle-base.com/articles/misc/lag-lead-analytic-functions