Tuesday, February 17, 2015

Welcome to the fabulous Oracle Answers !

You're one CTRL+F away from your answer.

Enjoy !



CASE AND CARACTER MANIPULATION

-Identify the output from the following SQL statement:
SELECT RPAD('SQL',6, '*')
FROM DUAL;
SQL***

-Which query selects the first names of the DJ On Demand clients who have a first name beginning with "A"? 
SELECT UPPER(first_name)
FROM d_clients
WHERE LOWER(first_name) LIKE 'a%'

-Which character manipulation function always returns a numerical value?
LENGTH

-What does the following SQL SELECT statement return?
SELECT UPPER( SUBSTR('Database Programming', INSTR('Database Programming','P'),20))
FROM dual;
PROGRAMMING

-Which of the following SQL statements would correctly return a song title identified in the database as "All Thesears"?      
WHERE title LIKE INITCAP('%all these years');

-Which of the following are types of SQL functions?
Multi-Row Functions
Single-Row Functions

-Character functions accept character arguments and only return character values. True or False?
False
        
-Which query would return a user password combining the ID of an employee and the first 4 characters of the last name?
SELECT CONCAT (employee_id, SUBSTR(last_name,1,4))
AS "User Passwords"
FROM employees


NUMBER FUNCTIONS

-The answer to the following script is 456. True or False?
SELECT TRUNC(ROUND(456.98))
FROM dual 
False
           
-Which number function may be used to determine if a value is odd or even?
MOD
            
-What is the result of the following SQL Statement:
SELECT ROUND(45.923,-1)
FROM DUAL;
50
           
-ROUND and TRUNC functions can be used with which of the following Datatypes?
Dates and numbers           

DATE FUNCTIONS

-What is the result of the following query?
SELECT ADD_YEARS ('11-JAN-94',6)
FROM dual;
This in not a valid SQL statement.
           
-What function would you use to return the highest date in a month?
LAST_DAY
           
-Which query would return a whole number if the sysdate is 26-MAY-04? 
SELECT TRUNC(MONTHS_BETWEEN(SYSDATE,'19-MAR-79') /12)
AS YEARS
FROM DUAL;
           
-If hire_date has a value of '03-July-03', then what is the output from this code
SELECT ROUND(hire_date, 'Year') FROM employees;
01-JAN-04
           
-Round and Trunc can be used on Date datatypes. True or False?
True
          
-What is the result of the following query?
SELECT ADD_MONTHS ('11-JAN-94',6)
FROM dual;
11-JUL-1994

CONVERSION FUNCTIONS

-You need to display the HIRE_DATE values in this format: 25th of July 2002. Which SELECT statement would you use?
SELECT TO_CHAR(hire_date, 'ddth "of" Month YYYY')
FROM employees;          

-Which statement is true about SQL functions?
a, b and c are true.
           
-A table has the following definition:
EMPLOYEES(
EMPLOYEE_ID NUMBER(6) NOT NULL,
NAME VARCHAR2(10) NOT NULL,
MANAGER_ID VARCHAR2(6))
and contains the following rows:
(1001, 'Bob Bevan', '200')
(200, 'Natacha Hansen', null)
Will the following query work?
SELECT *
FROM employees
WHERE employee_id = manager_id;

Yes, Oracle will perform implicit datatype conversion.
           
-Which statement will return the salary of e.g. 6000 from the Employees table in the following format $6000.00? 
SELECT TO_CHAR(salary, '$99999.00') SALARY
FROM employees          

-The following script will run successfully. True or False?
SELECT TO_CHAR(TO_DATE("25-DEC-04" ,'dd-MON-yy'))
FROM dual 
False
           
-Sysdate is 12-MAY-2004.
You need to store the following date: 7-DEC-89
Which statement about the date format for this value is true?
The RR date format will interpret the year as 1989, and the YY date format will interpret the year as 2089


NULL FUNCTIONS

-The following statement returns 0 (zero). True or False?
SELECT 121/NULL
FROM dual; 
False           

-Which function compares two expressions?
NULLIF           

-If quantity is a number datatype, what is the result of this statement?
SELECT NVL(200/quantity, 'zero') FROM inventory;
The statement fails       

-With the following data in Employees (last_name, commission_pct, manager_id) what is the result of the following statement?
DATA:
King,null,null
Kochhar, null,100
Vargas, null, 124
Zlotkey,.2, 100
SELECT last_name, NVL2(commission_pct, manager_id, -1) comm
FROM employees ; 
King, -1
Kochhar, -1
Vargas, -1
Zlotkey, 100           

