Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

SQL Query Interview Questions - Part 5

Uploaded by

RaJu Bhai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

SQL Query Interview Questions - Part 5

Uploaded by

RaJu Bhai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

11/29/2018 SQL Query Interview Questions - Part 5

Home Data Warehouse Informatica Informatica Scenarios Informatica Cloud Oracle Unix Hadoop

Search... Search
SQL Query Interview Questions - Part 5
Write SQL queries for the below interview questions:
Popular Posts

Informatica Scenario Based Interview Questions with


Answers - Part 1
1. Load the below products table into the target table.
Unix Sed Command to Delete Lines in File - 15 Examples

CREATE TABLE PRODUCTS String Functions in Hive

(
Top Examples of Awk Command in Unix
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(30) Sed Command in Unix and Linux Examples
);
Design/Implement/Create SCD Type 2 Effective Date
Mapping in Informatica
INSERT INTO PRODUCTS VALUES ( 100, 'Nokia');
INSERT INTO PRODUCTS VALUES ( 200, 'IPhone'); Date Functions in Hive
INSERT INTO PRODUCTS VALUES ( 300, 'Samsung');
INSERT INTO PRODUCTS VALUES ( 400, 'LG'); SQL Queries Interview Questions - Oracle Part 1
INSERT INTO PRODUCTS VALUES ( 500, 'BlackBerry');
Top Unix Interview Questions - Part 1
INSERT INTO PRODUCTS VALUES ( 600, 'Motorola');
COMMIT; Update Strategy Transformation in Informatica

SELECT * FROM PRODUCTS;


Have Questions? Follow Me
PRODUCT_ID PRODUCT_NAME
-----------------------
100 Nokia
200 IPhone
300 Samsung
400 LG

https://www.folkstalk.com/2012/01/sql-interview-questions-query-part-1.html 1/7
11/29/2018 SQL Query Interview Questions - Part 5

500 BlackBerry vijay bhaskar


600 Motorola Add to circles

The requirements for loading the target table are:


Select only 2 products randomly.
Do not select the products which are already loaded in the target table with in the last
30 days.
Target table should always contain the products loaded in 30 days. It should not
contain the products which are loaded prior to 30 days.
Solution:

First we will create a target table. The target table will have an additional column INSERT_DATE
to know when a product is loaded into the target table. The target
table structure is
994 have me in circles View all

CREATE TABLE TGT_PRODUCTS


(
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(30),
INSERT_DATE DATE
);

The next step is to pick 5 products randomly and then load into target table. While selecting
check whether the products are there in the

INSERT INTO TGT_PRODUCTS


SELECT PRODUCT_ID,
PRODUCT_NAME,
SYSDATE INSERT_DATE
FROM
(
SELECT PRODUCT_ID,

https://www.folkstalk.com/2012/01/sql-interview-questions-query-part-1.html 2/7
11/29/2018 SQL Query Interview Questions - Part 5

PRODUCT_NAME
FROM PRODUCTS S
WHERE NOT EXISTS (
SELECT 1
FROM TGT_PRODUCTS T
WHERE T.PRODUCT_ID = S.PRODUCT_ID
)
ORDER BY DBMS_RANDOM.VALUE --Random number generator in oracle.
)A
WHERE ROWNUM <= 2;

The last step is to delete the products from the table which are loaded 30 days back.

DELETE FROM TGT_PRODUCTS


WHERE INSERT_DATE < SYSDATE - 30;

2. Load the below CONTENTS table into the target table.

CREATE TABLE CONTENTS


(
CONTENT_ID INTEGER,
CONTENT_TYPE VARCHAR2(30)
);

INSERT INTO CONTENTS VALUES (1,'MOVIE');


INSERT INTO CONTENTS VALUES (2,'MOVIE');
INSERT INTO CONTENTS VALUES (3,'AUDIO');
INSERT INTO CONTENTS VALUES (4,'AUDIO');
INSERT INTO CONTENTS VALUES (5,'MAGAZINE');
INSERT INTO CONTENTS VALUES (6,'MAGAZINE');
COMMIT;

SELECT * FROM CONTENTS;


https://www.folkstalk.com/2012/01/sql-interview-questions-query-part-1.html 3/7
11/29/2018 SQL Query Interview Questions - Part 5

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.
The target table should always contain only one contain type.
The loading of content types should follow round-robin style. First MOVIE, second
AUDIO, Third MAGAZINE and again fourth Movie.

Solution:

First we will create a lookup table where we mention the priorities for the content types. The
lookup table “Create Statement” and data is shown below.

CREATE TABLE CONTENTS_LKP


(
CONTENT_TYPE VARCHAR2(30),
PRIORITY INTEGER,
LOAD_FLAG INTEGER
);

INSERT INTO CONTENTS_LKP VALUES('MOVIE',1,1);


INSERT INTO CONTENTS_LKP VALUES('AUDIO',2,0);
INSERT INTO CONTENTS_LKP VALUES('MAGAZINE',3,0);
COMMIT;

SELECT * FROM CONTENTS_LKP;

https://www.folkstalk.com/2012/01/sql-interview-questions-query-part-1.html 4/7
11/29/2018 SQL Query Interview Questions - Part 5

CONTENT_TYPE PRIORITY LOAD_FLAG


---------------------------------
MOVIE 1 1
AUDIO 2 0
MAGAZINE 3 0

Here if LOAD_FLAG is 1, then it indicates which content type needs to be loaded into the target
table. Only one content type will have LOAD_FLAG as 1. The other content types will have
LOAD_FLAG as 0. The target table structure is same as the source table structure.

The second step is to truncate the target table before loading the data

TRUNCATE TABLE TGT_CONTENTS;

The third step is to choose the appropriate content type from the lookup table to load the source
data into the target table.

INSERT INTO TGT_CONTENTS


SELECT CONTENT_ID,
CONTENT_TYPE
FROM CONTENTS
WHERE CONTENT_TYPE = (SELECT CONTENT_TYPE FROM CONTENTS_LKP WHERE LOAD_FLAG=1

The last step is to update the LOAD_FLAG of the Lookup table.

UPDATE CONTENTS_LKP
SET LOAD_FLAG = 0
WHERE LOAD_FLAG = 1;

https://www.folkstalk.com/2012/01/sql-interview-questions-query-part-1.html 5/7
11/29/2018 SQL Query Interview Questions - Part 5

UPDATE CONTENTS_LKP
SET LOAD_FLAG = 1
WHERE PRIORITY = (
SELECT DECODE( PRIORITY,(SELECT MAX(PRIORITY) FROM CONTENTS_LKP) ,1 , PRIORIT
FROM CONTENTS_LKP
WHERE CONTENT_TYPE = (SELECT DISTINCT CONTENT_TYPE FROM TGT_CONTENTS)
);

Recommended Posts:

SQL Query Interview Questions


SQL Query Interview Questions On Connect By Clause
Oracle Analytical Functions
How to find (calculate) median using oracle sql query
Oracle Complex Queries

If you like this post, then please share it on Google by clicking on the +1 button.

No comments:

Post a Comment

Enter your comment...

Comment as: Google Accoun

Publish Preview

https://www.folkstalk.com/2012/01/sql-interview-questions-query-part-1.html 6/7
11/29/2018 SQL Query Interview Questions - Part 5

Newer Post Home Older Post

Subscribe to: Post Comments (Atom)

https://www.folkstalk.com/2012/01/sql-interview-questions-query-part-1.html 7/7

You might also like