DB2 Interview Questions and Answers : Part 5

11/17/2009 No Comment

Collection of frequently asked IBM DB2 interview questions.

Write the SQL to find total number of rows in a DB2 table?
Answer : The SQL to find total no of rows -  SELECT COUNT(*) ... in db2 query

Write the SQL to to eliminate duplicate values in DB2 SELECT?
Answer : The SQL to eliminate duplicate values SELECT DISTINCT ... in db2 query

How can you select a particular row using indexes in DB2?
Answer : Specify the indexed columns in the WHERE clause of db2 query.

Can you tell me how do you find the maximum value in a column in db2?

Answer : Use SELECT MAX(...) .. in db2 query

Write the SQL to find the first 5 characters of FIRSTNAME column of DB2 table EMP ?

What do you mean by aggregate functions?

Answer : Bulit-in mathematical functions for use in SELECT clause.

Can MAX be used on a CHAR column?

Answer : Yes, MAX can be used.

Why the SQL statement SELECT AVG(SALARY) FROM EMP yields inaccurate results
Answer : Because SALARY is not declared to have NULLs and the employees for whom the salary is not known are also counted.

Write the SQL to concatenate the FIRSTNAME and LASTNAME from EMP table to give a complete name?


Explain the usage of VALUE function?Answer : 

1. Avoid -ve SQLCODEs by handling nulls and zeroes in computations
2. Substitute a numeric value for any nulls used in computation

What is the use of UNION and UNION ALL?

Answer : UNION : eliminates duplicates
UNION ALL: retains duplicates
Both these are used to combine the results of different SELECT statements.

If you have five SQL SELECT statements connected by UNION/UNION ALL, how many times you should specify UNION to eliminate the duplicate rows?
Answer : You need to specify only Once.

Tell me what is the restriction on using UNION in embedded SQL?

Answer : It has to be in a CURSOR.

In the SQL WHERE clause what is BETWEEN and IN?

Answer : BETWEEN supplies a range of values while IN supplies a list of values.

Is BETWEEN inclusive of the range values specified?
Answer : Yes.

Can you tell me what is 'LIKE' used for in WHERE clause? What are wildcard characters?

Answer : LIKE is used for partial string matches. ‘%’ ( for a string of any character ) and ‘_’ (for any single character ) are the two wild card characters.

Explain the usage of LIKE statement?

Answer : To do partial search for instance - to search employee by name, you need not specify the complete name; using LIKE, you can search for partial string matches.

In SQL what is the use of underscore ( ‘_’ ) in the LIKE statement?

Answer : Match for any single character.

What is the use of GROUP BY ... HAVING clause? 

Answer : GROUP BY partitions the selected rows on the distinct values of the column on which you group by.HAVING selects GROUPs which match the criteria specified

An employee table has the column PROJECT null. Explain how can you get a list of employees who are not assigned to any project?

Answer : 

What is the result of this query if no rows are selected:

Answer : NULL

Why you should not use SELECT * in embedded SQL programs?

Answer : For three reasons as cited below
If the table structure is changed ( a field is added ), the program will have to be modified
Program might retrieve the columns which it might not use, leading on I/O over head.
The chance of an index only scan is lost.

What do you mean by correlated subqueries? Explain the issues related with correlated subqueries?
Answer : A subquery in which the inner ( nested ) query refers back to the table in the outer query. Correlated subqueries must be evaluated for each qualified row of the outer query that is referred to.

What do you mean by a cursor? Where should it be used? 

Answer : Cursor is a programming device that allows the SELECT to find a set of rows but return them one at a time.Cursor should be used because the host language can deal with only one row at a time.

How can you retrieve rows from a DB2 table in embedded SQL?

Answer : Either by using the single row SELECT statements, or by using the CURSOR. 

What are the other ways available to you to retrieve a row from a table in embedded SQL other than cursor? 
Answer : Single row SELECTs.

Explain how can you specify and use a cursor in a COBOL program?

Answer : Use DECLARE CURSOR statement either in working storage or in procedure division (before open cursor), to specify the SELECT statement. Then use OPEN, FETCH rows in a loop and finally CLOSE.

Explain in brief hat will happen when you say OPEN CURSOR?

Answer : If there is an ORDER BY clause, rows are fetched, sorted and made available for the FETCH statement. Other wise simply the cursor is placed on the first row.

