Académique Documents
Professionnel Documents
Culture Documents
of the particular emp whose sal is <10000 with 2000RS & sal >10000 & <20000 with
4000RS and whose sal is >20000 with 5000RS now write a single update query.
UPDATE Emp
SET sal = sal +
CASE
WHEN sal>=1000 AND sal<2000 THEN 200
WHEN sal>=2000 AND sal<3000 THEN 300
END;
1. The following query retrieves "2" highest paid employees FROM each
Department :
2. Query that will display the total no. of employees, and of that total the
number who were hired in 1980, 1981, 1982, and 1983. Give appropriate
column headings.
SELECT a.deptno, ename, sal, (SELECT SUM(sal) FROM emp b WHERE a.deptno =
b.deptno)
FROM emp a
ORDER BY a.deptno;
OUTPUT :
=======
DEPTNO ENAME SAL SUM (SAL)
========= ======= ==== =========
10 KING 5000 11725
30 BLAKE 2850 10900
10 CLARK 2450 11725
10 JONES 2975 11725
30 MARTIN 1250 10900
30 ALLEN 1600 10900
30 TURNER 1500 10900
30 JAMES 950 10900
30 WARD 2750 10900
20 SMITH 8000 33000
20 SCOTT 3000 33000
20 MILLER 20000 33000
4. Create a matrix query to display the job, the salary for that job based on
department number, and the total salary for that job for all departments,
giving each column an appropriate heading.
SELECT job "Job", SUM (DECODE (deptno, 10, sal)) "Dept 10",
SUM (DECODE (deptno, 20, sal)) "Dept 20",
SUM (DECODE (deptno, 30, sal)) "Dept 30",
SUM (sal) "Total"
FROM emp
GROUP BY job ;
7. Tree Query :
9. Displaying EVERY 4th row in a table : (If a table has 14 rows, 4,8,12 rows
will be selected)
SELECT *
FROM emp
WHERE (ROWID,0) IN (SELECT ROWID, MOD(ROWNUM,4)
FROM emp);
10. Top N rows FROM a table : (Displays top 9 salaried people)
11. How does one count/sum RANGES of data values in a column? A value x
will be between values y and z if GREATEST(x, y) = LEAST(x, z).
SELECT
f2,
COUNT(DECODE(greatest(f1,59), least(f1,100), 1, 0)) "Range 60-100",
COUNT(DECODE(greatest(f1,30), least(f1, 59), 1, 0)) "Range 30-59",
COUNT(DECODE(greatest(f1,29), least(f1, 0), 1, 0)) "Range 00-29"
FROM my_table
GROUP BY f2;
SELECT num
FROM satyam
GROUP BY num
HAVING COUNT(*) > 1;
16. Query for getting the following output as many number of rows in the
table :
*
**
***
****
*****
SELECT RPAD(DECODE(temp,temp,'*'),ROWNUM,'*')
FROM srinu1;
FUNCTION F_BALANCE_VALUE
(p_business_group_id number, p_payroll_action_id number,
p_balance_name varchar2, p_dimension_name varchar2) RETURN NUMBER
IS
l_bal number;
l_defined_bal_id number;
l_assignment_action_id number;
BEGIN
SELECT assignment_action_id
INTO l_assignment_action_id
FROM
pay_assignment_actions
WHERE
assignment_id = :p_assignment_id
AND payroll_action_id = p_payroll_action_id;
SELECT
defined_balance_id
INTO
l_defined_bal_id
FROM
pay_balance_types pbt,
pay_defined_balances pdb,
pay_balance_dimensions pbd
WHERE
pbt.business_group_id = p_business_group_id
AND UPPER(pbt.balance_name) = UPPER(p_balance_name)
AND pbt.business_group_id = pdb.business_group_id
AND pbt.balance_type_id = pdb.balance_type_id
AND UPPER(pbd.dimension_name) = UPPER(p_dimension_name)
AND pdb.balance_dimension_id = pbd.balance_dimension_id;
l_bal := pay_balance_pkg.get_value(l_defined_bal_id,l_assignment_action_id);
RETURN (l_bal);
exception
WHEN no_data_found THEN
RETURN 0;
END;
FUNCTION f_element_value(
p_classification_name in varchar2,
p_element_name in varchar2,
p_business_group_id in number,
p_input_value_name in varchar2,
p_payroll_action_id in number,
p_assignment_id in number
)
RETURN number
IS
l_element_value number(14,2) default 0;
l_input_value_id pay_input_values_f.input_value_id%type;
l_element_type_id pay_element_types_f.element_type_id%type;
BEGIN
SELECT DISTINCT element_type_id
INTO l_element_type_id
FROM pay_element_types_f pet,
pay_element_classifications pec
WHERE pet.classification_id = pec.classification_id
AND upper(classification_name) = upper(p_classification_name)
AND upper(element_name) = upper(p_element_name)
AND pet.business_group_id = p_business_group_id;
SELECT input_value_id
INTO l_input_value_id
FROM pay_input_values_f
WHERE upper(name) = upper(p_input_value_name)
AND element_type_id = l_element_type_id;
SELECT NVL(prrv.result_value,0)
INTO l_element_value
FROM pay_run_result_values prrv,
pay_run_results prr,
pay_assignment_actions paa
WHERE prrv.run_result_id = prr.run_result_id
AND prr.assignment_ACTION_ID = paa.assignment_action_id
AND paa.assignment_id = p_assignment_id
AND input_value_id = l_input_value_id
AND paa.payroll_action_id = p_payroll_action_id;
RETURN (l_element_value);
exception
WHEN no_data_found THEN
RETURN 0;
END;
SELECT ename,
NVL(LENGTH(REPLACE(TRANSLATE(UPPER(RTRIM(ename)),'ABCDEFGHIJKLMNO
PQRSTUVWXYZ'' ',' @'),' ',''))+1,1) word_length
FROM emp;
Explanation :
TRANSLATE(UPPER(RTRIM(ename)),'ABCDEFGHIJKLMNOPQRSTUVWXYZ''
',' @') -- This will translate all the characters FROM A-
Z including a single quote to a space. It will also translate a space to a @.
REPLACE(TRANSLATE(UPPER(RTRIM(ename)),'ABCDEFGHIJKLMNOPQRSTUVWX
YZ'' ',' @'),' ','') -- This will replace every space with nothing in the
above result.
LENGTH(REPLACE(TRANSLATE(UPPER(RTRIM(ename)),'ABCDEFGHIJKLMNOPQR
STUVWXYZ'' ',' @'),' ',''))+1 -- This will give u the count
of @ characters in the above result.
WHERE <Condition>
UNION ALL returns all rows selected by either query, including all duplicates.
Q. What is a join?
Join is a process of retrieve pieces of data from different sets (tables) and returns them to the user or
program as one joined collection of data.
Q. List all the possible values that can be stored in a BOOLEAN data field.
There are only two values that can be stored in a BOOLEAN data field: -1(true) and 0(false).
Q. What is the highest value that can be stored in a BYTE data field?
A. The highest value that can be stored in a BYTE field is 255. or from -128 to 127. Byte is a set of
Bits that represent a single character. Usually there are 8 Bits in a Byte, sometimes more, depending
on how the measurement is being made. Each Char requires one byte of memory and can have a
value from 0 to 255 (or 0 to 11111111 in binary).
Q. What is Normalization?
A. The process of table design is called normalization.
Q. What is Trigger?
A. Trigger will execute a block of procedural code against the database when a table event occurs.
A2. A trigger defines a set of actions that are performed in response to an insert, update, or delete
operation on a specified table. When such an SQL operation is executed, in this case the trigger has
been activated.
Q. Write a SQL SELECT query that only returns each city only once from Students table? Do you
need to order this list with an ORDER BY clause?
A. SELECT DISTINCT City FROM Students;
Q. Write SQL SELECT query that returns the first and last name of each instructor, the Salary, and
gives each of them a number.
A. SELECT FirstName, LastName, Salary, ROWNUM FROM Instructors;
Q. Is the WHERE clause must appear always before the GROUP BY clause in SQL SELECT ?
A. Yes. The proper order for SQL SELECT clauses is: SELECT, FROM, WHERE, GROUP BY,
HAVING, ORDER BY. Only the SELECT and FROM clause are mandatory.
2. Semi-Additive Facts: A Fact which can be summed up to a few dimensions and not for
all dimensions available in the fact table.
3. Non-Additive Fact: A Fact which cannot be summed up for any of the dimensions
available in the fact table.
2. Snapshot Fact Table: This type of fact table deals with the particular period of time.
They contain non-additive and semi-additive facts.
==================================================================================