Beruflich Dokumente
Kultur Dokumente
NOTE: The answers go with their sequences. If a question was not answer, that means that it a repeating
question and the answer was given by the previous questions or it is not in the scope of this subject.
“Nothing in the world can take the place of persistence. Talent will not; nothing is
more common than unsuccessful men with talent. Genius will not; unrewarded
genius is almost a proverb. Educations will not the world is full of educated
derelicts. Persistence and determination alone are omnipotent. The slogan, 'Press
on,' has solved and always will solve the problems of the human race.”
Q: What is a Database?
A: A collection of all tables under a single or many different schemas can be stored and maintained in a
1
database. A database, in effect, is a collection of objects such as tables, indexes, stored procedures, etc.
Q: Query the employee names and their salaries from the employee table.
A: SQL> SELECT ename, sal FROM emp;
Q: Do the above query and use an “as” clause for the “salary” column aliases or column headings.
A: SQL> SELECT ename, sal AS salary FROM emp;
Q: Repeat the previous query and have “Full Name” for the ename’s column heading and “Salary” for the
“sal” column heading.
A: SQL> SELECT ename “Full Name”, sal "Salary"
FROM emp;
Q: Use the (NVL) the null value function to assign zero to any null value in the commission column for the
previous query.
A: SQL> SELECT ename, NVL(comm,0) commission
FROM emp;
Q: Concatenate the customers’ last name and first name separated by comma.
A: SQL> SELECT last_name || ', ' || first_name AS "full name"
FROM customers;
Q: Query the employees name whose names start with the letter “M.”
A: SQL> SELECT ename
FROM emp
WHERE ename LIKE 'M%';
Q: Query the employees name whose names end with the letter “R.”
A: SQL> SELECT ename
FROM emp
WHERE ename LIKE '%R';
Q: Query the employees name whose salaries between 2000 and 3000 dollars.
A: SQL> SELECT ename
2
FROM emp
WHERE sal BETWEEN 2000 AND 3000;
Q: Query the employees name and their department name using the “DECODE” function. If the
department number is 10 then print "accounting.” If the department number is 20 then print "research," or if
the department number is 30 then print "sales." Anything else prints others.
A: SQL> SELECT ename, DECODE (deptno, 10, 'Accounting',
20, 'Research',
30, 'Sales',
'Others') AS "Department"
FROM emp;
Q: Query all the employee names and their department including all the departments with no employees.
A: SQL> SELECT ename, dname
FROM emp e, dept d
WHERE e.deptno (+) = d.deptno;
Q: Query the managers’ name with their employees sorted by the manager name.
A: SQL> SELECT mgr.ename “Manager Name”, e.ename “Employee Name”
FROM emp mgr, emp e
WHERE mgr.empno = e.mgr
ORDER BY mgr.ename;
Q: Query the department number and their total, average, min, and max salaries for each department.
A: SQL> SELECT deptno, SUM(sal), AVG(sal), MIN(sal), MAX(sal)
FROM emp
GROUP BY deptno;
3
Q: Query the department no and their total salaries that have more than 5 employees working in their
department.
A: SQL> SELECT deptno, SUM(sal)
FROM emp
GROUP BY deptno
HAVING count(*) > 5;
Q: Query the employees name that work for the Research or Sales department (the department number 20
or 30).
A: SQL> SELECT ename, deptno
FROM emp
WHERE deptno IN (20, 30);
Q: Query the employees name that work in the "accounting" department. Assuming the department
number is unknown.
A: SQL> SELECT ename
FROM emp
WHERE deptno IN
(SELECT deptno
FROM dept
WHERE dname = "ACCOUNTING");
Q: Query the employees name and use the runtime variable to substitute the department number? Then run
it for following department no 10, 20, and 30.
A: SQL> SELECT ename
FROM emp
WHERE deptno = &deptno;
SQL> /
Q: Query the customer names which have more than four orders.
A: SQL> SELECT name
FROM customer c
WHERE exists
(SELECT 'T'
FROM ord
WHERE custid = c.custid
GROUP BY custid
HAVING count(*) > 4);
Q: Create a composite index on the employee table that contains two index columns (last name and first
name).
A: SQL> CREATE INDEX employee_lname_fname_ind_01
ON employee (lastname, firstname);
Q: Query the index tables that belong to the employee table and owns by the iself user.
A: SQL> SELECT index_name, uniqueness
FROM user_indexes
WHERE table_name = 'EMPLOYEE';
Q: Change the size of the "column_name" to 30 characters logically (for display only).
A: SQL> COLUMN column_name FORMAT a30
Q: Change the "last_name" column value from “Smith” to “Judd” where the "employee id" is 100.
A: SQL> UPDATE employee
SET lastname = 'Judd'
WHERE empid = 100;
Q: Delete all the employee records from the "employee" table using the delete command and the truncate
command.
A: SQL> DELETE FROM employee;
OR
SQL> TRUNCATE TABLE employee;
5
Q: What is the difference between the delete statement and the truncate statement?
A: Notice that the TRUNCATE command is a DDL statement and all DDL statements have commit
inclusive. That is why the ROLLBACK action after truncation does not work. Also, if you want to delete all
records from a table, you should use the TRUNCATE statement. It will change the table watermark. The
table watermark is an address that indicates a last location of a record in a table. On the DELETE statement
the watermark will not change. But using the TRUNCATE statement will change the watermark to the
beginning of the table.
Q: Copy the “EMP” table to another table and name the new table "employee." In the new employee table
use the employee name, job, commission and department number.
A: SQL> CREATE TABLE employee
AS SELECT ename, job, comm, deptno
FROM emp;
Q: Create a view to display the employee names of the “Accounting” department only.
A: SQL> CREATE VIEW employee_name
AS SELECT ename
FROM iself_employee
WHERE deptno = 10;
Q: Create an index on the employee table on the ename column only and name it employee_indx.
A: SQL> CREATE INDEX employee_indx
ON employee (ename);
Q: Create a user with username “newuser” and password "newpass." Its default tablespace should be the
"iself_data" tablespace.
6
A: SQL> CREATE USER newuser IDENTIFIED BY by newpass
DEFAULT TABLESPACE iself_data;
Q: Can the above new user access to any other user tables?
A: No.
Q: How do you open the SQL Script into the Oracle Buffer?
A: SQL> get c:\myscript.sql
Q: What is afiedt.buf?
7
A: The "afiedt.buf" file is a place that into which SQL*PLUS stores the most recently executed SQL
statement.
Q: How do you use the notepad editor as an independent tool in the SQLPLUS utility?
A: Just open your notepad editor outside of your SQLPLUS.
Q: What is the difference between the SQL ANSI statement and Original Oracle statement?
A: The Original Oracle statements are not follow the role of American National Standards Institute.
Q: What does the JOIN syntax in the Oracle SQL (DML) statement?
A: It does innor join using the ON clause.
SQL> SELECT ename, dept.deptno, dname
FROM emp JOIN dept
ON emp.deptno = dept.deptno
AND dname <> 'SALES'
/
Q: What is the difference between the JOIN syntax and the NATURAL JOIN syntax?
A: In the NATURAL JOIN syntax, you don't need the ON clause if the column’s names are the same.
Q: What does the CROSS JOIN syntax in the Oracle SQL statement?
A: We can use the Oracle9i ANSI standard CROSS JOIN syntax with no WHERE clause to create a
Cartesian product.
8
Q: What does the IN clause in the Oracle SQL statement?
A: The IN clause in the Oracle SQL statement is an equivalent of the OR condition in the SQL statement.
Q: What do the OUTER JOIN, RIGHT OUTER JOIN, LEFT OUTER JOIN, and FULL OUTER JOIN
syntax in the Oracle SQL statement?
A: We use the OUTER option when we want all records that have exact match plus those records that have
no match.
Q: How can you perform the FULL OUTER JOIN syntax using the Original Oracle syntax?
A: Although it is possible but it is very difficult to perform the full outer join using the original Oracle
syntax.
Q: Write a query to list all the department names that their total paid salaries are more than 1/3 of the total
salary of the company.
A: SQL> WITH summary_totals AS
(SELECT dname,
SUM(sal) AS totals
FROM emp NATURAL JOIN dept
GROUP BY dname)
SELECT dname, totals
FROM summary_totals
WHERE totals > (SELECT SUM(totals)*1/3
FROM summary_totals)
ORDER BY totals DESC
SQL>/
Q: What are the multiple columns in the SQL statement? Where or how do you use them?
A: We use multiple columns to match the multiple columns returned from the sub-query.
Q: Write a SQL statement to query the name of all employees who earn the maximum salary in their
department using the multiple columns syntax.
A: SQL> SELECT deptno, ename, job, sal
FROM emp
WHERE (deptno, sal) IN
(SELECT deptno, MAX(sal)
FROM emp
GROUP BY deptno)
/
9
Q: Write a SQL statement to query all of the employee names, jobs, and salaries where their salary is more
than 10% of the total company paid salary.
A: SQL> SELECT ename, job, sal
FROM (SELECT ename, job, sal
FROM emp
WHERE sal > (SELECT SUM(sal) * .1
FROM emp)
ORDER BY 3)
/
Q: Can you update, insert, or delete any records while you are using the MERGE statement?
A: Yes.
Q: Write the difference between ROWID and PRIMARY KEY in the Materialized View.
A: Fast refresh requires association between rows at snapshot and master sites. Snapshots that use
ROWIDs to refresh are called ROWID snapshots while those that use primary keys are called primary key
snapshots.
Q: What are the PRIMARY KEY and ROWID in the Materialized View Log?
A: The Materialized View log that use ROWIDs to refresh are called ROWID view log while those that use
primary keys are called primary key view log.
Q: Create a materialized view that contains the department number, number of employees, and total
10
salaries paid to employees by department.
A: SQL> CREATE MATERIALIZED VIEW mv_sal
BUILD IMMEDIATE
REFRESH ON DEMAND
AS SELECT deptno,
COUNT(1) AS no_of_emp, SUM(sal) AS salary
FROM emp
GROUP BY deptno
SQL> /
Q: What parameter should be used to update the materialized view every month automatically without
human intervention?
A: The START WITH SYSDATE option will create an immediate data, and the NEXT(SYSDATE+30)
option will update the table every 30 days.
Q: How does the ‘VALUE LESS THAN’ method work in the data partitioning?
A: The VALUES LESS THAN clause indicates the partition key value must be less then its assigned value
in order to be illegible for any DML transaction on its assigned tablespace.
Q: Create a range-based partitioning table named p_emp. Make sure that the data entry of the each
department goes to its own provided tablespaces such as the accounting department goes to the dept10ts
tablespace, the data entry of the research department goes to the dept20ts tablespace, etc.
A: SQL> CREATE TABLE p_emp (
empno NUMBER(4) PRIMARY KEY,
ename VARCHAR2(10),
job VARCHAR2(9),
mgr NUMBER(4),
hiredate DATE,
sale NUMBER(7,2),
comm NUMBER(7,2),
deptno NUMBER(2))
STORAGE (INITIAL 5K
NEXT 5K
PCTINCREASE 0)
PARTITION BY RANGE (deptno)
(PARTITION dept10
VALUES LESS THAN (20)
TABLESPACE dept10ts,
PARTITION dept20
VALUES LESS THAN (30)
TABLESPACE dept20ts,
PARTITION dept30
VALUES LESS THAN (40)
TABLESPACE dept30ts,
PARTITION deptxx
VALUES LESS THAN (MAXVALUE)
TABLESPACE deptxxts)
SQL> /
13
Q: Write a query to list the accounting employees from the partition table. Use the partition option.
A: SQL> SELECT * FROM p_emp PARTITION (dept10);
Q: Write a query to list employee number 7900 from the sales department?
A: SQL> SELECT * FROM p_emp PARTITION (dept30)
WHERE empno = 7900
SQL> /
Q: What are the differences between the CUBE and ROLLUP functions?
A: See the output…
Q: What environments may use the CUBE and ROLLUP functions most?
A: Warehousing.
Q: Write a query to list an aggregation sum report for each job, in each year, using the ROLLUP grouping
option.
A: SQL> SELECT year, job, SUM(sal), COUNT(*)
FROM emp
GROUP BY ROLLUP (year, job)
SQL> /
Q: Write a query to list an aggregation sum report for each job, in each year, using the CUBE grouping
option.
A: SQL> SELECT year, job, SUM(sal), COUNT(*)
FROM emp
WHERE deptno = 20
GROUP BY CUBE (year, job)
SQL> /
Q: Create an object type with two columns to hold the employee's child name and date of birth and name it
employee_kids .
A: SQL> CREATE TYPE employee_kids AS OBJECT (
NAME VARCHAR2(30),
dob DATE
)
SQL> /
Q: Create the emp_family table containing the kid’s column with a type of employee_kids_table.
A: SQL> CREATE TABLE emp_family
(empno NUMBER,
kids employee_kids_table)
NESTED TABLE kids STORE AS nested_employee_kids_table
SQL> /
Q: What is the difference between a materialized view and a materialized view log?
A: The Materialized view is a real duplicated data from a primary table but the materialized view log is an
on going logs generated due to the table changes after the last refresh.
16
Q: What is a composite index?
A: A primary key can be composed of more than one column. We call it a composite index.
Q: Write a query to concatenate the customers’ last name and first name separated by comma.
A: SELECT last_name || ‘, ‘ || first_name
as “Full Name”
FROM customers
/
Q: Query the employees name and their department name using the “DECODE” function. If the
department number is 10 then print "accounting.” If the department number is 20 then print "research," or if
the department number is 30 then print "sales." Anything else prints others.
A: SELECT ename, DECODE (deptno, 10, 'Accounting',
20, 'Research',
30, 'Sales',
'Others') AS "Department"
FROM emp
/
Q: Query the department number and their total salaries that have more than 5 employees working in their
department.
A: SELECT deptno, SUM(sal)
FROM emp
GROUP BY deptno
HAVING count(*) > 5
/
Q: query the customer names which have more than four orders.
A: SELECT name FROM customer c
WHERE exists (SELECT 'T' FROM ord
WHERE custid = c.custid
GROUP BY custid
HAVING count(*) > 4)
/
Q: What is the difference between the delete statement and the truncate statement?
A: On the DELETE statement the watermark will not change. But using the TRUNCATE statement will
change the watermark to the beginning of the table.
Q: Copy the “EMP” table to another table and name the new table "employee." In the new employee table
use the employee name, job, commission and department number.
A: CREATE TABLE employee
AS SELECT ename, job, comm, deptno
FROM emp
/
Q: Write a query to list all the department names that their total paid salaries are more than 1/3 of the total
salary of the company.
A: SQL> WITH summary_totals AS
(SELECT dname,
SUM (sal) AS totals
FROM emp NATURAL JOIN dept
GROUP BY dname)
SELECT dname, totals
FROM summary_totals
WHERE totals > (SELECT SUM (totals)*1/3
FROM summary_totals)
ORDER BY totals DESC
SQL>/
19