What is the number of records if you apply
JOIN
LEFT JOIN
RIGHT JOIN
Solution
Create two tables:
On a.col1 = b.col1
select * from Test1 a Left JOIN Test2 b
On a.col1 = b.col1
On a.col1 = b.col1
_______________________________________________________________________
select * from Employee where To_char(Joining_date,’YYYY’)=’2017′;
___________________________________________________________________________select * from Employee where substr(convert(varchar,Joining_date,103),7,4)=’2017′;
Question:
_________________________________________________________________________
Question : How would you create a list or a table from scratch in a database where you dont have write permission.
Answer: Use the With Clause
With Table1 As(
select 'MLCF02353' As C1,'Thalam' As Names from dual union
select 'ICBNY0061', 'Abhishek' from dual union
select 'BNPN.02E9','Roza' from dual
)
select * from table1
_________________________________________________________________________
Question : Extract the first name, last name from a full name
Answer:
with table_A as
(
select 'Abhishek Rath' as Full_name from dual union all
select 'Piyush Gangwar ' as Full_name from dual union all
select 'kabita Ghosh' as Full_name from dual union all
select 'Nimisha Jain ' as Full_name from dual union all
Select 'Pranav Thale' as Full_name from dual
)
select Full_name,
SUBSTR(Full_name,0,INSTR(TRIM(Full_name),' ')- 1) AS first_name,
SUBSTR(full_name,INSTR(TRIM(full_name),' ',-1)+ 1) AS last_name from table_A
FULL_NAME
|
FIRST_NAME
|
MID_NAME
|
LAST_NAME
|
SUFFIX
|
---|---|---|---|---|
ABC DEF GHI JKL, MN | ABC | DEF GHI | JKL | MN |
OPQ RST, UV | OPQ | - | RST | UV |
WXY Z | WXY | - | Z | - |
Ronald V. McDonald, DO | Ronald | V | McDonald | DO |
Fred Derf, DD | Fred | - | Derf | DD |
Pig Pen | Pig | - | Pen | - |
_______________________________________________________________________________
Question: INSTR Function explained with examples (https://www.databasestar.com/oracle-instr/)
Question: Regular Expression explained with examples (https://www.databasestar.com/oracle-regexp-functions/)
_______________________________________________________________________________
Question: Print all the dates from past x number of months or x number of days till today.
Answer:
_______________________________________________________________________________
select
to_date(to_CHAR(ADD_MONTHS(TRUNC(SYSDATE), -3),'dd/mm/yyyy'),'dd-mm-yyyy') + rownum -1 AS DAY
from
all_objects -- All objects table is used as it has a lot of records.
where
rownum <= to_date(TO_CHAR(SYSDATE,'DD/MM/YYYY'),'DD/MM/YYYY')-to_date(to_CHAR(ADD_MONTHS(TRUNC(SYSDATE), -3),'dd/mm/yyyy'),'dd-mm-yyyy')+1
Question :
Table A |
Table B |
|
1 |
1 |
|
2 |
2 |
|
1 |
3 |
|
3 |
4 |
|
4 |
4 |
|
5 |
||
4 |
What is the Output of Left Join and Inner join
Name |
Subject |
Marks |
Grade |
Range |
|
Ravi |
Maths |
70 |
A |
80>= |
|
Sandeep |
English |
80 |
B |
70-79 |
|
Rahul |
Science |
65 |
C |
60-69 |
|
Sakshi |
SST |
76 |
D |
<60 |
|
Swati |
Computers |
84 |
|||
Ramesh |
Accounting |
55 |
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
So what's going on here?
The connect by level
clause generates a row for each value. It finds how many values there are by:
The regexp_substr
extracts each value using this regular expression:
[^,]+
This searches for:
The third argument tells regexp_substr
to start the search at the first character. And the final one instructs it to fetch the Nth occurrence of the pattern. So row one finds the first value, row two the second, and so on.
ORDER_DATE | PRODUCT_ID | QTY |
---|---|---|
2007/09/25 | 1000 | 20 |
2007/09/26 | 2000 | 15 |
2007/09/27 | 1000 | 8 |
2007/09/28 | 2000 | 12 |
2007/09/29 | 2000 | 2 |
2007/09/30 | 1000 | 4 |
PRODUCT_ID | ORDER_DATE | NEXT_ORDER_DATE |
---|---|---|
1000 | 2007/09/25 | 2007/09/26 |
2000 | 2007/09/26 | 2007/09/27 |
1000 | 2007/09/27 | 2007/09/28 |
2000 | 2007/09/28 | 2007/09/29 |
2000 | 2007/09/29 | 2007/09/30 |
1000 | 2007/09/30 | NULL |
SELECT product_id, order_date, LEAD (order_date,1) OVER (ORDER BY order_date) AS next_order_date FROM orders;
In this example, the LEAD function will sort in ascending order all of the order_date values in the orders table and then return the next order_date since we used an offset of 1.
If we had used an offset of 2 instead, it would have returned the order_date from 2 orders later. If we had used an offset of 3, it would have returned the order_date from 3 orders later....and so on.
Using Partitions
Now let's look at a more complex example where we use a query partition clause to return the next order_date for each product_id.
Enter the following SQL statement:
SELECT product_id, order_date, LEAD (order_date,1) OVER (PARTITION BY product_id ORDER BY order_date) AS next_order_date FROM orders;
It would return the following result:
PRODUCT_ID | ORDER_DATE | NEXT_ORDER_DATE |
---|---|---|
1000 | 2007/09/25 | 2007/09/27 |
1000 | 2007/09/27 | 2007/09/30 |
1000 | 2007/09/30 | NULL |
2000 | 2007/09/26 | 2007/09/28 |
2000 | 2007/09/28 | 2007/09/29 |
2000 | 2007/09/29 | NULL |
In this example, the LEAD function will partition the results by product_id and then sort by order_date as indicated by PARTITION BY product_id ORDER BY order_date
. This means that the LEAD function will only evaluate an order_date value if the product_id matches the current record's product_id. When a new product_id is encountered, the LEAD function will restart its calculations and use the appropriate product_id partition.
As you can see, the 3rd record in the result set has a value of NULL for the next_order_date because it is the last record for the partition where product_id is 1000 (sorted by order_date). This is also true for the 6th record where the product_id is 2000.