Академический Документы
Профессиональный Документы
Культура Документы
TYPES OF DBMS:
1. HDBMS: In HDBMS all the data are stored in the tree
structure, in this HDBMS retrieving a data is difficult.
2. NDBMS: In NDBMS all the data are stored in tree
structure as well as all the data are interconnected in
network fashion. In NDBMS insertion of data is
difficult.
3. RDBMS: In RDBMS all the data are stored in tables
and tables may have some relation.
SQL: It is a language where we can interact with the
database in the form of queries.
SQL is a language and my-sql is a database.
We have many database in market like Oracle, my-sql, ms-
sql, Derby, Mongo DB, IBM2.
We are using Oracle 10G but latest versions are 11G and
12C
DATA MODEL:
31/1/2o18
Relational Operators:
>
<
>=
<=
=
!=
All the relational operators can be used in where condition but
not select clause.
Q: To retrieve all the employee whose salary is less than 2000?
Ans: SELECT * FROM EMP WHERE SAL< 2000;
Q: To retrieve employee name and salary for all the employee
who earns atleast 1600?
Ans: SELECT ENAME, SAL FROM EMP WHERE SAL >= 1600;
Q: To retrieve all the employee who doesn't belongs to dept.
20;
Ans: SELECT * FROM EMP WHERE DEPTNO!= 20;
LOGICAL OPERATORS:
1. AND
2. OR
3. NOT
Logical operators can be used in where condition but we can
not used within select clause.
Q: To retrieve all employee whose job is manager and works for
department 20?
Ans: SELECT * FROM EMP WHERE JOB= 'MANAGER' AND DEPT=
20;
Q: To retrieve all the analysts who earns more than 1600?
Ans: SELECT * FROM EMP WHERE JOB= 'ANALYSTS' AND SAL >
1600;
Q: To retrieve all the employee who belongs to dept. 20 or 30
respectively?
And: SELECT * FORM EMP WHERE DEPTNO = 20 OR 3O;
Q: To retrieve all the employee who joined in year 81?
Ans: SELECT * FROM EMP WHERE HIREDATE > = '01-JAN-81'
AND HIREDATE <= '31-DEC-81';
Q: To retrieve all the employee whose employee numbers are
7900, 7902, 7788 and 7698?
Ans: SELECT * FROM EMP WHERE EMPNO= 7900 OR EMPNO=
7902 OR EMPNO= 7788 OR EMPNO= 7698;
SPECIAL OPERATORS:
IN OR OPERATOR: Whenever we having one or more
operation we can go for in operator to simplify the query.
eg: SELECT * FROM EMP WHERE EMPNO IN(7900, 7902,
7788, 7698);
Q: To retrieve all the employee whose job is clerk and
analyst?
Ans: SELECT * FROM EMP WHERE JOB IN(CLERK,
ANALYST);
Q: To retrieve all the employee whose salary is in a range
of 1000 - 2500?
Ans: SELECT * FROM EMP WHERE SAL >= 1000 AND SAL <=
2500;
BETWEEN AND OPERATOR: Whenever we are getting
range of values we can go for AND operator.
eg: SELECT * FROM EMP WHERE SAL >= 1000 AND
SAL <= 2500;
length(lit/column):
eg: select length(ename) from emp;
select ename, length(ename) from emp;
Q: Write a query to retrieve all the employee name whose
name have exactly four characters in it don't use like
operator?
Ans: select ename from emp where length(ename)=4;
SELECT 5
FROM 1
WHERE 2
GROUP BY 3
HAVING 4
ORDER BY 6
SUB QUERY:
We go for sub query whenever we want to retrieve
some information based on other unknown information.
sub query in the sense one query within another query
in that case inner query executed first and outer query
executed based on the output of inner query.
Q: Write a query to retrieve all employee who's job is
same as 'FORD' job?
Ans: SELECT * FROM EMP WHERE JOB= (SELECT JOB FROM
EMP WHERE ENAME= 'FORD');
Q: Write a query to retrieve all the employee who's salary
is more than SMITH salary?
Ans: SELECT * FROM EMP WHERE SALARY > (SELECT SAL
FROM EMP WHERE ENAME= 'SMITH');
Q: Write to retrieve all the employee who's salary is more
than miller salary and whose job is same as 'ALLEN' job?
Ans: SELECT * FROM EMP WHERE SALARY > (SALARY SAL
FROM EMP WHERE ENAME = 'MILLER') AND JOB= (SELECT
JOB FROM EMP WHERE ENAME = 'ALLEN');
Q: Write a query to retrieve employee name and salary
whose salary is more than average salary of dept 10?
Ans: SELECT ENAME, SAL WHERE SAL > (SELECT AVG(SAL)
FROM EMP WHERE DEPTNO= 10);
Q: Write a query to retrieve all the employee names who
belongs to new york?
Ans: SELECT ENAME FROM EMP WEHRE DEPTNO= (SELECT
DEPTNO FROM DEPT WHERE LOC= 'NEW YORK');
Q: Write to retrieve department information of scott?
Ans: SELECT * FROM DEPT WHERE DEPTNO= (SELECT
DEPTNO FROM EMP WHERE ENAME= 'SCOTT');
JOINS(oracle joins):
We go for joins whenever we want to retrieve information
form one or more table.
TYPES OF JOINS:
INNER JOINS
1.EQUI JOIN
2,NON-EQUI JOIN
3. SELF JOIN
CARTITION JOIN
OUTER JOINS
1. LEFT OUTER JOIN
2.RIGHT OUTER JOIN
3.FULL OUTER JOIN
INNER JOIN: Only the rows will displayed in output
which ever satisfies the join condition. Those are known
as Innner join rows.
1. Equi join: Whenever we are joing two tables using
equal symbol are known as equi joins.
CARTITION JOIN:
Cartition join means each and every row of one table will be
join with each and every row of another table
We can achieve cartition join wherever the joining condition
is missed out.
eg: select * from emp,dept;
OUTER JOIN:
Outer join means will be retrieving the rows which are satisfy
the condition as well as whichever doesn't satisfy condition
based on the type of outer join.
Left-Outer Join:
Inner join rows + left out rows from left table
eg: select * from emp e, dept d where e.deptno=
d.deptno(+);
Right-Outer Join:
eg: select * from emp e, dept d where e.deptno(+)=
d.deptno;
Full-Outer Join:
Inner join rows + left out rows from left as well as right table.
eg: select * from emp e, dept d where e.deptno(+)=
d.deptno(+);
Note: We can not achieve full outer join using oracle joins
If we want to achieve we have to go for ANSI Joins
ANSI(American National Standard Institute) Join:
Oracle joins only works in oracle database but ANSI Join
works in all database.
eg: select * from emp e full outer join dept d on e.deptno=
d.deptno;
Q: Write a query to retrieve all the managers and clerks who
works in Account and Marketing department?
Ans: select * from emp e, dept d where e.deptno= d.deptno
and job in('MANAGER', 'CLERK') and dname in('ACCOUNTIN',
'MARKETING');
Q: Write a query to retrieve all the salesman who are not
located as DALLAS?
Ans: select * from emp e, dept d where e.deptno= d.deptno
and job= 'SALESMAN' and loc!= 'DALAS';
Q: Write a query to retrieve department name and location
for all the employees who are working for 'CLARK'?(using
join)
Ans: Select dname, loc from emp e, emp m, dept d where
e.mgr= m.empno and e.deptno= d.deptno and m.ename=
'CLARK';
Q: Write a query to display all the employees who works in
same dept has SCOTT but don't retrieve SCOTT
information?(using join)
Ans: select * from emp e1, emp e2 where e1.deptno=
e2.deptno and e1.ename= 'SCOTT' and e2.ename!= 'SCOTT';
CORELATED SUB-QUERIES:
Q: Write a query to retrieve first highest salary?
Ans: select max(sal) from emp;
Q: Write a query to find second highest salary?
Ans: select max(sal) from emp where sal<(select max(sal)
from emp);
Q: Write a query to retrieve third highest salary?
Ans: select * from emp x where 3= (select count(distinct sal)
from emp y where y.sal>= x.sal);
Q: Write a query to retrieve eighth highest salary?
Ans: select * from emp x where 8= (select count(distinct sal)
from emp y where y.sal>= x.sal);
Q: Write a query to retrieve nth highest salary?
Ans: select * from emp x where &n= (select count(distinct
sal) from emp y where y.sal>=x.sal);
Q: Write a query to retrieve third least salary?
Ans: select * from emp x where 3= (select count(distinct sal)
from emp y where y.sal<=x.sal);
Q: Write a query to retrieve second and sixth highest salary?
Ans: select * from emp x where 2= (select count(distinct sal)
from emp y where y.sal<= x.sal) or 6=( select count(distinct
sal) from emp y where y.sal>= x.sal);
Q: Write a query to retrieve first three highest salaries?
Ans: select * from emp x where 3>= (select count(distinct sal)
from emp y where y.sal >= x.sal) order by sal;
Q: Write a query to retrieve five least salaries?
Ans: select * from emp x where 5>= (select count(distinct sal)
from emp y where y.sal<= x.sal) order by sal;
Q: Write a query to retrieve fifth employee who joined the
company?
Ans: select * from emp x where 5= (select count(distinct sal)
from emp y where y.hiredate<= x.hiredate);
Q: Write a query to retrieve all the employee whose salary is
more than average salary of the own department?
Ans: select * from emp x where sal> (select avg(sal) from
emp y wehre y.deptno= x.deptno);
Q: Write a query to retrieve all the employee whose salary is
more than average of salary of their job?
Ans: select * from emp x where sal> (select avg(sal) from
emp y where y.job= x.job);
Q: Write a query to retrieve all the employee whose salary is
greater than the salary of the manager?
Ans: select * from emp x where sal> (select avg(sal) from
emp y where x.mgr= y.empno);
CONSTRAINTS:
Whenever data is being inserted in a table that time if we
want to restrict the data we should create column with
costraints.
1.) NOT NULL:
If we declare any column as not null in that column null
values are not allowed but duplicate values are allowed.
2.) UNIQUE:
If we declare any column as unique in that column null
values are allowed but duplicate values are not allowed.
eg: phone number.
3.) PRIMARY KEY:
It is a combination of both unique as well as not null
constraints i.e. in that column we can not have null values
as well as duplicate values.
4.) CANDIDATE KEY:
Columns that are eligible to become primary key are know
as candidate key.
ALTERNATE KEY:
Columns that are eligible to become primary key but not
selected as primary key.
eg:
DATA TYPE:
1. number: number(8, 2)
here 8 -> is total and 2 ->is decimal value
2.String: char(4) suppose we take SQL to store in that case
one block of memory wasted fixed.
varchar()
varchar2(4)no waste of memory
3.Date: date
Udate ccommand:
update table_name set col1= val1, col2= val2 where
condition;
update studentst set smarks= 80, age= 18, where sid= 111;
Delete command:
delect table_name where condition;
e.g: delete emp2 where job= 'CLERK';
Note: All DML commands are temporary once if we close our
SQL editor whatever DML command is executed will be
reverse.
If we want to make it permanent we have to use a commit
statement.
VIEWS:
views is just a virtual table.
By using views we can achieve two things
1. We can increase a database performance.
2. We can provide security to our data.
Security:
Creating view:
create view view_name
as
select …..
from…..
where…..
order by…..
conn system/tiger;
grant create view to scott;
PL/SQL:
FUNCTION:
Q: Write a PL/SQL command to create a view for tall the
employees and give access to only to the ename, empno, job
and salary?
Ans: create view vw_analyst
as
select ename, empno, job, sal
from emp
where job= 'ANALYST'
order by sal;
syntax:
create or replace function f_name(arguments)
return datatype
as
variable declaration;
begin
executable statement;
end f_name;
/