0% found this document useful (0 votes)
126 views

Dbms Lecture

SQL is a language used to manage data in relational database management systems. It allows users to select, insert, update, and delete data as well as manage schema objects and grant permissions. Some key capabilities of SQL include selection of rows and columns from tables, joining of data from multiple tables, and restricting and sorting of retrieved data. Common data types in SQL are numbers, dates, and character strings. Functions allow manipulation of data values and conditional logic. Joins are used to combine data from different tables.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Dbms Lecture

SQL is a language used to manage data in relational database management systems. It allows users to select, insert, update, and delete data as well as manage schema objects and grant permissions. Some key capabilities of SQL include selection of rows and columns from tables, joining of data from multiple tables, and restricting and sorting of retrieved data. Common data types in SQL are numbers, dates, and character strings. Functions allow manipulation of data values and conditional logic. Joins are used to combine data from different tables.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL- structured Query Language

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

-wild core searching


% - denotes one or more characters

_ - denotes one character


select * from employees where last_name like '%a%';
*is null
select * from employees
where commission_pct is null;
SINGLE ROW FUNCTION
- manipulate data items
-accept arguments and return one value
-can be nested
TYPES OF SINGLE ROW FUNCTIONS
1. CHARACTER
*CASE MANIPULATION
1. LOWER
select last_name from employees
where last_name like upper('a_%');
2. UPPER
3. INITCAP -camel casing
select initcap( first_name||last_name) f
rom employees;
*CHARACTER MANIPULATION
A. CONCAT
select concat(concat(concat(last
_name, ' '), job_id),last_name);
last_name || ' ' || job_id
B. SUBSTR
select substr('darrellad', 1, 5)
from dual;
1st argu - string
2nd argu - from what no. charact
er
3rd argu - to what no. of charac
ter
C. LENGTH
select length(last_name) from em
ployees;
D. INSTR
select instr('darrellad','r') fr
om dual;
-kinukuha niya kung pangilang ch
aracter ung first occurance na nakuha
nung letter na nilagay nia.
E. LPAD
select Lpad(salary, 10, '*') fro
m employees;
1st argu - column na meron sia.

2nd argu - limit nung string


3rd argu - if kulang anong character ang
ipapalit mo.
F. RPAD
select Rpad(salary, 10, '*') fro
m employees;
G. TRIM
select trim('d' from 'darrellad'
) from dual;
2. NUMBER
mod - kinukuha nia yung modulus or remainder
select length(last_name), mod(salary,2) from employees
trunc- wala siang pakialam sa decimal point.
round- ggawin niyang whole no. round-off
3. DATE
a. MONTHS_BETWEEN -no. of months between two dates
select last_name, hire_date, round(months_betwee
n( sysdate, hire_date))
from employees;
b. ADD_MONTHS

select last_name, hire_date, add_months(hire_dat

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

------- defaul ang days, default ang mon

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

- default ang days, month, tapos same ye

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

select last_name, salary, to_char(salary


,'fm$99,999.00')
from employees;
select last_name, salary, 'Php ' || to_c
har(salary,'fm99,999.00')
from employees;
select salary, to_char(salary,'fm$99,999
.00')
from employees;
select last_name, hire_date
from employees
where hire_date= to_date('JUNE 1
7, 1987','fm MONTH DD"," YYYY' );
RR-Date FORMAT
specified date
0-49
he
current year

50-99
if two digit

0-49

return date is in the


century BEFORE the current one
of current
year Are

return date in t

50-99
return date in the

return date in the

century AFTER 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

_pct, 0) from employees;


*nvl2(expr1 , expr2, expr3)
expr1 //pag walang laman o null
expression 3 ung lalabas
=pag my laman o
not null, lalabas ang expression ng 2.
select last_name, nvl2(commissio
n_pct, 'sal+comm', salary) from employees;
*nullif(exr1, expr2)
-pag parehas sila nung length ng
expr1 and 2 magnull sia
-pag hindi parehas kukunin nia u
ng lenght ng first expression.
select last_name, first_name, nu
llif(length(last_name), length(first_name)) from employees;
*coalesce(expr1, expr2, ....exp)
-pag null sia icheck nia ung sus
unod na expression, pag not null ilalagay nia ung expression.
select last_name, commission_pct
, manager_id, coalesce(commission_pct, manager_id, salary) from employees;
CONDITIONAL EXPRESSION
*

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)

when 0 then 'Even'


else 'Odd' end"Odd or Even"
from employees
where last_name not like 'A_%' and last_name not like 'E_%' and
last_name not like 'I_%' and last_name not like 'O_%' and last_name not like 'U_
%';

--------------------------------------------------------------------------------------------------------------JOINING - kumukuha ng data from different table


21- Employees
8- Department
------------------168
cartician product=lalabas ung parepareho
SQL 8i Prior
1. EquiJoin
s.

- 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 e. last_name, e.salary, jg.grade_level


from employees e, job_grades jg
where e.salary between jg.lowest_sal and jg.highest_sal;
3. Outer Join - maglalagay ng (+) para sa mga null values
-left or right lang.
select e.last_name, d.department_name
from employees e, departments d
where e.department_id(+)=d.department_id;
4. Self Join- pinagjoin sa sariling table
employee id and lastname ----- worker
manager employee_id and manager lastname ------ managers
select w.employee_id "Employee ID",
w.last_name"Employee Name",
m.employee_id "Manager Employee ID",
m.last_name"Manager Name"
from employees w, employees m
where m.employee_id(+)=w.manager_id;
select e.last_name, e.hire_date
from employees e, employees davies
where davies.last_name='Davies' and
davies.hire_date>e.hire_date;
Sql 1999 syntax
1. CROSS JOIN - cross product ng table
- cartesian product
select last_name
from employees
cross join departments;
2.NATURAL JOIN -what is common between two table.
-automatic
select last_name
from employees
natural join departments;
3. USING CLAUSE- pgjoin ung pag dalawang column name na magkaparehas
-more than one ung ipapagjoin.
kung kaninu siya maglink.
select last_name
from employees e
join departments
using (department_id);
4. ON CLAUSE

-specify ung joining condition


-equivalent ng equijoin.

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;

You might also like