This action might not be possible to undo. Are you sure you want to continue?
As a database developer, writing SQL queries, PLSQL code is part of daily life. Having a good knowledge on SQL is really important. Here i am posting some practical examples on SQL queries. To solve these interview questions on SQL queries you have to create the products, sales tables in your oracle database. The "Create Table", "Insert" statements are provided below.
CREATE TABLE PRODUCTS ( PRODUCT_ID PRODUCT_NAME ); CREATE TABLE SALES ( SALE_ID PRODUCT_ID YEAR Quantity PRICE ); INSERT INSERT INSERT INSERT INTO INTO INTO INTO PRODUCTS PRODUCTS PRODUCTS PRODUCTS SALES SALES SALES SALES SALES SALES SALES SALES SALES
INTEGER, INTEGER, INTEGER, INTEGER, INTEGER VALUES VALUES VALUES VALUES ( ( ( ( ( ( ( ( ( ( ( ( ( 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 200, 300, 400, 100, 100, 100, 200, 200, 200, 300, 300, 300, 'Nokia'); 'IPhone'); 'Samsung'); 'LG'); 2010, 2011, 2012, 2010, 2011, 2012, 2010, 2011, 2012, 25, 16, 8, 10, 15, 20, 20, 18, 20, 5000); 5000); 5000); 9000); 9000); 9000); 7000); 7000); 7000);
INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO COMMIT;
VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES VALUES
The products table contains the below data.
SELECT * FROM PRODUCTS; PRODUCT_ID PRODUCT_NAME ----------------------100 Nokia 200 IPhone 300 Samsung
The sales table contains the following data.
SELECT * FROM SALES;
SALE_ID PRODUCT_ID YEAR QUANTITY PRICE -------------------------------------1 100 2010 25 5000 2 100 2011 16 5000 3 100 2012 8 5000 4 200 2010 10 9000 5 200 2011 15 9000 6 200 2012 20 9000 7 300 2010 20 7000 8 300 2011 18 7000 9 300 2012 20 7000
Here Quantity is the number of products sold in each year. Price is the sale price of each product. I hope you have created the tables in your oracle database. Now try to solve the below SQL queries. 1. Write a SQL query to find the products which have continuous increase in sales every year? Solution: Here “Iphone” is the only product whose sales are increasing every year. STEP1: First we will get the previous year sales for each product. The SQL query to do this is
SELECT P.PRODUCT_NAME, S.YEAR, S.QUANTITY, LEAD(S.QUANTITY,1,0) OVER ( PARTITION BY P.PRODUCT_ID ORDER BY S.YEAR DESC ) QUAN_PREV_YEAR FROM PRODUCTS P, SALES S WHERE P.PRODUCT_ID = S.PRODUCT_ID; PRODUCT_NAME YEAR QUANTITY QUAN_PREV_YEAR ----------------------------------------Nokia 2012 8 16 Nokia 2011 16 25 Nokia 2010 25 0 IPhone 2012 20 15 IPhone 2011 15 10 IPhone 2010 10 0 Samsung 2012 20 18 Samsung 2011 18 20 Samsung 2010 20 0
PRODUCT_ID = S. This can be achieved in three ways.QUANTITY.1.PRODUCT_ID ORDER BY S. STEP2: We will find the difference between the quantities of a product with its previous year’s quantity.QUANTITY LEAD(S.PRODUCT_NAME. The final query to get the required result is SELECT PRODUCT_NAME FROM ( SELECT P.YEAR DESC ) QUAN_DIFF FROM PRODUCTS P. SELECT P.0) OVER ( PARTITION BY P.PRODUCT_NAME FROM PRODUCTS P LEFT OUTER JOIN SALES S ON (P.PRODUCT_NAME FROM PRODUCTS P WHERE P.Here the lead analytic function will get the quantity of a product in its previous year.PRODUCT_ID). PRODUCT_NAME -----------IPhone 2.PRODUCT_ID )A GROUP BY PRODUCT_NAME HAVING MIN(QUAN_DIFF) >= 0.QUANTITY IS NULL PRODUCT_NAME -----------LG Method2: Using the NOT IN operator. Method1: Using left outer join. then the product is a constantly increasing in sales. If this difference is greater than or equal to zero for all the rows. Write a SQL query to find the products which does not have sales at all? Solution: “LG” is the only product which does not have sales at all.PRODUCT_ID = S. S. WHERE S. SALES S WHERE P. SELECT P.PRODUCT_ID NOT IN .
QUANTITY. SALES S_2011 WHERE P. The SQL query to get the required output is SELECT P. .YEAR = 2012 AND S_2011.PRODUCT_NAME FROM PRODUCTS P.PRODUCT_ID AND S_2012. Samsung in 2011 and IPhone. PRODUCT_NAME -----------Nokia 4.YEAR = 2011 AND S_2012. Similarly.PRODUCT_ID = S_2012. SALES S_2012.YEAR.(SELECT DISTINCT PRODUCT_ID FROM SALES).PRODUCT_ID AND S_2012. YEAR FROM ( SELECT P.PRODUCT_ID). Write a SQL query to find the products whose sales decreased in 2012 compared to 2011? Solution: Here Nokia is the only product whose sales decreased in year 2012 when compared with the sales in the year 2011.PRODUCT_NAME FROM PRODUCTS P WHERE NOT EXISTS (SELECT 1 FROM SALES S WHERE S.PRODUCT_ID = P. Write a query to select the top product sold in each year? Solution: Nokia is the top product sold in the year 2010.QUANTITY < S_2011. The query for this is SELECT PRODUCT_NAME. S. SELECT P. PRODUCT_NAME -----------LG Method3: Using the NOT EXISTS operator.PRODUCT_NAME.PRODUCT_ID = S_2011. Samsung in 2012. PRODUCT_NAME -----------LG 3.
Oracle Part 2 SQL Queries Interview Questions . Write a query to find the total sales of each product.Oracle Part 3 SQL Queries Interview Questions .QUANTITY*S.PRODUCT_ID) GROUP BY P.? Solution: This is a simple query.PRODUCT_NAME. Email ThisBlogThis!Share to TwitterShare to Facebook Labels: Oracle.FROM WHERE ) A WHERE RNK = 1.PRICE ).PRODUCT_ID = S. PRODUCT_NAME TOTAL_SALES --------------------------LG 0 IPhone 405000 Samsung 406000 Nokia 245000 Recommended Posts: SQL Queries Interview Questions . SALES S P. SELECT P.QUANTITY DESC ) RNK PRODUCTS P.Oracle Part 5 If you like this post.PRODUCT_ID PRODUCT_NAME YEAR -------------------Nokia 2010 Samsung 2011 IPhone 2012 Samsung 2012 5. RANK() OVER ( PARTITION BY S. 0) TOTAL_SALES FROM PRODUCTS P LEFT OUTER JOIN SALES S ON (P. then please share it on Google by clicking on the +1 button.PRODUCT_NAME. You just need to group by the data on PRODUCT_NAME and then find the sum of sales. NVL( SUM( S.Oracle Part 1 SQL Queries Interview Questions .PRODUCT_ID = S.YEAR ORDER BY S.Oracle Part 4 SQL Queries Interview Questions . Oracle Interview Questions 5 comments: .
2012 20:37 @Neel Without using analytical functions. . vijay bhaskar02 January. Neel02 January. 2012 11:21 Please provide simple sql (dont use analytical function) to question no 4 (i.e Write a query to select the top product sold in each year) ReplyDelete 2.1.
we can do two ways: -.year.product_id.PRODUCT_NAME. s.so top 3 would include all rows :) hence query for top 2 result. 2012 05:46 Thanks Vijay for quick and super easy answer. quantity. ----Using analytical function---select s. S..YEAR = S.QUAN = S. Now to your question (about top 3 product sold each year)..product name can be found by joining products table to above result set --using simple sql -.... What if we want to get top 3 products sold in every year? ReplyDelete 3.QUANTITY AND S. Neel03 January. MAX(QUANTITY) QUAN FROM SALES GROUP BY YEAR )A.. year.(i am assuming only top 2 -. ad. rank() over(partition by year order by quantity desc) sno from sales )s where s..SELECT P.YEAR.as number of records r 3 total each year.(corelated query) select ad.year.quantity from (select product_id. ad. i was actually looking for such kind of solution.QUANTITY FROM ( SELECT YEAR.product_id. Thanks a lot.quantity from sales ad .PRODUCT_ID. s. -. PRODUCTS P WHERE A.sno <=2.PRODUCT_ID = P. S. SALES S.YEAR AND A.
where 2 >= ( select count(quantity) from sales b where ad. 2012 19:56 @Neel The correlated query working only when there is distinct quantities in a year. . This needs to be changed a bit. ReplyDelete 4.quantity <= b.quantity and ad.year. please suggest if we can achieve same using some other simple sql(no anlaytic) way.year=b. I will try and post the query.year ) order by ad. I checked it for picking only one product in a year and it failed in that case. vijay bhaskar03 January.
com. or write me to duglus88@gmail. 2012 23:12 Vijay. I executed corelated query again and again.ReplyDelete 5. Where i have used PRODUCTS and SALES tables as an example. and post some intersting question just as you posted.----. Neel03 January.Oracle Part 1 .---100 2010 25 300 2010 20 300 2011 18 100 2011 16 300 2012 20* 200 2012 20* so. Neel. So. I want to test my oracle sql skills. Thanks. Here also i am using the same tables. just take a look at the tables by going through that link and it will be easy for you to understand the questions mentioned here. The SQL query for this is . Lets try with some other way. Write a query to find the products whose quantity sold in a year should be greater than the average quantity of the product sold across all the years? Solution: This can be solved with the help of correlated query.Oracle Part 2 This is continuation to my previous post. Reply SQL Queries Interview Questions . this is really fine (bcoz. and found the result which include same quantity in same year (star marked. SQL Queries Interview Questions . Solve the below examples by writing SQL queries. 1. i hv mentioned equal sign also in insider query). manually): see the result set: id year quantity --.
S_S.PRODUCT_ID = S. SALES S WHERE P. S_I.PRODUCT_ID = S_S. SALES S_I.QUANTITY > (SELECT AVG(QUANTITY) FROM SALES S1 WHERE S1. S. S. PRODUCT_NAME YEAR QUANTITY -------------------------Nokia 2010 25 IPhone 2012 20 Samsung 2012 20 Samsung 2010 20 2. Write a query to compare the products sales of "IPhone" and "Samsung" in each year? The output should look like as YEAR IPHONE_QUANT SAM_QUANT IPHONE_PRICE SAM_PRICE --------------------------------------------------2010 10 20 9000 7000 2011 15 18 9000 7000 2012 20 20 9000 7000 Solution: By using self-join SQL query we can get the required result.PRICE IPHONE_PRICE.QUANTITY FROM PRODUCTS P.PRODUCT_ID AND P_I.QUANTITY SAM_QUANT.PRODUCT_NAME.PRICE SAM_PRICE FROM PRODUCTS P_I. S_S.SELECT P.YEAR = S_S. The required SQL query is SELECT S_I.YEAR 3.PRODUCT_ID = S_I.YEAR. Write a query to find the ratios of the sales of a product? Solution: The ratio of a product is calculated as the total sales price in a particular year divide by . PRODUCTS P_S.PRODUCT_ID AND S.PRODUCT_NAME = 'IPhone' AND P_S. S_I.PRODUCT_ID ). SALES S_S WHERE P_I.PRODUCT_ID = S.PRODUCT_ID AND P_S.QUANTITY IPHONE_QUANT.PRODUCT_NAME = 'Samsung' AND S_I.YEAR.
PRODUCT_ID = S.163265306 Nokia 2011 0. Oracle provides RATIO_TO_REPORT analytical function for finding the ratios. In the SALES table quantity of each product is stored in rows for every year. then use the below query for transposing the row data into column data.YEAR.QUANTITY.PRODUCT_ID) )A PIVOT ( MAX(QUANTITY) AS QUAN FOR (YEAR) IN (2010. S.510204082 Samsung 2010 0.344827586 Samsung 2011 0. The SQL query is SELECT P.PRODUCT_NAME.333333333 IPhone 2012 0.YEAR FROM PRODUCTS P. The SQL query for this is SELECT * FROM ( SELECT P. SELECT P. SALES S WHERE (P. S.PRICE) OVER(PARTITION BY P. RATIO_TO_REPORT(S.2012)). .326530612 Nokia 2010 0.222222222 Nokia 2012 0. Now write a query to transpose the quantity for each product and display it in columns? The output should look like as PRODUCT_NAME QUAN_2010 QUAN_2011 QUAN_2012 -----------------------------------------IPhone 10 15 20 Samsung 20 18 20 Nokia 25 16 8 Solution: Oracle 11g provides a pivot function to transpose the row data into column data. PRODUCT_NAME YEAR RATIO ----------------------------IPhone 2011 0. SALES S WHERE (P.344827586 Samsung 2012 0.the total sales price across all years.PRODUCT_NAME.444444444 IPhone 2010 0.310344828 4.PRODUCT_ID = S.PRODUCT_ID).QUANTITY*S.PRODUCT_NAME. S.PRODUCT_NAME ) SALES_RATIO FROM PRODUCTS P.2011. If you are not running oracle 11g database.
YEAR NUM_PRODUCTS -----------------2010 3 2011 3 2012 3 Recommended Posts: SQL Queries Interview Questions .Oracle Part 3 SQL Queries Interview Questions .PRODUCT_ID = S.QUANTITY)) QUAN_2011. The SQL query for this question is SELECT YEAR. Email ThisBlogThis!Share to TwitterShare to Facebook Labels: Oracle.MAX(DECODE(S. MAX(DECODE(S. S.YEAR. then please share it on Google by clicking on the +1 button.2011.YEAR. Write a query to find the number of products sold in each year? Solution: To get this result we have to group by on year and the find the count. S. .PRODUCT_NAME.Oracle Part 4 SQL Queries Interview Questions .QUANTITY)) QUAN_2010.Oracle Part 2 SQL Queries Interview Questions . MAX(DECODE(S. S.PRODUCT_ID) GROUP BY P.2010.Oracle Part 1 SQL Queries Interview Questions . Oracle Interview Questions 10 comments: 1. 5. SALES S WHERE (P.Oracle Part 5 If you like this post. COUNT(1) NUM_PRODUCTS FROM SALES GROUP BY YEAR.YEAR.2012.QUANTITY)) QUAN_2012 FROM PRODUCTS P.
QUANTITY FROM PRODUCTS P. 2012 21:43 Hi Vijay.YEAR.PRODUCT_ID AND S. SALES S WHERE P. S. S.QUANTITY > (SELECT AVG(QUANTITY) FROM SALES S ).PRODUCT_ID = S. .PRODUCT_NAME. For 1st query we need to write query like given below then only we will get all values which are greater than the avg value.Yuvakesh Y04 July. SELECT P.
2012 22:00 This works when you want to find the avg across all the years (ignoring products).Correct me if am wrong. Delete . In my requirement i want to find the products whose quantity is greater than the average quantity of the product across all the years. vijay bhaskar04 July. ReplyDelete Replies 1.
SALES S WHERE P. SELECT P.PRODUCT_ID = S.P.YEAR) order by year .QUANTITY > (SELECT AVG(QUANTITY) FROM SALES S1 WHERE S1.2. 2012 08:10 i think below query will not work as year 2011 is not included in the output. S.PRODUCT_NAME. shaukat pathan03 October. this should have below query correct me if i am wrong.QUANTITY FROM PRODUCTS P.YEAR = S.PRODUCT_id.PRODUCT_ID AND S.YEAR. S.
vijay bhaskar03 October.Delete 3. 2012 08:30 Year 2011 is not qualified as the product sales is less than the average sales Delete Reply .
2012 04:43 Hi Vijay.2. Below is the Input table. I want to make this in mysql. Yuvakesh Y01 August. . City Name Count A Yuvakesh 2 C Yuvakesh 4 D Yuvakesh 3 A Raj 1 C Raj 4 E Raj 3 here based on the Name i need to calculate the sum(2+4+3) and i need to display all the rows as it is along with sum as shown in below table.
Yuvakesh Y02 August. Thanks in advance. 2012 22:23 Thanks a lot Vijay. a... sum(count_c) sum_s from tablename group by Name ) a.count_c.Name = t.Name Delete 2. am able to get desired result by using the above query. vijay bhaskar01 August.OutPut: City Name Count sum A Yuvakesh 2 9 C Yuvakesh 4 9 D Yuvakesh 3 9 A Raj 1 8 C Raj 4 8 E Raj 3 8 Please let me know how to get the desired result in mysql. ReplyDelete Replies 1. ..sum_s from ( select Name.city. tablename t where a. This is shown below: select t.Name. t. 2012 23:11 I am not sure about analytic functions in mysql. However you can get the required output by using two sub queries and joining them. t.
Yuvakesh Y14 September. vijay bhaskar14 September. 2012 00:25 Hi Vijay. Regards.. 2012 06:30 Hi. Thanks in advance. Right now i dont have access to the db..Regards. Then i will provide the exact sql queries on monday. I would like to delete 'last_data_updated' for table_id group from my etl_run table by using the below query Delete from etl_run where last_data_updated not in (select max(last_data_updated) as last_data_updated from `etl_run` where status='Completed' group by table_ID). Yuvakesh Delete Reply 3.. Yuvakesh ReplyDelete Replies 1. let me know. But am facing an error like 'you can't specify target table for updat.. delete from etl_run t where not exists . I am providing rough sql queries here. Try them and if they didn't work.' Please suggest me how to achieve this in another way.
SQL Queries Interview Questions . Yuvakesh Y18 September.(select 1 from etl_run where status ='completed' and table_id = t. that i will rectify them. 1. If you find any bugs in the queries.Oracle Part 3 Here I am providing Oracle SQL Query Interview Questions. Thanks a lot.table_id ).'DD-MON-YYYY')+LEVEL-1 C_DATE FROM DUAL . TO_CHAR(C_DATE. 2000 to till now? Solution: SELECT C_DATE. 2. Delete 2. Write a query to display only friday dates from Jan. delete from etl_run t where last_data_updated != (select max(last_data_updated) from etl_run where status ='completed' and table_id = t.last_data_updated ). Please do comment. So. 2012 02:54 Thanks Vijay.table_id group by table_id having max(last_data_updated) = t.with the help of the above logic I have developed required query. Write a query to generate sequence numbers from 1 to the specified number N? Solution: SELECT LEVEL FROM DUAL CONNECT BY LEVEL<=&N.'DY') FROM ( SELECT TO_DATE('01-JAN-2000'.
3 A. 5 B. 3 B.1) A FROM DUAL CONNECT BY LEVEL <=LENGTH('SMILE').'DD-MON-YYYY')+1) ) WHERE TO_CHAR(C_DATE. 3 B. Write a query to display each letter of the word "SMILE" in a separate row? S M I L E Solution: SELECT SUBSTR('SMILE'. 4. 5 B.PRODUCTS. .LEVEL. 5 C. Repeat ---------------A. B should be repeated 5 times and C should be repeated 2 times.REPEAT >= A. Repeat ---------------A.CONNECT BY LEVEL <= (SYSDATE . ( SELECT LEVEL L FROM DUAL CONNECT BY LEVEL <= (SELECT MAX(REPEAT) FROM T) ) A WHERE T.L ORDER BY T.TO_DATE('01-JAN-2000'. 3 A. 5 B. 2 Solution: SELECT PRODUCTS. 2 C.'DY') = 'FRI'. 2 Now in the output data. REPEAT FROM T. 3. the product A should be repeated 3 times. 5 B. 5 C. Write a query to duplicate each row based on the value in the repeat column? The input table data looks like as below Products. The output will look like as below Products.
If you pass a string to the ascii function.Oracle Part 3 SQL Queries Interview Questions . Solution1: SELECT SUBSTR(DUMP('SMILE').69. ram and jhon are friends of vamsi and so on. ram vamsi.76. Friend_of_Firend ---------------------sam. Friend_Name ----------------sam. The ASCII function will give ascii value for only one character.Oracle Part 5 SQL Queries Interview Questions .LEVEL.Oracle Part 1 SQL Queries Interview Questions . ram . it will give the ascii value of first letter in the string. anand Here ram and vamsi are friends of sam. jhon ram. Recommended Posts: SQL Queries Interview Questions .5. The output should look as Name.77. ram sam. Here i am providing two solutions to get the ascii values of string. Convert the string "SMILE" to Ascii values? The output should look like as 83. Solution2: SELECT WM_CONCAT(A) FROM ( SELECT ASCII(SUBSTR('SMILE'.jhon. vamsi vamsi. ram.Oracle Part 2 SQL Queries Interview Questions .1)) A FROM DUAL CONNECT BY LEVEL <=LENGTH('SMILE') ). vijay ram. For sam. Consider the following friends table as the source Name.vijay and anand are friends of friends. Now write a query to find friends of friends of sam.Oracle Part 4 1.Oracle Part 4 SQL Queries Interview Questions . Where 83 is the ascii value of S and so on.15) FROM DUAL.73.
The outuput should look as Name. quantity_sold. 260.friend_name = f2. 155. 3.name.name. 2009 B. 2009 G. In the output. Now extend the above query to exclude mutual friends. 2010 L. 910. friends f2 f1.friend_name as friend_of_friend friends f1. you can see ram is displayed as friends of friends. 2.friend_name as friend_of_friend friends f1.friend_name = f2. sam.friend_name = f2. 2010 H.name = f1.name NOT EXISTS (SELECT 1 FROM friends f3 WHERE f3. This is because. Friend_of_Friend ---------------------sam. 999. Write a query to get the top 5 products based on the quantity sold without using the row_number analytical function? The source data looks as Products. friends f2 f1.friend_name). This is an extension to the problem 1. f2. jhon vijay anand Solution: SELECT FROM WHERE AND f1.name AND f3. 200. 2009 F. f2. vijay sam.name = 'sam' f1. 455. 2009 E. 580.name = 'sam' f1.name. 2010 M. 620. jhon sam. 2010 J. 390. year ----------------------------A.sam. sam. 2010 I. 2010 . 2009 D. 2009 C. 135. 810. anand Solution: SELECT FROM WHERE AND AND f1. 109. ram is mutual friend of sam and vamsi.
write a query to get the top 5 products in each year based on the quantity sold? Solution: SELECT products. quantity_sold. year )A WHERE r <= 5. quantity_sold. row_number() OVER( PARTITION BY year ORDER BY quantity_sold DESC) r from t )A . year. quantity_sold. 5. Write a query to produce the same output using row_number analytical function? Solution: SELECT products. row_number() OVER( ORDER BY quantity_sold DESC) r from t )A WHERE r <= 5. year FROM ( SELECT products. This is an extension to the problem 3. This is an extension to the problem 3.Solution: SELECT FROM ( SELECT products. quantity_sold. year FROM ( SELECT products. year. rownum r from t ORDER BY quantity_sold DESC 4. products. quantity_sold. quantity_sold. year.
'BlackBerry'). 'Samsung'). Target table should always contain the products loaded in 30 days.Oracle Part 1 SQL Queries Interview Questions . SELECT * FROM PRODUCTS.Oracle Part 3 SQL Queries Interview Questions . PRODUCT_ID PRODUCT_NAME ----------------------100 Nokia 200 IPhone 300 Samsung 400 LG 500 BlackBerry 600 Motorola The requirements for loading the target table are: • • • Select only 2 products randomly. PRODUCTS PRODUCTS PRODUCTS PRODUCTS PRODUCTS PRODUCTS VALUES VALUES VALUES VALUES VALUES VALUES ( ( ( ( ( ( 100.Oracle Part 4 SQL Queries Interview Questions .WHERE r <= 5. Solution: . 'Nokia'). PRODUCT_NAME VARCHAR2(30) ). 'IPhone'). 600. 300. 'Motorola'). CREATE TABLE PRODUCTS ( PRODUCT_ID INTEGER. INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO COMMIT. Recommended Posts: SQL Queries Interview Questions . 'LG').Oracle Part 5 SQL Query Interview Questions . 400. Do not select the products which are already loaded in the target table with in the last 30 days. 500.Part 5 Write SQL queries for the below interview questions: 1.Oracle Part 2 SQL Queries Interview Questions . It should not contain the products which are loaded prior to 30 days. 200. Load the below products table into the target table.
The last step is to delete the products from the table which are loaded 30 days back. (6. INSERT_DATE DATE ).PRODUCT_ID = S.PRODUCT_ID ) ORDER BY DBMS_RANDOM. SYSDATE INSERT_DATE FROM ( SELECT PRODUCT_ID. DELETE FROM TGT_PRODUCTS WHERE INSERT_DATE < SYSDATE . (3. The next step is to pick 5 products randomly and then load into target table. The target table will have an additional column INSERT_DATE to know when a product is loaded into the target table. PRODUCT_NAME FROM PRODUCTS S WHERE NOT EXISTS ( SELECT 1 FROM TGT_PRODUCTS T WHERE T. (5.'MAGAZINE').VALUE --Random number generator in oracle. PRODUCT_NAME.'AUDIO'). CREATE TABLE CONTENTS ( CONTENT_ID INTEGER. (4. CONTENT_TYPE VARCHAR2(30) ). PRODUCT_NAME VARCHAR2(30). INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO INSERT INTO COMMIT. While selecting check whether the products are there in the INSERT INTO TGT_PRODUCTS SELECT PRODUCT_ID. )A WHERE ROWNUM <= 2. 2. SELECT * FROM CONTENTS.First we will create a target table. .'MOVIE').30. (2.'MOVIE'). Load the below CONTENTS table into the target table.'AUDIO').'MAGAZINE'). CONTENTS CONTENTS CONTENTS CONTENTS CONTENTS CONTENTS VALUES VALUES VALUES VALUES VALUES VALUES (1. The target table structure is CREATE TABLE TGT_PRODUCTS ( PRODUCT_ID INTEGER.
. Solution: First we will create a lookup table where we mention the priorities for the content types.1. INSERT INTO CONTENTS_LKP VALUES('MAGAZINE'. second AUDIO. COMMIT. PRIORITY INTEGER.1). The second step is to truncate the target table before loading the data TRUNCATE TABLE TGT_CONTENTS.3. INSERT INTO CONTENTS_LKP VALUES('MOVIE'. The other content types will have LOAD_FLAG as 0. First MOVIE. The loading of content types should follow round-robin style. CREATE TABLE CONTENTS_LKP ( CONTENT_TYPE VARCHAR2(30).0). The target table structure is same as the source table structure. SELECT * FROM CONTENTS_LKP. Third MAGAZINE and again fourth Movie. Only one content type will have LOAD_FLAG as 1. The target table should always contain only one contain type.0). then it indicates which content type needs to be loaded into the target table. The lookup table “Create Statement” and data is shown below. CONTENT_TYPE PRIORITY LOAD_FLAG --------------------------------MOVIE 1 1 AUDIO 2 0 MAGAZINE 3 0 Here if LOAD_FLAG is 1. LOAD_FLAG INTEGER ).CONTENT_ID CONTENT_TYPE ----------------------1 MOVIE 2 MOVIE 3 AUDIO 4 AUDIO 5 MAGAZINE 6 MAGAZINE The requirements to load the target table are: • • • Load only one content type at a time into the target table. INSERT INTO CONTENTS_LKP VALUES('AUDIO'.2.
PRIORITY+1) FROM CONTENTS_LKP WHERE CONTENT_TYPE = (SELECT DISTINCT CONTENT_TYPE FROM TGT_CONTENTS) ). UPDATE CONTENTS_LKP SET LOAD_FLAG = 1 WHERE PRIORITY = ( SELECT DECODE( PRIORITY. CONTENT_TYPE FROM CONTENTS WHERE CONTENT_TYPE = (SELECT CONTENT_TYPE FROM CONTENTS_LKP WHERE LOAD_FLAG=1).1 . INSERT INTO TGT_CONTENTS SELECT CONTENT_ID. The last step is to update the LOAD_FLAG of the Lookup table.(SELECT MAX(PRIORITY) FROM CONTENTS_LKP) . UPDATE CONTENTS_LKP SET LOAD_FLAG = 0 WHERE LOAD_FLAG = 1. .The third step is to choose the appropriate content type from the lookup table to load the source data into the target table.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.