Day 1: SQL/PLSQL Concepts
Day 1: SQL/PLSQL Concepts
UPDATE <table_name>
SET <column_name> = <value>
WHERE <column_name> = <value> ;
ENAME SAL
------------------------------------
MARTIN 1250
TURNER 1500
WARD 1250
Using the IN Operator
Use the IN operator to test for values in
a list
SQL> SELECT empno, ename, sal, mgr
FROM emp
WHERE mgr IN (7902, 7566, 7788);
ENAME
-------------------------
MARTIN
JAMES
WARD
Using the IS NULL Operator
ENAME MGR
---------- ---------
KING
Logical Operators
Operator Meaning
Returns TRUE if both
AND component conditions are
TRUE
Returns TRUE if either
OR component condition is
TRUE
Returns TRUE if the
NOT following condition is
FALSE
Using the AND Operator
AND requires both conditions to be TRUE.
ENAME JOB
---------------- --------------------------
Ram PRESSIDENT
Shayam SALESMAN
Sohan SALESMAN
Mohan SALESMAN
Rules of Precedence
2 NOT
3 AND
4 OR
Rules of Precedence
SQL > SELECT ename, job, sal
FROM emp
WHERE job = ‘SALESMAN’
OR job = ‘PRESSIDENT’
AND SAL > 1500;
CEIL (2.83) 3
CEIL (x) CEIL (2.49) 3
CEIL (-1.6) -1
FLOOR (2.83) 2
FLOOR (x) FLOOR (2.49) 2
FLOOR (-1.6) -2
TRUNC (125.456, 1) 125.4
TRUNC (x, y) TRUNC (125.456, 0) 125
MONTHS_BETWEEN ('16-Sep-
MONTHS_BETWEEN( ) 3
10', '16-Dec-10')
NEXT_DAY ('06-Jun-10',
NEXT_DAY( ) 09-JUN-10
'Wednesday')
4) Conversion Functions: These are functions that help us to convert a
value in one form to another form. For Example: a null value into an actual
value, or a value from one datatype to another datatype like NVL,
TO_CHAR, TO_NUMBER, TO_DATE etc.
MAX(): This function is used to get the maximum value from a column.
E.g. SELECT MAX (salary) FROM employee;
MIN(): This function is used to get the
minimum value from a column.
SELECT MIN (salary) FROM employee;
Equijoins :
SELECT e.last_name, e.job_id, d.department_id, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id
ORDER BY last_name, job_id;
Non-equi Joins :
select e.empno, e.ename, e.sal, s.grade
from emp e, salgrade s
where e.sal between s.lowsal and s.hisal;
Self Joins :
SELECT e1.last_name||' works for '||e2.last_name "Employees and
Their Managers"
FROM employees e1, employees e2
WHERE e1.manager_id = e2.employee_id
AND e1.last_name LIKE 'R%'
ORDER BY e1.last_name;
Inner Join :
It is a join of two or more tables that returns
only those rows that satisfy the join condition.
Outer Joins :
select e.empno, e.ename, e.sal, e.deptno,
d.dname, d.city from emp e, dept d
where e.deptno(+)=d.deptno;
Creating and managing Tables with
constraints
CREATE TABLE table_name (
column_name datatype [[CONSTRAINT constraint_name]
column_constraint] [default_value],
OPEN emp_cur;
LOOP
FETCH emp_cur INTO emp_rec;
dbms_output.put_line(emp_rec.first_name || ' ' ||
emp_rec.last_name || ' ' || emp_rec.salary);
END LOOP;
END;
How to execute a Stored Procedure?
1) From the SQL prompt.
EXECUTE [or EXEC] procedure_name;
2) Within another procedure –
Creating Functions
CREATE [OR REPLACE] FUNCTION function_name
[parameters] RETURN return_datatype;
IS Declaration_section
BEGIN
Execution_section
Return return_variable;
EXCEPTION exception section;
END;
CREATE OR REPLACE FUNCTION
employer_details_func RETURN
VARCHAR(20);
IS
emp_name VARCHAR(20);
BEGIN
SELECT first_name INTO emp_name
FROM emp_tbl
WHERE empID = '100‘;
RETURN emp_name;
END;
How to execute a PL/SQL Function?