Can you tell me if DECLARE CURSOR is executable?

Answer : No.

Can you have more than one cursor open at any one time in a SQL program?

Answer : Yes.

When you do a SQL COMMIT,  will the cursor be closed?

Answer : Yes.

Explain how can you leave the cursor open after issuing a SQL COMMIT in DB2?

Answer : Use WITH HOLD option in DECLARE CURSOR statement. But, it has not effect in psuedo-conversational CICS programs.

Define a 
Answer : 
A VARCHAR column REMARKS would be defined as follows:...

Give the physical storage length of each of the following DB2 data types:

Answer : DATE: 4bytes, TIME: 3bytes, TIMESTAMP: 10bytes

Give the COBOL picture clause of the following DB2 data types:

Answer : 
TIME : PIC X(08)

What do you mean by DCLGEN ?

Answer : DeCLarations GENerator: used to create the host language copy books for the table definitions. Also creates the DECLARE table.

List the contents of a DCLGEN?

Answer : 
1. EXEC SQL DECLARE TABLE statement which gives the layout of the table/view in terms of DB2 datatypes.
2. A host language copy book that gives the host variable definitions for the column names.

Is the usage of 
DCLGEN mandatory? If not, why would you use it at all? 
Answer : It is not mandatory to use DCLGEN.Using DCLGEN, helps detect wrongly spelt column names etc. during the pre-compile stage itself ( because of the DECLARE TABLE ). DCLGEN being a tool, would generate accurate host variable definitions for the table reducing chances of error.

necessary in DCLGEN ? Can you tell me why it used?
Answer : It not necessary to have DECLARE TABLE statement in DCLGEN. This is used by the pre-compiler to validate the table-name, view-name, column name etc., during pre-compile.

Explain how is a typical DB2 batch program executed?
Answer : 
1. Use DSN utility to run a DB2 batch program from native TSO. An example is shown:
2. Use IKJEFT01 utility program to run the above DSN command in a JCL.
Assuming that a site’s standard is that pgm name = plan name, what is the easiest way to find out which pgms are affected by change in a table’s structure ?
Query the catalogue tables SYSPLANDEP and SYSPACKDEP.

Can you name some fields from SQLCA.

Answer : Some of the fields are SQLCODE, SQLERRM, SQLERRD

Tell me the way of quickly find out the # of rows updated after an update statement?

Answer : You have to check the value stored in SQLERRD(3).

Define EXPLAIN in DB2?
Answer : EXPLAIN is used to display the access path as determined by the optimizer for a SQL statement. It can be used in SPUFI (for single SQL statement) or in BIND step (for embedded SQL).

What are the pre-requisites before you do EXPLAIN?

Answer : Make sure that the PLAN_TABLE is created under the AUTHID.

Can you tell me where is the output of EXPLAIN stored? Answer : The output is stored in userid.PLAN_TABLE

What does it mean when the EXPLAIN has output with MATCHCOLS = 0?

Answer : A nonmatching index scan if ACCESSTYPE = I.

What is the usage of EXPLAIN of a dynamic SQL statement?

Answer : 
1. Use SPUFI or QMF to EXPLAIN the dynamic SQL statement
2. Include EXPLAIN command in the embedded dynamic SQL statements

What are the isolation levels possible?

Answer : The isolation level are CS: Cursor Stability and RR: Repeatable Read

Explain the difference between CS and RR isolation levels?

Answer : CS: Releases the lock on a page after use
RR: Retains all locks acquired till end of transaction

How and when do you specify the isolation level in DB2? 

Answer : During the BIND process. ISOLATION (CS/RR ).  ISOLATION LEVEL is a parameter for the bind process.
If you use CS and update a page, will the lock be released after you are done with that page?Answer : No, the lock will not be released.

Name the various locking levels available in DB2?

Can you tell how does DB2 database determine what lock-size to use?

Answer : 
1. Based on the lock-size given while creating the tablespace
2. Programmer can direct the DB2 what lock-size to use
3. If lock-size ANY is specified, DB2 usually chooses a lock-size of PAGE

What are the disadvantages of PAGE level lock?
Answer : There will ne high resource utilization if large updates are to be done
Related Posts

No comments :


Aired | The content is copyrighted and may not be reproduced on other websites. | Copyright © 2009-2016 | All Rights Reserved 2016

Contact Us | About Us | Privacy Policy and Disclaimer