-Consider the following data in Employees table: (last_name, commission_pct, manager_id)
DATA:
King,null,null
Kochhar, null,100
Vargas, null, 124
Zlotkey,.2, 100
What is the result of the following statement:
SELECT last_name, COALESCE(commission_pct, manager_id, -1) comm
FROM employees ; 
King, -1
Kochhar, 100
Vargas, 124
Zlotkey, .2


CONDITIONAL EXPRESSIONS


-CASE and DECODE evaluate expressions in a similar way to IF-THEN-ELSE logic. However, DECODE is specific to Oracle syntax. True or False?
True
           
-Which statement will return a listing of last names, salaries and a rating of 'Low', 'Medium', 'Good' or 'Excellent' depending on the salary value?
SELECT last_name,salary,
(CASE WHEN salary<5000 THEN 'Low'
     WHEN salary<10000 THEN 'Medium'
     WHEN salary<20000 THEN 'Good'
     ELSE 'Excellent'
END) qualified_salary
FROM employees;

-For the given data from Employees (last_name, manager_id) what is the result of the following statement:
DATA:( King, null
Kochhar, 100
De Haan, 100
Hunold, 102
Ernst, 103)
SELECT last_name,
DECODE(manager_id, 100, 'King', 'A N Other') "Works For?"
FROM employees
King, A N Other
Kochhar, King
De Haan, King
Hunold, A N Other
Ernst, A N Other

-Which of the following is a conditional expression used in SQL? 
CASE

CROSS JOINS AND NATURAL JOINS


-A NATURAL JOIN is based on:
Columns with the same name and datatype
           
-The ___________ join is the ANSI-standard syntax used to generate a Cartesian product.
CROSS
           

-The join column must be included in the select statement when you use the NATURAL JOIN clause. True or False?
False
           
-What happens when you create a Cartesian product?
All rows from one table are joined to all rows of another table




JOIN CLAUSES


-The following is a valid SQL statement.
SELECT e.employee_id, e.last_name, d.location_id, department_id
FROM employees e JOIN departments d
USING (department_id) ;
True or False?
True
         
-The primary advantage of using JOIN ON is:
It permits columns with different names to be joined
           
-Table aliases MUST be used with columns referenced in the JOIN USING clause. True or False?         
False       

-You can do nonequi-joins with ANSI-Syntax. True or False?
True

-The keywords JOIN _____________ should be used to join tables with the same column names but different datatypes.
USING


INNER VERSUS OUTER JOINS

-What is another name for a simple join or an inner join?
Equijoin
           
-Given the following descriptions of the employees and jobs tables, which of the following scripts will display each employee's possible minimum and maximum salaries based on their job title?
EMPLOYEES Table:
Name  Null?   Type
EMPLOYEE_ID       NOT NULL   NUMBER (6)
FIRST_NAME                      VARCHAR2 (20)
LAST_NAME           NOT NULL   VARCHAR2 (25)
EMAIL           NOT NULL   VARCHAR2 (25)
PHONE_NUMBER              VARCHAR2 (20)
HIRE_DATE NOT NULL   DATE
JOB_ID          NOT NULL   VARCHAR2 (10)
SALARY                   NUMBER (8,2)
COMMISSION_PCT                       NUMBER (2,2)
MANAGER_ID                    NUMBER (6)
DEPARTMENT_ID              NUMBER (4)
JOBS Table:
Name  Null?   Type
JOB_ID          NOT NULL   VARCHAR2 (10)
JOB_TITLE   NOT NULL   VARCHAR2 (35)
MIN_SALARY                     NUMBER (6)
MAX_SALARY                   NUMBER (6)
SELECT first_name, last_name, job_id, min_salary, max_salary
FROM employees
NATURAL JOIN jobs;           

-The following statement is an example of what kind of join?
SELECT car.vehicle_id, driver.name
FROM car
LEFT OUTER JOIN driver ON (driver_id) ;
Outer Join
           
-Which syntax would be used to retrieve all rows in both the EMPLOYEES and DEPARTMENTS tables, even when there is no match?
FULL OUTER JOIN

-For which of the following tables will all the values be retrieved even if there is no match in the other?
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id);
employees
           
-EMPLOYEES Table:
Name  Null?   Type
EMPLOYEE_ID       NOT NULL   NUMBER(6)
FIRST_NAME                      VARCHAR2(20)
LAST_NAME           NOT NULL   VARCHAR2(25)
DEPARTMENT_ID              NUMBER (4)
DEPARTMENTS Table:
Name  Null?   Type
DEPARTMENT_ID  NOT NULL   NUMBER 4
DEPARTMENT_NAME      NOT NULL   VARCHAR2(30)
MANAGER_ID                    NUMBER (6)
A query is needed to display each department and its manager name from the above tables. However, not all departments have a manager but we want departments returned in all cases. Which of the following SQL: 1999 syntax scripts will accomplish the task?
SELECT d.department_id, e.first_name, e.last_name
FROM employees e
RIGHT OUTER JOIN departments d
ON (e.employee_id = d.manager_id);

-If you select rows from two tables (employees and departments) using an outer join, what will you get? Use the code below to arrive at your answer:
SELECT e.last_name, e.department_id, d.department_name
FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id);
All employees including those that do not have a departement_id assigned to them


SELF JOINS VERSUS OUTER JOINS

-Which of the following database design concepts is implemented with a self join?
Recursive Relationship
           
-Hierarchical queries can walk both Top-Down and Bottom-Up. True or False?
True       
-Which select statement will return the last name and hire date of an employee and his/ her manager for employees that started in the company before their managers?
SELECT w.last_name, w.hire_date, m.last_name, m.hire_date
FROM employees w , employees m
WHERE w.manager_id = m.employee_id
AND w.hire_date < m.hire_date
-Hierarchical queries MUST use the LEVEL pseudo column. True or False?
False
           
-Which SELECT statement implements a self join?
SELECT e.employee_id, m.manager_id
FROM employees e, employees m
WHERE m.employee_id = e.manager_id;

-Which of the following database design concepts do you need in your tables to write Hierarchical queries?
Recursive Relationship

GROUP FUNCTIONS

-You can use GROUP functions in all clauses of a SELECT statement. True or False?
False   


-What two group functions can be used with any datatype?
MIN, MAX
           
-Given the following data in the employees table (employee_id, salary, commission_pct)
DATA: (143, 2600, null
144, 2500, null
149, 10500, .2
174, 11000, .3
176, 8600, .2
178, 7000, .15)
What is the result of the following statement:
SELECT AVG(commission_pct)
FROM employees
WHERE employee_id IN( 143,144,149,174,176,178)
0.2125
           
-The following statement will work because it uses the same column with different GROUP functions:
SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary)
FROM employees;
True or False?
True
           
-Given the following data in the employees table (employee_id, salary, commission_pct)
DATA: (143, 2600, null
144, 2500, null
149, 10500, .2
174, 11000, .3
176, 8600, .2
178, 7000, .15)
What is the result of the following statement:
SELECT SUM(commission_pct), COUNT(salary)
FROM employees
WHERE employee_id IN( 143,144,149,174,176,178)
SUM = .85 and COUNT = 6


COUNT, DISTINCT, NVL

-What would the following SQL statement return?
SELECT COUNT(DISTINCT salary)
FROM employees;
The number of unique salaries in the employees table

-To include null values in the calculations of a group function, you must:
Convert the null to a value using the NVL( ) function
           
-What would the following SQL statement return?
SELECT COUNT(first_name)
FROM employees;
The total number of non-null first names in the employees table     

-Using your existing knowledge of the employees table, would the following two statements produce the same result?
SELECT COUNT(*)
FROM employees;
SELECT COUNT(commission_pct)
FROM employees;
No

-Given the following data in the employees table (employee_id, salary, commission_pct)
DATA: (143, 2600, null
144, 2500, null
149, 10500, .2
174, 11000, .3
176, 8600, .2
178, 7000, .15)
What is the result of the following statement:
SELECT SUM(commission_pct), COUNT(commission_pct)
FROM employees
WHERE employee_id IN( 143,144,149,174,176,178)
SUM = .85 and COUNT = 4

USING GROUP BY AND HAVING CLAUSES

-Is the following statement correct?
SELECT department_id, AVG(salary)
FROM employees;
No, because a GROUP BY department_id clause is needed
           
-Which of the following SQL statements could display the number of people with the same last name:       
SELECT last_name, COUNT(last_name)
FROM EMPLOYEES
GROUP BY last_name;

-How would you alter the following query to list only employees where there existed more than one per last_name with the same last name:
SELECT last_name, COUNT(employee_id)
FROM EMPLOYEES
GROUP BY last_name; 
SELECT last_name, COUNT(last_name)
FROM EMPLOYEES
GROUP BY last_name
HAVING COUNT(last_name) > 1;           

-Read the following SELECT statement. Choose the column or columns that must be included in the GROUP BY clause.
SELECT COUNT(last_name), grade, gender
FROM STUDENTS
GROUP_BY ?????;  
grade, gender
           
