Mock Test 8
Mock Test 8
NF-1
NF-2
•NF-1+
•Find the unique keys and assign Primary keys. Direct dependent redundant data taken
into new table.
NF-3
•NF-2+
&& once input is provided, subsequent call variable is taken from memory
WITH t
AS
(
SELECT ‘KING‘ ename, ‘king@mgmail.com‘ email_id FROM dual
UNION ALL
SELECT ‘SCOTT‘ ename, ‘scott_tiger@gmail.com‘ email_id FROM dual
UNION ALL
SELECT ‘FORD‘ ename, ‘ford@gmail.com‘ email_id FROM dual
)
SELECT * FROM t WHERE email_id LIKE ‘%\_%‘ ESCAPE ‘\‘
/
ENAME EMAIL_ID
—— ———–
SCOTT scott_tiger@gmail.com
_ and “%” are special characters used inside LIKE clause. So to find string with _ or %
we need to add ESCAPE in search query
Example:
1 Debra Burks
2 Kasha Todd
3 Tameka Fisher
4 Daryl Spence
5 Charolette Rice
6 Lyndsey Bean
7 Latasha Hays
SELECT e1.empno,
e1.ename,
e1.sal,
e2.avg_sal
FROM emp e1 WHERE e1.sal >
(SELECT AVG(e2.sal) AS avg_sal
FROM emp e2
WHERE e1.deptno = e2.deptno
) ORDER BY e1.sal DESC
/
ERROR at line 4:
ORA-00904: “E2″.”AVG_SAL”: invalid identifier
VARCHAR2 -> minimum size is VARCHAR2(1);
LONG -> There cannot be more than one column of LONG datatype.
1. Data Storing
2. Data retrieval
3. Data modification
If number < 0, then the SIGN function returns -1. if number = 0, then the SIGN function
returns 0. If number > 0, then the SIGN function returns 1.
SELECT * FROM emp ORDER BY sal DESC FETCH NEXT 2 ROWS WITH TIES
/
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
—— —— ——- —— ——— —- —– ———-
7839 KING PRESIDENT – 17-NOV-81 5000 – 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 – 20
7902 FORD ANALYST 7566 03-DEC-81 3000 – 20
Syntax
The value used to determine the largest integer value that is equal to or greater than the
number.
WITH t
AS
(
SELECT TO_TIMESTAMP(‘06-09-2020 09:15:25.245374‘,‘DD-MM-YYYY
HH:MI:SS.FF‘) outputTime FROM dual
)SELECT EXTRACT(YEAR FROM t.outputTime) time_year,
EXTRACT(MONTH FROM t.outputTime) time_month,
EXTRACT(DAY FROM t.outputTime) time_day,
EXTRACT(HOUR FROM t.outputTime) time_hour,
EXTRACT(MINUTE FROM t.outputTime) time_Minute,
EXTRACT(SECOND FROM t.outputTime) time_Second
FROM t
/
TIME_YEAR TIME_MONTH TIME_DAY TIME_HOUR TIME_MINUTE TIME_SECOND
———- ————– ———- ———– ———- —————
2020 9 6 9 15 25.245374
ROUND(Num, n)
-1 means works on digit before decimal means here 5 => TRUNC(315.451, -1) = 310
-2 means works on 2 digit before decimal means here 15 => TRUNC(315.451, -2) =
300
-3 means works on 3 digit before decimal means here 315 => TRUNC(315.451, -3) = 0
LAST_DAY(date) takes input a date and displays the last date of the month
Oracle recommends that you specify explicit conversions, rather than rely on implicit or
automatic conversions,
•SQL statements are easier to understand when you use explicit data type conversion
functions.
•Implicit data type conversion can have a negative impact on performance
Example:
SELECT SUM(e.sal)
FROM emp e GROUP BY deptno HAVING e.deptno IN (10, 20) ORDER BY SUM(e.sal)
DESC
/
SUM(E.SAL)
————–
10875
8750
DECODE ( , , ,
,,
);
Equivalent to
IF ( = ) THEN
ELIF ( = ) THEN
ELSE
END IF;
SELECT DECODE(40, 10, ‘ACCOUNTING‘, 20, ‘RESEARCH‘,30, ‘SALES‘, ‘OTHERS‘ )
dept_desc
FROM dual
/
dept_desc
———-
OTHERS
SELECT A.VAL A_VAL, B.VAL B_VAL FROM A
LEFT OUTER JOIN B ON A.ID = B.ID
/
A_VAL B_VAL
—– ———–
20 20
30 30
10
LEFT OUTER JOIN (LEFT JOIN)
It would return the all records from table1 and only those records from table2 that
intersect with table1
Self-joins
Object privilege
Example:
We can find all the object level privileges granted to a user or a role
SQL> SELECT grantee, table_name, privilege FROM user_tab_privs
/
GRANTEE TABLE_NAME PRIVILEGE
————- ————- ——————
ROLE_ADMIN DEPT DELETE
ROLE_ADMIN DEPT INSERT
SCOTT EMPLOYEES SELECT
ROLE_ADMIN EMP SELECT
How to check it
CONN user1/user1
SELECT *
FROM user_tab_privs WHERE table_name = ‘EMP‘;
conn scott/tiger
–Find grants attached to a role
SELECT * FROM user_tab_privs WHERE grantee = ‘ROLE_CLERK‘
/
GRANTEE OWNER TABLE_NAME GRANTOR PRIVILEGE
———— —— ————- ——– ———
ROLE_CLERK SCOTT FRUITS SCOTT SELECT
ROLE_CLERK SCOTT EMP SCOTT SELECT
ROLE_CLERK SCOTT DEPT SCOTT SELECT
Conn user1/user1
SELECT * FROM user_tab_privs WHERE owner = ‘SCOTT‘
/
GRANTEE OWNER TABLE_NAME GRANTOR PRIVILEGE
——– —— ———- ——— ——
USER1 SCOTT EMP SCOTT INSERT
USER1 SCOTT EMP SCOTT SELECT
USER_TAB_PRIVS can provide privileges granted to a user and role
CREATE TABLE Emp_Exp
(
Empl_id VARCHAR2(10),
Empl_nm VARCHAR2(30),
Joining_date DATE,
Experince INTERVAL YEAR TO MONTH
)
/
Table created
Example:
Note: