Professional Documents
Culture Documents
SQL Queries Interview Questions - Oracle Part 1
SQL Queries Interview Questions - Oracle Part 1
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 exampleson 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.
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(30)
);
SALE_ID INTEGER,
PRODUCT_ID INTEGER,
YEAR INTEGER,
Quantity INTEGER,
PRICE INTEGER
);
INSERT INTO PRODUCTS VALUES ( 100, 'Nokia');
COMMIT;
The products table contains the below data.
PRODUCT_ID PRODUCT_NAME
-----------------------
100 Nokia
200 IPhone
300 Samsung
--------------------------------------
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 belowSQL 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
-----------------------------------------
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
Here the lead analytic function will get the quantity of a product in its previous year.
STEP2: We will find the difference between the quantities of a product with its previous years quantity.
If this difference is greater than or equal to zero for all the rows, then the product is a constantly
increasing in sales. The final query to get the required result is
SELECT PRODUCT_NAME
FROM
SELECT P.PRODUCT_NAME,
S.QUANTITY -
LEAD(S.QUANTITY,1,0) OVER (
PARTITION BY P.PRODUCT_ID
) QUAN_DIFF
FROM PRODUCTS P,
SALES S
GROUP BY PRODUCT_NAME
PRODUCT_NAME
------------
IPhone
2. 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. This can be achieved in three ways.
SELECT P.PRODUCT_NAME
FROM PRODUCTS P
SALES S
ON (P.PRODUCT_ID = S.PRODUCT_ID);
------------
LG
SELECT P.PRODUCT_NAME
FROM PRODUCTS P
PRODUCT_NAME
------------
LG
SELECT P.PRODUCT_NAME
FROM PRODUCTS P
WHERE NOT EXISTS
PRODUCT_NAME
------------
LG
3. 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 withthe sales in
the year 2011. The SQL query to get the required output is
SELECT P.PRODUCT_NAME
FROM PRODUCTS P,
SALES S_2012,
SALES S_2011
PRODUCT_NAME
------------
Nokia
Solution:
Nokia is the top product sold in the year 2010. Similarly, Samsung in 2011 and IPhone, Samsung in
2012. The query for this is
SELECT PRODUCT_NAME,
YEAR
FROM
SELECT P.PRODUCT_NAME,
S.YEAR,
RANK() OVER (
PARTITION BY S.YEAR
) RNK
FROM PRODUCTS P,
SALES S
)A
WHERE RNK = 1;
PRODUCT_NAME YEAR
--------------------
Nokia 2010
Samsung 2011
IPhone 2012
Samsung 2012
This is a simple query. You just need to group by the data on PRODUCT_NAME and then find the sum
of sales.
SELECT P.PRODUCT_NAME,
FROM PRODUCTS P
SALES S
ON (P.PRODUCT_ID = S.PRODUCT_ID)
GROUP BY P.PRODUCT_NAME;
PRODUCT_NAME TOTAL_SALES
---------------------------
LG 0
IPhone 405000
Samsung 406000
Nokia 245000
SQL Queries Interview Questions - Oracle Part 2
This is continuation to my previous post, SQL Queries Interview Questions - Oracle Part 1, Where i
have used PRODUCTS and SALES tables as an example. Here also i am using the same tables. So,
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.
1. Write a query to find the products whose quantity sold in a year should be greater than the average
quantity sold across all the years?
Solution:
This can be solved with the help of correlated query. The SQL query for this is
SELECT P.PRODUCT_NAME,
S.YEAR,
S.QUANTITY
FROM PRODUCTS P,
SALES S
(SELECT AVG(QUANTITY)
FROM SALES S1
--------------------------
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
---------------------------------------------------
Solution:
By using self-join SQL query we can get the required result. The required SQL query is
SELECT S_I.YEAR,
S_I.QUANTITY IPHONE_QUANT,
S_S.QUANTITY SAM_QUANT,
S_I.PRICE IPHONE_PRICE,
S_S.PRICE SAM_PRICE
SALES S_I,
PRODUCTS P_S,
SALES S_S
Solution:
The ratio of a product is calculated as the total sales price in a particular year divide by the total
sales price across all years. Oracle provides RATIO_TO_REPORT analytical function for finding the
ratios. The SQL query is
SELECT P.PRODUCT_NAME,
S.YEAR,
RATIO_TO_REPORT(S.QUANTITY*S.PRICE)
FROM PRODUCTS P,
SALES S
-----------------------------
4. In the SALES table quantity of each product is stored in rows for every year. Now write a query to
transpose the quantity for each product and display it in columns? The output should look like as
------------------------------------------
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. The SQL query for this
is
SELECT * FROM
SELECT P.PRODUCT_NAME,
S.QUANTITY,
S.YEAR
FROM PRODUCTS P,
SALES S
)A
If you are not running oracle 11g database, then use the below query for transposing the row data into
column data.
SELECT P.PRODUCT_NAME,
FROM PRODUCTS P,
SALES S
GROUP BY P.PRODUCT_NAME;
5. 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. The SQL query for this question is
SELECT YEAR,
COUNT(1) NUM_PRODUCTS
FROM SALES
GROUP BY YEAR;
YEAR NUM_PRODUCTS
------------------
2010 3
2011 3
2012 3
Solution:
SELECT LEVEL FROM DUAL CONNECT BY LEVEL<=&N;
2. Write a query to display only friday dates from Jan, 2000 to till now?
Solution:
SELECT C_DATE,
TO_CHAR(C_DATE,'DY')
FROM
FROM DUAL
(SYSDATE - TO_DATE('01-JAN-2000','DD-MON-YYYY')+1)
3. Write a query to duplicate each row based on the value in the repeat column? The input table data
looks like as below
Products, Repeat
----------------
A, 3
B, 5
C, 2
Now in the output data, the product A should be repeated 3 times, B should be repeated 5 times and C
should be repeated 2 times. The output will look like as below
Products, Repeat
----------------
A, 3
A, 3
A, 3
B, 5
B, 5
B, 5
B, 5
B, 5
C, 2
C, 2
Solution:
SELECT PRODUCTS,
REPEAT
FROM T,
)A
ORDER BY T.PRODUCTS;
4. Write a query to display each letter of the word "SMILE" in a separate row?
Solution:
SELECT SUBSTR('SMILE',LEVEL,1) A
FROM DUAL
5. Convert the string "SMILE" to Ascii values? The output should look like as 83,77,73,76,69. Where 83
is the ascii value of S and so on.
The ASCII function will give ascii value for only one character. If you pass a string to the ascii function, it
will give the ascii value of first letter in the string. Here i am providing two solutions to get the ascii
values of string.
Solution1:
SELECT SUBSTR(DUMP('SMILE'),15)
FROM DUAL;
Solution2:
SELECT WM_CONCAT(A)
FROM
SELECT ASCII(SUBSTR('SMILE',LEVEL,1)) A
FROM DUAL
);
SQL Queries Interview Questions - Oracle Part 4
1. Consider the following friends table as the source
Name, Friend_Name
-----------------
sam, ram
sam, vamsi
vamsi, ram
vamsi, jhon
ram, vijay
ram, anand
Here ram and vamsi are friends of sam; ram and jhon are friends of vamsi and so on. Now write a
query to find friends of friends of sam. For sam; ram,jhon,vijay and anand are friends of friends. The
output should look as
Name, Friend_of_Firend
----------------------
sam, ram
sam, jhon
sam, vijay
sam, anand
Solution:
SELECT f1.name,
f2.friend_name as friend_of_friend
friends f2
2. This is an extension to the problem 1. In the output, you can see ram is displayed as friends of
friends. This is because, ram is mutual friend of sam and vamsi. Now extend the above query to
exclude mutual friends. The outuput should look as
Name, Friend_of_Friend
----------------------
sam, jhon
sam, vijay
sam, anand
Solution:
SELECT f1.name,
f2.friend_name as friend_of_friend
FROM friends f1,
friends f2
3. 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
-----------------------------
A, 200, 2009
B, 155, 2009
C, 455, 2009
D, 620, 2009
E, 135, 2009
F, 390, 2009
G, 999, 2010
H, 810, 2010
I, 910, 2010
J, 109, 2010
L, 260, 2010
M, 580, 2010
Solution:
SELECT products,
quantity_sold,
year
FROM
SELECT products,
quantity_sold,
year,
rownum r
from t
ORDER BY quantity_sold DESC
)A
WHERE r <= 5;
4. This is an extension to the problem 3. Write a query to produce the same output using row_number
analytical function?
Solution:
SELECT products,
quantity_sold,
year
FROM
SELECT products,
quantity_sold,
year,
row_number() OVER(
from t
)A
WHERE r <= 5;
5. This is an extension to the problem 3. write a query to get the top 5 products in eachyear based on
the quantity sold?
Solution:
SELECT products,
quantity_sold,
year
FROM
SELECT products,
quantity_sold,
year,
row_number() OVER(
PARTITION BY year
from t
)A
WHERE r <= 5;
SQL Query Interview Questions - Part 5
Write SQL queries for the below interview questions:
PRODUCT_ID INTEGER,
PRODUCT_NAME VARCHAR2(30)
);
COMMIT;
SELECT * FROM PRODUCTS;
PRODUCT_ID PRODUCT_NAME
-----------------------
100 Nokia
200 IPhone
300 Samsung
400 LG
500 BlackBerry
600 Motorola
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 shouldnot 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
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
SELECT PRODUCT_ID,
PRODUCT_NAME,
SYSDATE INSERT_DATE
FROM
SELECT PRODUCT_ID,
PRODUCT_NAME
FROM PRODUCTS S
FROM TGT_PRODUCTS T
)A
The last step is to delete the products from the table which are loaded 30 days back.
CONTENT_ID INTEGER,
CONTENT_TYPE VARCHAR2(30)
);
INSERT INTO CONTENTS VALUES (1,'MOVIE');
COMMIT;
CONTENT_ID CONTENT_TYPE
-----------------------
1 MOVIE
2 MOVIE
3 AUDIO
4 AUDIO
5 MAGAZINE
6 MAGAZINE
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.
CONTENT_TYPE VARCHAR2(30),
PRIORITY INTEGER,
LOAD_FLAG INTEGER
);
COMMIT;
---------------------------------
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 tablestructure.
The second step is to truncate the target table before loading the data
The third step is to choose the appropriate content type from the lookup table to load the source data
into the target table.
CONTENT_TYPE
FROM CONTENTS
UPDATE CONTENTS_LKP
SET LOAD_FLAG = 0
WHERE LOAD_FLAG = 1;
UPDATE CONTENTS_LKP
SET LOAD_FLAG = 1
WHERE PRIORITY = (
FROM CONTENTS_LKP
);
Grep Command in Unix and Linux Examples
Grep is the frequently used command in Unix (or Linux). Most of us use grep just for finding the words
in a file. The power of grep comes with using its options and regularexpressions. You can analyze large
sets of log files with the help of grep command.
Grep stands for Global search for Regular Expressions and Print.
This saves a lot of time if you are executing the same command again and again.
!grep
This displays the last executed grep command and also prints the result set of the command on the
terminal.
This is the basic usage of grep command. It searches for the given string in the specified file.
This searches for the string "Error" in the log file and prints all the lines that has the word "Error".
This is also the basic usage of the grep command. You can manually specify the list of files you want to
search or you can specify a file pattern (use regular expressions) tosearch for.
This will search for the lines which starts with a number. Regular expressions is huge topic and I am not
covering it here. This example is just for providing the usage of regularexpressions.
By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option
to grep makes it match only the whole words.
Some times, if you are searching for an error in a log file; it is always good to know the lines around the
error lines to know the cause of the error.
This will prints the matched lines along with the two lines before the matched lines.
This will display the matched lines along with the three lines after the matched lines.
This will display the matched lines and also five lines before and after the matched lines.
grep -r "string" *
You can display the lines that are not matched with the specified search sting patternusing the -v option.
You can remove the blank lines using the grep command.
We can find the number of lines that matches the given string/pattern
We can just display the files that contains the given string/pattern.
15. Display the file names that do not contain the pattern.
We can display the files which do not contain the matched string/pattern.
We can make the grep command to display the position of the line which contains the matched string in
a file using the -n option
The -b option allows the grep command to display the character position of the matched string in a file.
The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines
which start with the given string or pattern.
The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines
which end with the given string or pattern.
1. Listing files
The first thing after logging into the unix system, everyone does is listing the files in a directory. The ls
command is used to list the files in a directory.
>ls
add.sh
logfile.txt
prime.pl
If you simply execute ls on the command prompt, then it will display the files and directories in the
current directory.
>ls /usr/local/bin
You can pass a directory as an argument to ls command. In this case, the ls command prints all the files
and directories in the specific directory you have passed.
The next thing is to display the contents of a file. The cat command is used to display thecontents in a
file.
>cat file.txt
The head command can be used to print the specified number of lines from the starting of a file. The
below head command displays the first five lines of file.
>head -5 logfile.dat
The tail command can be used to print the specified number of lines from the ending of a file. The
below tail command displays the last three lines of file.
>tail -3 logfile.dat
The cd command can be used to change from one directory to another directory. You need to specify
the target directory where you want to go.
>cd /var/tmp
6. Creating a file.
The touch command simply creates an empty file. The below touch command creates a new file in the
current directory.
touch new_file.txt
The cp command is used to copy the content of source file into the target file. If the target file already
have data, then it will be overwritten.
Directories are a way of organizing your files. The mkdir command is used to create the specified
directory.
>mkdir backup
The mv command is used to rename the files and it also used for moving the files from one directory
into another directory.
The wc command can be used to find the number of line, words and characters in a file.
>wc logfile.txt
21 26 198 logfile.txt
To know about the unix command, it is always good to see the man pages. To see the man pages
simply pass the command as an argument to the man.
man ls
Before starting with the interview questions, we will see the difference between the aggregate functions
and analytic functions with an example. I have used SALES TABLEas an example to solve
the interview questions. Please create the below sales table in your oracle database.
SALE_ID INTEGER,
PRODUCT_ID INTEGER,
YEAR INTEGER,
Quantity INTEGER,
PRICE INTEGER
);
INSERT INTO SALES VALUES ( 1, 100, 2008, 10, 5000);
COMMIT;
--------------------------------------
SELECT Year,
COUNT(1) CNT
FROM SALES
GROUP BY YEAR;
YEAR CNT
---------
2009 3
2010 3
2011 3
2008 3
2012 3
SELECT SALE_ID,
PRODUCT_ID,
Year,
QUANTITY,
PRICE,
FROM SALES;
SALE_ID PRODUCT_ID YEAR QUANTITY PRICE CNT
------------------------------------------
From the ouputs, you can observe that the aggregate functions return only one row per group whereas
analytic functions keeps all the rows in the gorup. Using the aggregate functions, the select clause
contains only the columns specified in group by clause and aggregate functions whereas in analytic
functions you can specify all the columns in thetable.
The PARTITION BY clause is similar to GROUP By clause, it specifies the window of rows that the
analytic funciton should operate on.
I hope you got some basic idea about aggregate and analytic functions. Now lets start with solving
the Interview Questions on Oracle Analytic Functions.
1. Write a SQL query using the analytic function to find the total sales(QUANTITY) of each product?
Solution:
SUM analytic function can be used to find the total sales. The SQL query is
SELECT PRODUCT_ID,
QUANTITY,
FROM SALES;
-----------------------------
100 12 71
100 10 71
100 25 71
100 16 71
100 8 71
200 15 72
200 10 72
200 20 72
200 14 72
200 13 72
300 20 94
300 18 94
300 17 94
300 20 94
300 19 94
2. Write a SQL query to find the cumulative sum of sales(QUANTITY) of each product? Here first sort
the QUANTITY in ascendaing order for each product and then accumulate the QUANTITY.
Cumulative sum of QUANTITY for a product = QUANTITY of current row + sum of QUANTITIES all
previous rows in that product.
Solution:
We have to use the option "ROWS UNBOUNDED PRECEDING" in the SUM analytic function to get the
cumulative sum. The SQL query to get the ouput is
SELECT PRODUCT_ID,
QUANTITY,
FROM SALES;
-----------------------------
100 8 8
100 10 18
100 12 30
100 16 46
100 25 71
200 10 10
200 13 23
200 14 37
200 15 52
200 20 72
300 17 17
300 18 35
300 19 54
300 20 74
300 20 94
The ORDER BY clause is used to sort the data. Here the ROWS UNBOUNDED PRECEDING option
specifies that the SUM analytic function should operate on the current row and the pervious rows
processed.
3. Write a SQL query to find the sum of sales of current row and previous 2 rows in a product group?
Sort the data on sales and then find the sum.
Solution:
SELECT PRODUCT_ID,
QUANTITY,
SUM(QUANTITY) OVER(
PARTITION BY PRODUCT_ID
FROM SALES;
------------------------------
100 25 25
100 16 41
100 12 53
100 10 38
100 8 30
200 20 20
200 15 35
200 14 49
200 13 42
200 10 37
300 20 20
300 20 40
300 19 59
300 18 57
300 17 54
The ROWS BETWEEN clause specifies the range of rows to consider for calculating the SUM.
Solution:
SELECT PRODUCT_ID,
QUANTITY,
--------------------------
100 8 12
100 10 12
100 12 12
100 16 12
100 25 12
200 10 14
200 13 14
200 14 14
200 15 14
200 20 14
300 17 19
300 18 19
300 19 19
300 20 19
300 20 19
5. Write a SQL query to find the minimum sales of a product without using the group by clause.
Solution:
SELECT PRODUCT_ID,
YEAR,
QUANTITY
FROM
SELECT PRODUCT_ID,
YEAR,
QUANTITY,
FROM SALES
) WHERE MIN_SALE_RANK = 1;
------------------------
100 2012 8
200 2010 10
300 2008 17
Normalization is the process of organizing the columns, tables of a database to minimize the
redundancy of data. Normalization involves in dividing large tables into smaller tables and defining
relationships between them. Normalization is used in OLTP systems.
First Normal Form: Duplicate columns from the same table needs to be eliminated. We have to
create separate tables for each group of related data and identify each row with a unique column or
set of columns (Primary Key)
Second Normal Form: First it should meet the requirement of first normal form. Removes the
subsets of data that apply to multiple rows of a table and place them in separate tables.
Relationships must be created between the new tables and their predecessors through the use of
foreign keys.
Third Normal Form: First it should meet the requirements of second normal form. Remove columns
that are not depending upon the primary key.
Fourth Normal Form: There should not be any multi-valued dependencies.
3. What is De-normalization?
De-normalization is the process of optimizing the read performance of a database by adding redundant
data or by grouping data. De-normalization is used in OLAP systems.
4. What is a Transaction?
A transaction is a logical unit of work performed against a database in which all steps must be
performed or none.
Atomic: Transactions must be atomic. Transactions must fail or succeed as a single unit.
Consistent: The database must always be in consistent state. There should not be any partial
transactions
Isolation: The changes made by a user should be visible only to that user until the transaction is
committed.
MOLAP: The data is stored in multi-dimensional cube. The storage is not in the relational database,
but in proprietary formats.
ROLAP: ROLAP relies on manipulating the data stored in the RDBMS for slicing and dicing
functionality.
HOLAP: HOLAP combines the advantages of both MOLAP and ROLAP. For summary type
information, HOLAP leverages on cube technology for faster performance. For detail information,
HOLAP can drill through the cube.
One to one relationship is a simple reference between two tables. Consider Customer and Address
tables as an example. A customer can have only one address and an address references only one
customer.
8. Explain one-to-many relationship with an example?
One-to-many relationships can be implemented by splitting the data into two tables with a primary key
and foreign key relationship. Here the row in one table is referenced by one or more rows in the other
table. An example is the Employees and Departments table, where the row in the Departments table is
referenced by one or more rows in the Employees table.
Many-to-Many relationship is created between two tables by creating a junction table with the key from
both the tables forming the composite primary key of the junction table.
An example is Students, Subjects and Stud_Sub_junc tables. A student can opt for one or more
subjects in a year. Similarly a subject can be opted by one or more students. So a junction table is
created to implement the many-to-many relationship.
SELECT Columns | *
FROM Table_Name
[WHERE Search_Condition]
[GROUP BY Group_By_Expression]
[HAVING Search_Condition]
Year product
-------------
2010 A
2010 B
2010 C
2010 D
2011 X
2011 Y
2011 Z
Here, in the output we will concatenate the products in each year by a comma separator. The desired
output is:
year product_list
------------------
2010 A,B,C,D
2011 X,Y,Z
The LISTAGG function can be used to aggregate the strings. You can pass the explicit delimiter to the
LISTAGG function.
SELECT year,
LISTAGG(product, ',') WITHIN GROUP (ORDER BY product) AS product_list
FROM products
GROUP BY year;
WM_CONCAT function:
You cannot pass an explicit delimiter to the WM_CONCAT function. It uses comma as the string
separator.
SELECT year,
wm_concat(product) AS product_list
FROM products
GROUP BY year;
The pviot operator converts row data to column data and also can do aggregates whileconverting. To
see how pivot operator works, consider the following "sales" table as anyexample
--------------------------------------
1 A 10
1 B 20
2 A 30
2 B 40
2 C 50
3 A 60
3 B 70
3 C 80
The rows of the "sales" table needs to be converted into columns as shown below
-----------------------------------------
1 10 20
2 30 40 50
3 60 70 80
SELECT *
SELECT *
pivot XML ( sum(price) as total_price for (product) IN ( SELECT distinct product from sales) )
If you are not using oracle 11g database, then you can implement the unpivot feature asconverting rows
to columns
Unpivot:
-----------------------------------------
1 10 20
2 30 40 50
3 60 70 80
---------------------------
1 A 10
1 B 20
2 A 30
2 B 40
2 C 50
3 A 60
3 B 70
3 C 80
SELECT *
FROM sales_rev
UNPIVOT [EXCLUDE NULLs | INCLUDE NULLs] (price FOR product IN (a_product AS 'A', b_product
AS 'B', c_product_c AS 'C'));
The columns price and product in the unpivot clause are required and these names need not to be
present in the table.
I know the problem is not clear without giving an example. Let say I have the Employees table with the
below data.
Table Name: Employees
Dept_Id Emp_Seq
---------------
10 1
10 2
10 3
10 5
10 6
10 8
10 9
10 11
20 1
20 2
I want to find the minimum and maximum values of continuous Emp_Seq numbers. The output should
look as.
-----------------------
10 1 3
10 5 6
10 8 9
10 11 11
20 1 2
Write an SQL query in oracle to find the minimum and maximum values of continuous Emp_Seq in
each department?
STEP1: First we will generate unique sequence numbers in each department using the Row_Number
analytic function in the Oracle. The SQL query is.
SELECT Dept_Id,
Emp_Seq,
FROM employees;
Dept_Id Emp_Seq rn
--------------------
10 1 1
10 2 2
10 3 3
10 5 4
10 6 5
10 8 6
10 9 7
10 11 8
20 1 1
20 2 2
STEP2: Subtract the value of rn from emp_seq to identify the continuous sequences as a group. The
SQL query is
SELECT Dept_Id,
Emp_Seq,
FROM employees;
---------------------------
10 1 0
10 2 0
10 3 0
10 5 1
10 6 1
10 8 2
10 9 2
10 11 3
20 1 0
20 2 0
STEP3: The combination of the Dept_Id and Dept_Split fields will become the group for continuous
rows. Now use group by on these fields and find the min and max values. The final SQL query is
SELECT Dept_Id,
MIN(Emp_Seq) Min_Seq,
MAX(Emp_Seq) Max_Seq
FROM
(
SELECT Dept_Id,
Emp_Seq,
FROM employees;
)A
I have seen people writing redundant sub-queries and worrying about their query performance. As an
example, find the total sales in each year and also the sales of product with id 10 in each year.
SELECT T.YEAR,
T.TOT_SAL,
P.PROD_10_SAL
(
SELECT YEAR,
SUM(PRICE) TOT_SAL
FROM SALES
GROUP BY YEAR
)T
SELECT YEAR,
SUM(PRICE) PROD_10_SAL
FROM SALES
WHERE PRODUCT_ID = 10
)P
ON (T.YEAR = P.YEAR);
Most SQL developers write the above Sql query without even thinking that it can be solved in a single
query. The above query is rewritten as
SELECT YEAR,
ELSE NULL
END ) PROD_10_SAL,
SUM(SALES) TOT_SAL
FROM SALES
GROUP BY YEAR;
Now you can see the difference, just by reading the sales table one time we will able to solve
the problem.
First take a look at of your query, identify the redundant logic and then tune it.
Some times you can rewrite a LEFT OUTER JOIN by using NOT EXISTS or NOT IN and vice versa. As
an example, I want to find the products which do not sold in the year 2011.
SELECT P.PRODUCT_ID,
P.PRODUCT_NAME
FROM PRODUCTS P
SALES S
ON (P.PRODUCT_ID = S.PRODUCT_ID)
WHERE S.SALE_ID IS NULL;
The same query can be rewritten using NOT EXISTS and NOT IN as
SELECT P.PRODUCT_ID,
P.PRODUCT_NAME
FROM PRODUCTS P
SELECT 1
FROM SALES S
SELECT P.PRODUCT_ID,
P.PRODUCT_NAME
FROM PRODUCTS P
SELECT PRODUCT_ID
FROM SALES
);
Analyze the performance of these three queries and use the appropriate one.
Note: Be careful while using the NOT IN. If the sub query returns at lease row with NULL data, then the
main query won't return a row at all.
As similar to LEFT OUTER JOIN, the INNER JOINS can also be implemented with the EXISTS or
IN operators. As an example, we will find the sales of products whose product ids exists in the
products table.
SELECT S.PRODUCT_ID,
SUM(PRICE)
FROM SALES S
JOIN
PRODUCTS P
ON (S.PRODUCT_ID = P.PRODUCT_ID)
GROUP BY S.PRODUCT_ID;
As we are not selecting any columns from the products table, we can rewrite the same query with the
help of EXISTS or IN operator.
SELECT S.PRODUCT_ID,
SUM(PRICE)
FROM SALES S
WHERE EXISTS
SELECT 1
FROM PRODUCTS P
GROUP BY S.PRODUCT_ID;
SELECT S.PRODUCT_ID,
SUM(PRICE)
FROM SALES S
WHERE PRODUCT_ID IN
(
SELECT PRODUCT_ID
FROM PRODUCTS P
);
GROUP BY S.PRODUCT_ID;
We will see a simple join between the SALES and PRODUCTS table.
SELECT S.SALE_ID,
S.PRODUCT_ID,
P.PRODUCT_NAME
FROM SALES S
JOIN
PRODUCTS P
ON (S.PRODUCT_ID = P.PRODUCT_ID)
SELECT S.SALE_ID,
S.PRODUCT_ID,
(SELECT PRODUCT_NAME
FROM PRODUCTS P
FROM SALES S
Analyze these two queries with the explain plan and check which one gives better performance.
Try to avoid writing complex Sql queries. Split the queries and store the data in temporary tables or use
the Oracle With Clause for temporary storage. This will improve the performance. You can also use the
temporary tables or with clause when you want to reuse the same query more than once. This saves
the time and increases the performance.
Create the required indexes. In the mean time avoid creating too many indexes on a table.
Use the explain plan, auto trace to know about the query execution.
Ask the DBA to watch the query and gather stats like CPU usage, number of row read etc.
8. How to replace the n-th line in a file with a new line in Unix?
sed -i'' '10 d' filename # d stands for delete
sed -i'' '10 i new inserted line' filename # i stands for insert
11. How will you find which operating system your system is running on in UNIX?
uname -a
2. Write a command to search for the file 'map' in the current directory?
find -name map -type f
4. Write a command to remove the first number on all lines that start with "@"?
sed '\,^@, s/[0-9][0-9]*//' < filename
5. How to print the file names in a directory that has the word "term"?
grep -l term *
The '-l' option make the grep command to print only the filename without printing the content of the file.
As soon as the grep command finds the pattern in a file, it prints the pattern and stops searching other
lines in the file.
7. How do you display the calendar for the month march in the year 1985?
The cal command can be used to display the current month calendar. You can pass the month and year
as arguments to display the required year, month combination calendar.
cal 03 1985
This will display the calendar for the March month and year 1985.
Vmstat: reports on virtual memory statistics for processes, disk, tape and CPU activity.
3. Write a command to find the sum of bytes (size of file) of all files in a directory.
ls -l | grep '^-'| awk 'BEGIN {sum=0} {sum = sum + $5} END {print sum}'
4. Write a command to print the lines which end with the word "end"?
grep 'end$' filename
The '$' symbol specifies the grep command to search for the pattern at the end of the line.
5. Write a command to select only those lines containing "july" as a whole word?
grep -w july filename
The '-w' option makes the grep command to search for exact whole words. If the specified pattern is
found in a string, then it is not considered as a whole word. For example: In the string "mikejulymak",
the pattern "july" is found. However "july" is not a whole word in that string.
9. Write a command to list the files in '/usr' directory that start with 'ch' and then display the number of
lines in each file?
wc -l /usr/ch*
Another way is
find /usr -name 'ch*' -type f -exec wc -l {} \;
2. Write a command to display all the files recursively with path under current directory?
find . -depth -print
3. Display zero byte size files in the current directory?
find -size 0 -type f
4. Write a command to display the third and fifth character from each line of a file?
cut -c 3,5 filename
5. Write a command to print the fields from 10th to the end of the line. The fields in the line are delimited
by a comma?
cut -d',' -f10- filename
6. How to replace the word "Gun" with "Pen" in the first 100 lines of a file?
sed '1,00 s/Gun/Pen/' < filename
7. Write a Unix command to display the lines in a file that do not contain the word "RAM"?
grep -v RAM filename
The '-v' option tells the grep to print the lines that do not contain the specified pattern.
10. How to find out the usage of the CPU by the processes?
The top utility can be used to display the CPU usage by the processes.
3. How to replace the second occurrence of the word "bat" with "ball" in a file?
sed 's/bat/ball/2' < filename
4. How to remove all the occurrences of the word "jhon" except the first one in a line with in the entire
file?
sed 's/jhon//2g' < filename
5. How to replace the word "lite" with "light" from 100th line to last line in a file?
sed '100,$ s/lite/light/' < filename
6. How to list the files that are accessed 5 days ago in the current directory?
find -atime 5 -type f
7. How to list the files that were modified 5 days ago in the current directory?
find -mtime 5 -type f
8. How to list the files whose status is changed 5 days ago in the current directory?
find -ctime 5 -type f
2. Write a command to display the first 10 characters from each line of a file?
cut -c -10 filename
3. The fields in each line are delimited by comma. Write a command to display third field from each line
of a file?
cut -d',' -f2 filename
4. Write a command to print the fields from 10 to 20 from each line of a file?
cut -d',' -f10-20 filename
6. By default the cut command displays the entire line if there is no delimiter in it. Which cut option is
used to supress these kind of lines?
The -s option is used to supress the lines that do not contain the delimiter.
8. Write a command to replace the word "bad" with "good" globally in a file?
sed s/bad/good/g < filename
9. Write a command to replace the word "apple" with "(apple)" in a file?
sed s/apple/(&)/ < filename
10. Write a command to switch the two consecutive words "apple" and "mango" in a file?
sed 's/\(apple\) \(mango\)/\2 \1/' < filename
11. Write a command to display the characters from 10 to 20 from each line of a file?
cut -c 10-20 filename
1. Write a command to print the lines that has the the pattern "july" in all the files in a particular
directory?
grep july *
This will print all the lines in all files that contain the word july along with the file name. If any of the
files contain words like "JULY" or "July", the above command would not print those lines.
2. Write a command to print the lines that has the word "july" in all the files in a directory and also
suppress the filename in the output.
grep -h july *
3. Write a command to print the lines that has the word "july" while ignoring the case.
grep -i july *
The option i make the grep command to treat the pattern as case insensitive.
4. When you use a single file as input to the grep command to search for a pattern, it won't print the
filename in the output. Now write a grep command to print the filename in the output without using the '-
H' option.
grep pattern filename /dev/null
The /dev/null or null device is special file that discards the data written to it. So, the /dev/null is always
an empty file.
Another way to print the filename is using the '-H' option. The grep command for this is
grep -H pattern filename
5. Write a command to print the file names in a directory that does not contain the word "july"?
grep -L july *
The '-L' option makes the grep command to print the filenames that do not contain the specified pattern.
6. Write a command to print the line numbers along with the line that has the word "july"?
grep -n july filename
The '-n' option is used to print the line numbers in a file. The line numbers start from 1
7. Write a command to print the lines that starts with the word "start"?
grep '^start' filename
The '^' symbol specifies the grep command to search for the pattern at the start of the line.
8. In the text file, some lines are delimited by colon and some are delimited by space. Write a command
to print the third field of each line.
awk '{ if( $0 ~ /:/ ) { FS=":"; } else { FS =" "; } print $3 }' filename
9. Write a command to print the line number before each line?
awk '{print NR, $0}' filename
10. Write a command to print the second and third line of a file without using NR.
awk 'BEGIN {RS="";FS="\n"} {print $2,$3}' filename
11. How to create an alias for the complex command and remove the alias?
The alias utility is used to create the alias for a command. The below command createsalias for ps -aef
command.
alias pg='ps -aef'
If you use pg, it will work the same way as ps -aef.
To remove the alias simply use the unalias command as
unalias pg