-Is the following statement correct:
SELECT first_name, last_name, salary, department_id, COUNT(employee_id)
FROM employees
WHERE department_id = 50
GROUP BY last_name, first_name, department_id;
No, because the statement is missing salary in the GROUP BY clause


USING ROLLUP AND CUBE OPERATIONS, AND GROUPING SETS

-Examine the following statement:
SELECT department_id, manager_id, job_id, SUM(salary)
FROM employees
GROUP BY GROUPING SETS(.......);
Select the correct GROUP BY GROUPING SETS clause from the following list: 
GROUP BY GROUPING SETS ((department_id, manager_id), (department_id, job_id), (manager_id, job_id))
           
-Examine the following statement:
SELECT department_id, manager_id, job_id, SUM(salary)
FROM employees
GROUP BY ROLLUP(department_id, manager_id)
What extra data will this query generate?
The statement will fail.

-Examine the following statement:
SELECT department_id, manager_id, job_id, SUM(salary)
FROM employees
GROUP BY GROUPING SETS((department_id, manager_id), (department_id, job_id))
What data will this query generate? 
Sum of salaries for (department_id, job_id) and (department_id, manager_id)

-If you want to include subtotals and grand totals for all columns mentioned in a GROUP BY clause you should use which of the following extensions to the GROUP BY clause?
CUBE
           
            
USING SET OPERATORS

-MINUS will give you rows from the first query not present in the second query? (True or False)
True

-INTERSECT will give you the common rows found in both queries? (True or False)
True
           
-Which ones of the following are correct SET operators?
UNION, MINUS
UNION ALL, INTERSECTION

-The difference between UNION and UNION ALL is
UNION will remove duplicates, UNION ALL returns all rows from all queries

FUNDAMENTALS OF SUBQUERIES

-What will the following statement return:
SELECT last_name, salary
FROM employees
WHERE (department_id, job_id) = (SELECT department_id, job_id FROM employees WHERE employee_id = 103)
A list of last_names and salaries of employees that works in the same department and has the same job_id as that of employee 103.


-What will the following statement return:
SELECT employee_id, last_name
FROM employees
WHERE salary =(SELECT MIN(salary) FROM employees GROUP BY department_id);
Nothing. It is an invalid statement.     

-Which of the following statements is a true guideline for using subqueries?
The outer and inner queries can reference more than one table. They can get data from different tables.
           
-What will the following statement return:
SELECT last_name, salary
FROM employees
WHERE salary < (SELECT salary FROM employees WHERE employee_id = 103)
A list of last_names and salaries of employees that makes less than employee 103
           
-Examine the following statement:
SELECT last_name, salary
FROM employees
WHERE department_id = (SELECT department_id FROM employees WHERE employee_id = 103) AND job_id = (SELECT job_id FROM employees WHERE employee_id = 103)
Is this a pair-wise or non-pair-wise Subquery?
This is an example of a non-pair-wise subquery.
           
-Subqueries can only be placed in the WHERE clause. True or False?
False


SINGLE ROW SUBQUERIES

-If the subquery returns no rows will the outer query return any values?
No, because the subquery will be treated like a null value.
           
-Subqueries are limited to four per SQL transaction. True or False?
False
           
-In a non-correlated subquery, the outer query always executes prior to the inner query's execution. True or False?
False
           
-Single row subqueries may not include this operator:
ALL
           
-The result of this statement will be:
SELECT last_name, job_id, salary, department_id
FROM employees
WHERE job_id =(SELECT job_id FROM employees WHERE employee_id = 141) AND department_id = (SELECT department_id FROM departments WHERE location_id =1500)
Only the employees whose job id matches employee 141 and who work in location 1500


MULTIPLE-ROW SUBQUERIES

-In a subquery the ALL operator compares a value to every value returned by the inner query. True or False?      
True
           
-The SQL multiple-row subquery extends the capability of the single-row syntax through the use of what three comparison operators?
IN, ANY and ALL
           
-Multiple-row subqueries must have NOT, IN or ANY in the WHERE clause of the inner query. True or False?
False
           
-Group functions, such as HAVING and GROUP BY can be used in multiple-row subqueries. True or False?    
True
       
-When a multiple-row subquery uses the NOT IN (<>ALL) operator, if one of the values returned by the inner query is a null value, the entire query returns:
No rows returned
           
-Group functions can be used in subqueries even though they may return many rows. True or False?
True
           
-The salary column of the f_staffs table contains the following values: 4000 5050 6000 11000 23000
Which of the following statements will return the last_name and first_name of those employees who earn more than 5000.
SELECT last_name, first_name
FROM f_staffs
WHERE salary IN (SELECT salary FROM f_staffs WHERE salary > 5000);

