Find Order ID of Orders with Total Greater than Average in DB2

Mandalika
Updated on 30-Nov-2020 09:32:42

217 Views

We can find all the ORDER_ID which are having ORDER_TOTAL greater than the average value of all the ORDER_TOTAL present in the ORDERS table using the sub-query.For example, if we have below ORDERS table.ORDER_IDORDER_TOTALA223451867A629985634A569027615A5691187960A56915132A5691880363Below is the subquery to find out the desired data.ExampleSELECT ORDER_ID, ORDER_TOTAL FROM ORDERS    WHERE ORDER_TOTAL > (SELECT AVG(ORDER_TOTAL) FROM ORDERS)The result of the above query will be as below.ORDER_IDORDER_TOTALA2234587960A6299880363

Behaviour of a COBOL DB2 Program When Number of Locks Exceed the Limit

Mandalika
Updated on 30-Nov-2020 09:31:38

1K+ Views

Problem: How will the COBOL-DB2 program behave once the number of locks placed on the table space exceeds the defined limit?SolutionThe number of locks which an application can place on a DB2 resource such as page, table row, etc., is defined in DSNZPARM. Once the number of page and row level locks in any table exceeds the permissible limit, then the lock escalation takes place.In lock escalation, DB2 releases the page or row level lock which it has held and attempts to acquire a tablespace level or higher lock. In this case, the application now has wider access/scope to DB2 ... Read More

Find Access Path Selected by Optimizer for a Query

Mandalika
Updated on 30-Nov-2020 09:30:38

320 Views

DB2 optimizer plays an important role in the overall performance of the database. The optimizer selects the optimal access path for each query through which data can be fetched from the database. It identifies the indexes to follow, query predicates, etc.The optimizer selects the access path automatically and we can easily find the access path using EXPLAIN DB2 command. We have to SET the query number first and then give our SQL query to find out its access path in three simple steps.For example, We have a DB2 ORDERS table and we want to examine the SELECT query which has ... Read More

Purpose and Usage of ROWID and SEQUENCE in DB2

Mandalika
Updated on 30-Nov-2020 09:29:16

1K+ Views

Problem: How can you implement a logic to automatically generate a unique value in a DB2 column for every new row inserted?SolutionWe can implement a logic in a DB2 table through which we can have one column which will have an automatically generated value for every new row inserted. This column can serve as a primary key and hence it is very useful for the random access of the DB2 table. This logic can be implemented via ROW-ID and SEQUENCE.Any one column of the DB2 table can be defined as type ROW-ID following which DB2 will automatically assign a new ... Read More

Purpose and Usage of Subqueries in DB

Mandalika
Updated on 30-Nov-2020 09:27:33

208 Views

Problem: What are DB2 subqueries and what is the use of subqueries? Explain subqueries with the help of an example on the ORDERS table.SolutionA subquery in DB2 is a query within a query, i.e., nested query. A subquery is executed first followed by its parent query. We can have a subquery up to 15 levels.For example, if we have below ORDERS table in DB2.ORDER_IDORDER_TOTALZ223451267Z629986734Z569028815Z5691178990Z56915432Z5691877453If we want to extract all the orders from the ORDERS table which have ORDER_TOTAL more than overall average, then we can use the below sub query.ExampleSELECT ORDER_ID FROM ORDERS WHERE ORDER_TOTAL > (SELECT AVG(ORDER_TOTAL) FROM ORDERS)The ... Read More

Limitations of Using Outer Join on a DB2 Table

Mandalika
Updated on 30-Nov-2020 09:26:31

519 Views

The result of OUTER JOIN includes matched and unmatched rows in the WHERE clause. There are two main limitations of OUTER JOINS in DB2.The WHERE clause of OUTER JOIN can only have ‘=’ relational operator. , etc are not allowed in case of OUTER JOIN of two or more tables. Also two or more conditions in WHERE clause can only be used with AND logical operator, other logical operators such as OR, NOT is not allowed.The functions to handle NULL operators such as VALUE and COALESCE could not be used with the OUTER JOINS.For example, if we have below 2 ... Read More

Provide and Remove User Access to DB2 Objects

Mandalika
Updated on 30-Nov-2020 09:25:20

772 Views

DB2 has a concept of DCL, through which we can control the access to DB2 objects like table, plan, etc. DCL stands for Data Control Language and using this we can provide and remove user access to the DB2 objects.GRANT command will give the user access to the mentioned object and REVOKE command will remove the user access.For example, if we have to provide SELECT and UPDATE access on the ORDERS table to user REL123X then we will fire below command.GRANT SELECT, UPDATE ON ORDERS TO REL123XIf we want to provide INSERT access, then we will use the below command.GRANT ... Read More

Keep Locks on Resources After a Rollback

Mandalika
Updated on 30-Nov-2020 09:23:42

294 Views

DB2 places locks on the resources like table, tablespace, etc., when any application is using that resource. In a COBOL-DB2 program, if we are modifying or deleting data from a table then DB2 places locks. When we give the ROLLBACK statement to revert the changes made in the database, all the locks held by the DB2 are released by default.If we want to keep a lock on resources even after rollback, then we have to use ON ROLLBACK RETAIN LOCKS instead of ROLLBACK.For example, consider a cursor ORDER_CUR which is declared with FOR UPDATE OF clause. Therefore, as soon as ... Read More

Purpose and Usage of SAVEPOINT in COBOL DB2 Program

Mandalika
Updated on 30-Nov-2020 09:22:26

3K+ Views

Problem: How to use SAVEPOINT in a DB2? What is the purpose of SAVE-POINT in DB2? Explain with the help of an example.SolutionThe SAVEPOINT is used as a marker or tag to save the changes without committing in the database. After making the changes in the database, we can give a named SAVEPOINT statement and after that at any point of time we can rollback the changes to this savepoint using ROLLBACK statement.Practically, we can have multiple SAVEPOINTS in our COBOL-DB2 program and we can jump back to any of these SAVEPOINTS using ROLLBACK. This will revert all changes made ... Read More

Stored Procedure in DB2: Creation Guide

Mandalika
Updated on 30-Nov-2020 09:19:19

7K+ Views

The DB2 STORED PROCEDURE are the programs which are directly managed by DBMS. The STORED PROCEDURE generally contains SQLs and they can be called by application programs. The STORED PROCEDURE processes the query and returns the result to the application program. The STORED PROCEDURES can be used for the SQLs which are very often used, so instead of using the same SQL query again and again, we can simply use STORED PROCEDURE.The other benefit of STORED PROCEDURE is that they are fast and have good performance as compared to static SQLs used in application programs. The STORED PROCEDURE can be ... Read More

Advertisements