Dbms Lecture
Dbms Lecture
3 CAPABILITIES
a. SELECTION - this capability of sql that select rows from a table
b. PROJECTION - this capability of sql that select columns from a table
c. JOINING - used to bring together data from different tables.
BASIC SELECT STATEMENT
select * /column_name / expression
from table_name;
SELECT CLAUSE
-specifies the column to be displayed
FROM CLAUSE
-specief the table containing the columns listed in the selected clause
VARCHAR2() -string
DATE
- format sensitive
NUMBER -real
-integral
DESCRIBE -structured
* - content
RESTRICTING & SORTING OF DATA
WHERE CLAUSE- use to restrict no. of rows retrieve if the condition is m
et.
ORDER BY CLAUSE - sorting of data by Asc or Desc
COMPARISON OPERATOR
= equal
<> not equal
>= greater than equal
> greater than
<= less than equal
< less than
3 COMMON DATA TYPES
a. varchar2
b. number
c. dates
OTHER COMPARISON OPERATOR
* BETWEEN AND
select * from employees
where salary between 3000 and 5000;
select * from employees
where salary >= 3000 and salary <=5000;
*IN (SET)
select * from employees
where department_id in (20, 90. 50);
select * from employees
where department_id =20 or department_id = 90 or departm
ent_id=50;
*like
e, 2)
from employees;
c. NEXT_DAY
select last_name, hire_date, next_day(sysdate, '
Friday')
from employees;
D. LAST_DAY
select last_name, hire_date, last_day(sysdate)
from employees;
E. ROUND
*round months---- default ang days, same year either
1-15 same month
16-31 next month
select last_name, hire_date, round(hire_date, mo
nth') from employees;
*round year
ths
1-6 months same year
7-12 months next year
select last_name, hire_date, round(hire_date, 'y
ear') from employees;
F. TRUNC
*trunc month
- default ang days, same month and year.
select last_name, hire_date, trunc(hire_date, 'm
onth') from employees;
*trunc year
ar.
select last_name, hire_date, trunc(hire_date, 'y
ear') from employees;
4. CONVERSION
FUNCTION
1. IMPLICIT - si oracle mismo ang nagcoconvert
implicit data type conversion
FROM
TO
varchar2 or char
number
varchar2 or char
dates
number
varchar2
date
varchar2
ELEMENTS OF NUMB
ER
9
0
$
.
,
represent number
force a zero to be displayed
place floating dollar sign
print decimal point
print thousand indicator
50-99
if two digit
0-49
return date in t
50-99
return date in the
current century
current one
select hire_date, to_char(hire_date,'fm
DD-MON-RRRR') from employees;
2. EXPLICIT - si user and nagcoconvert
char to date = to_date
char to number = to_number
number to char = to_char
date to char = to_char
to_char function
to_char(date, 'format_model');
FORMAT MODEL
-must be enclosed in single quotation
-has an fm element to remove padded blan
ks or suppress leading zero
-separate from date value by comma.
ELEMENTS of DATE
YYYY- full year in numbers
YEAR - spelled out year
MM - two digit no.
MONTH - spelled out month
MON - three letter abbreviation of month
DY
- three leeter abbreviation of d
ay
DAY - full name of the day
DD
- two numeric of the day
sp - spelled out nia
th - maglalagay sia ng th
HH - hours
MM - minutes
SS - seconds
select to_char(sysdate, 'fm ddsp
th "of" month yyyy') from dual;
select hire_date, to_char(hire_d
ate, 'fm Ddsp-Month-Year') from employees;
select hire_date, to_char(hire_d
ate, 'fm Dy-Month-Year') from employees;
select to_char(sysdate, 'fm ddsp
th "of" month yyyy HH.MM.SS') from dual;
5. GENERAL -NULL VALUE IS WALANG LAMAN!!!
*nvl(expr1, expr2)
select last_name, nvl(commission
EXPRESSION
select last_name, salary, job_id,
case job_id
when 'IT_PROG' then salary*1.10
when 'SA_REP' then salary*1.20
when 'ST_CLERK' then salary*1.30
else salary end "Revised Salary"
from employees;
*Decode FUNCTION
select last_name, salary, job_id
,
decode(job_id,
'IT_PROG', salary*1.10,
'SA_REP', salary*1.20,
'ST_CLERK', salary*1.30,
salary)"Revised Salary"
from employees;
ACTIVITY
select last_name, salary, job_id, salary*12 as "Annual Salary",
nvl(to_char(commission_pct), 'no commission'),
case MOD(length(last_name)+length(first_name), 2)
- kung ano lang ang may equal sa join natin yun lang ung ilalaba
select employees.last_name, employees.salary, department
s.department_name,
employees.job_id
from employees,
departments where employees.department_id = departments.
department_id;
select employees.last_name, employees.salary, department
s.department_name, locations.city
from employees, departments, locations
where employees.department_id=departments.department_id
and departments.location_id=locations.location_id;
select e.last_name, d.department_name, l.city
from employees e, departments d, locations l
where e.department_id=d.department_id and d.location_id=
l.location_id and
(last_name like 'A%' or last_name like 'J%' or last_name
like 'M%');
select e.last_name, r.region_name
from employees e, regions r, countries c, locations l, d
epartments d
where r.region_id=c.region_id and
c.country_id=l.country_id and
l.location_id=d.location_id and
d.department_id=e.department_id;
2. Non EquiJoin - pwedeng pagsamahin ung ibang table gamit ung mga comparison op
erator
kapag wala siyang kagaya na column.
select last_name
from employees e
join departments d
on d.department_id= e.department_id;
5. LEFT / RIGHT / Full Outer Join select e.last_name, d.department_name
from employees e
full outer join departments d
on e.department_id=d.department_id;
Group FUNCTION
-avg
-sum
-max
-min
-count
select avg(salary), max(salary), min(salary), sum(salary), count(employee_id)
from employees;
select department_id, avg(salary) from employees;
select * from locations;
select * from departments;
select sum(e.salary), avg(e.salary), l.city
from employees e, departments d, locations l
wher
e e.manager_id=d.manager_id and l.location_id=d.location_id
group by city;