-There can be more than one subquery returning information to the outer query. True or False?
True
           

CORRELATED SUBQUERIES

-The WITH-clause is a way of creating extra tables in the database? (True or False)
False 
           
-Correlated Subqueries must work on the same tables in both the inner and outer query?
False
          
-In a correlated subquery the outer and inner query are joined on one or more columns?
True
           
-Table aliases must be used when you are writing correlated subqueries?
True

INSERT STATEMENTS

-Insert statements can be combined with subqueries to create more than one row per statement. True or False?
True
           
-Which of the following statements will add a new customer to the customers table in the Global Fast Foods database?
INSERT INTO customers (id, first_name, last_name, address, city, state, zip, phone_number)
VALUES (145, 'Katie', 'Hernandez', '92 Chico Way', 'Los Angeles', 'CA', 98008, 8586667641);      

-If the employees table have 7 rows how many rows are inserted into the copy_emps table with the following statement:
INSERT INTO copy_emps (employee_id, first_name, last_name, salary, department_id)
SELECT employee_id, first_name, last_name, salary, department_id
FROM employees
7 rows, as there is no WHERE-clause on the subquery.
           
-To return a table summary on the customers table, which of the following is correct?
DESCRIBE customers, or DESC customers
         
-DML is an acronym that stands for:
Data Manipulation Language
           
-When inserting rows into a table all columns must be given values. True or False?
False
          
-When inserting a new row the null keyword can be included in the values list for any null column. True or False?  
True
           
-Is it possible to insert more than one row at a time using an INSERT statement with a VALUES clause?           
No, you can only create one row at a time when using the VALUES clause.

-What is the quickest way to use today's date when you are creating a new row?
Use the SYSDATE function.


UPDATING COLUMN VALUES AND DELETING ROWS

-If you are performing an UPDATE statement with a subquery, it MUST be a correlated subquery?
False
           
-To change an existing row in a table, you can use the UPDATE or INSERT statements. True or False?
False
           
-Assuming there are no Foreign Keys on the EMPLOYEES table, if the following subquery returns one row, how many rows will be deleted from the EMPLOYEES table?
DELETE FROM employees
WHERE department_id =
(SELECT department_id
FROM departments
WHERE department_name LIKE '%Public%');
All the rows in the EMPLOYEES table with department_ids matching the department_id returned by the subquery.
           
-Using your knowledge of the employees table, what would be the result of the following statement:
DELETE FROM employees;
All rows in the employees table will be deleted if there are no constraints on the table.
           
-Which of the following statements best describes what will happen to the student table in this SQL statement?
UPDATE students
SET lunch_number = (SELECT lunch_number FROM student WHERE student_id = 17)
WHERE student_id = 19;
The statement updates the student_table by replacing student id 19's lunch number with student id 17's lunch number.
           
-DELETE statements can use correlated subqueries?
True
           
-How many rows will be deleted from the employees table with the following statement?
DELETE FROM employees
WHERE last_name = 'king';
No rows will be deleted, as no employees match the WHERE-clause.
           
-If the subquery returns one row, how many rows will be deleted from the employees table?
DELETE FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name LIKE '%Public%');
All rows in the employees table which work in the given department will be deleted.
           
-Is the following statement valid, i.e. is it allowed to update rows in one table, based on a subquery from another table?
UPDATE copy_emp
SET department_id = (SELECT department_id FROM employees WHERE employee_id = 100)
WHERE job_id = (SELECT job_id FROM employees WHERE employee_id = 200);
Yes, this is a perfectly valid statement.
           

DEFAULT VALUES, MERGE, AND MULTI-TABLE INSERTS

-The MERGE function combines the:
INSERT and UPDATE commands
           
-A multi-table insert statement can insert into more than one table?
True
           
-In developing the Employees table, you create a column called hire_date. You assign the hire_date column a DATE datatype with a DEFAULT value of 0 (zero). A user can come back later and enter the correct hire_date. This is __________. 
A bad idea. The default value must match the DATE datatype of the column.
           
-Which statement below will not insert a row of data onto a table?
INSERT INTO (id, lname, fname, lunch_num)
VALUES (143354, 'Roberts', 'Cameron', 6543);
           
-If a default value was set for a null column, Oracle sets the column to the default value. However, if no default value was set when the column was created, Oracle inserts an empty space. True or False?
False
           
-The MERGE statement can be used to update rows in one table based on values in another table and if the update fails, then the rows will automatically be inserted instead. True or False?
True
           
-A multi-table insert statement must have a subquery at the end of the statement?
True
          
-The DEFAULT keyword can be used in the following statements:
INSERT and UPDATE