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

day5 live(1)

Uploaded by

dsid952
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

day5 live(1)

Uploaded by

dsid952
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

--subquery

it is a query within a query


Q1(Q2)

Q1 : outer query
Q2 : inner query

Q2 will be executed first , will pass result to the outer query

--eg display all records who are doing the same job as that of smith
select * from emp where job=
(select job from emp where ename='smith')
and ename !='smith';

--eg display all records who are earning more than the avg sal
select * from emp where sal >
(select avg(sal) from emp);

--eg display name of emp from the accounting department


q1 : emp
q2 : dept
select ename , deptno from emp where deptno=
(select deptno from dept where dname='accounting')

--nested query
select ename , deptno from emp where deptno=
(select deptno from dept where dname= (fgdklfgkldfkg ))

--eg display name of emp from the sales department


select ename , deptno from emp where deptno=
(select deptno from dept where dname='sales');

--eg display name of emp working in new york city


select ename , deptno from emp where deptno=
(select deptno from dept where loc='new york');

--display details of the department table where the employee exists


dept : 10, 20, 30, 40
emp: 10, 20, 30
q1 : dept
q2 : emp

select * from dept where exists


(select * from emp where emp.deptno= dept.deptno) ;

--display details of the department table where the employee not exists
select * from dept where not exists
(select * from emp where emp.deptno= dept.deptno) ;

--eg display all records who are doing the same job as that of smith, jones

select * from emp where job in


(select job from emp where ename='smith' or ename = 'jones');

--eg display all records who are not doing the same job as that of smith, jones
select * from emp where job not in
(select job from emp where ename='smith' or ename = 'jones');
in = used when there are multiple value
= when 1 value

--predicates
>all
>any
<all
<any

--eg display name , sal of emp whose sal is greater than all the employees in
deptno 20
select ename, sal from emp where sal >all (select sal from emp
where deptno=20)
or
select ename, sal from emp where sal >(select max(sal) from emp
where deptno=20);

sal |
+---------+
| 800.00 | 3000 --> n
| 2975.00 | 4000 y
| 3000.00 | 6500 y
| 1100.00 | 7000 y
| 3000.00 | 2000 n
1200 n
1500 n

--eg display name , sal of emp whose sal is greater than any the employees in
deptno 20

select ename, sal from emp where sal >any (select sal from emp
where deptno=20)
or
select ename, sal from emp where sal > (select min(sal) from emp
where deptno=20)

sal |
+---------+
| 800.00 | 3000 --> y
| 2975.00 | 4000 y
| 3000.00 | 6500 y
| 1100.00 | 7000 y
| 3000.00 | 2000 y
1200 y
1500 y

===========
--co related subquery
is performed on the same table
there will be replica created for the same table
there should be 2 cols in a table which are related then only co related subquery
will be performed
co related subquery can not be performed on all tables
it takes time to perform co related subquery.

--eg display the name of employees who are senior to their manager
select a.ename from emp a
where a.hiredate < (select b.hiredate
from emp b
where a.mgr=b.empno);

--eg display the name of employees who are junior to their manager
select a.ename from emp a
where a.hiredate > (select b.hiredate
from emp b
where a.mgr=b.empno);

===================
--Built-In Functions
1 numeric Functions
2 string Functions
3 date Functions
4 conversion Functions

--string Functions 87
select reverse(ename) from emp;
select lcase('PRITI');
select Ucase('priti');
select length('PRITI'); # 5
select concat('PRITI', ' DALVI');
select replace('priti', 'i', 'ee');
select substr('priti',1,3);
select substr('priti',2,3);
--instr() : is used to check for the position

select instr('cdac course' , 'o'); --7

--oracle
select instr('cdac course' , 'c',1,2);
|
occurance
select lpad('priti', 10, '$'); --> padding from left
$$$$$priti

select rpad('priti', 10, '$');

--to remove the space from left


select ltrim(' priti ');
select rtrim(' priti ');
select trim(' priti ');
select lcase(ename) from emp;
select lcase(ucase(ename)) from emp; --nested fun

select ename , length(ename) from emp;


select ename , substr(ename,1,3) from emp;

=====================
--numeric fuctions
select sqrt(144);
select rand(); --0 to 1
select rand() * 100 ; 0 to 100
select round(123.56,1); 123.6 |
select round(123.5612,2); 123.56
select round(123.5612,0); 124
select round(123.5612,-1); 120
select round(123.5612,-2); 100
select round(12333.612,-3); 12000

-1 : round to the nearest 10


-2 : round to the nearest 100
-3 : round to the nearest 1000

select round(rand() * 100, 0) ;


select ename , ifnull(comm, 1200) from emp;
select ceiling(5.2); 6
select floor(5.2); 5
select greatest(5,2,8,99,4,8);
select least(5,2,8,99,4,8);

--date functions
select adddate('2024-1-1', 3); 2024-01-04
select date('2024-1-1 2:44:55'); 2024-1-1
select datediff('2024-4-1 2:44:55', '2024-3-10 2:44:55');
days
--display ename and the expr emp in days
select ename , datediff(curdate(), hiredate) from emp;

--display ename and the expr emp in yrs


select ename , round(datediff(curdate(), hiredate) / 365 , 0)
as expr from emp;

select ename , abs(round(datediff(hiredate, curdate()) / 365 , 0) )


as expr from emp;

--display ename and the expr emp in months

select abs(-99) ; 99

--display age

--date format
SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W'); Saturday
SELECT DATE_FORMAT('1997-10-04 22:23:00', '%M'); October
SELECT DATE_FORMAT('1997-10-04 22:23:00', '%m'); 10

SELECT DATE_FORMAT('1997-10-04 22:23:00', '%Y'); 1997


SELECT DATE_FORMAT('1997-10-04 22:23:00', '%W %M %Y %T');
SELECT DATE_FORMAT('1997-10-04 22:23:00', '%D');

===joins
it is used to get the data from 2 or more tables

-types of join
1 inner join
2 outer join
left outer join
right outer join
full outer join --> not surppoted in mysql
3 cross join
4 self join

===
--1 cross join
mapping will be performed
every row from the first table will be mapped to all the rows in the 2nd table
eg : 3 schemes --> 100 customer to be explained
eg : shrits with all the sizes in a mall
rows : *
cols : +

t1
c1 c2
10 x
20 y

t2
c3 c4 c5
1 a pp
2 b yy
3 c kk

c1 c2 c3 c4 c5
10 x 1 a pp
10 x 2 b yy
10 x 3 c kk
20 y 1 a pp
20 y 2 b yy
20 y 3 c kk
rows : 2 * 3 =6
cols : 2 + 3= 5

--eg
select * from emp
cross join dept;

emp r 14 cols : 8
dept r 4 cols : 3
row : 14 * 4 =56 cols : 8+3=11

--eg
select ename, sal, dname , loc from emp
cross join dept
where sal > 1000 and comm is null;
=========================
--inner join
the common records will be displayed

emp : 10, 20, 30


dept : 10, 20, 30, 40
common :10, 20, 30

--eg
e e d d
select ename ,sal, dname , loc, d.deptno
from emp e, dept d
where e.deptno=d.deptno;

--eg
select ename ,sal, dname , loc, d.deptno
from emp e, dept d
where e.deptno=d.deptno and sal > 1000 and d.deptno in (10,20);
--eg common subjects in ele, and mech eng
common lag : spoken : eng
diff departments : lunch : gather : canteen / cafeteria

======joining 3 tables
create table location(
loc varchar(20),
remark varchar(20));
insert into location values('NEW YORK', 'remark1');
insert into location values('DALLAS', 'remark2');
insert into location values('CHICAGO', 'remark3');
insert into location values('BOSTON', 'remark4');

e e d d l l
select ename ,sal, d.deptno , dname, l.loc , remark
from emp e, dept d, location l
where e.deptno=d.deptno
and d.loc=l.loc;

--outer join
--note
you can expect the null values in outer join

--left outer join


emp : 10, 20, 30
dept : 10, 20, 30, 40
common :10, 20, 30

--14 rec
select ename , sal ,dname , loc, d.deptno
from emp e
left join
dept d
on (e.deptno=d.deptno);

--right outer join 15 rec


select ename , sal ,dname , loc, d.deptno
from emp e
right join
dept d
on (e.deptno=d.deptno)
where sal > 1000;

--full not working


select ename , sal ,dname , loc, d.deptno
from emp e
full join
dept d
on (e.deptno=d.deptno) ;

--eg present_emp common retired_emp


200 50 30
annal fun : call the emp all emp : full outer join
call the emp who are retired and fnf is pending : right outer join
call the emp who are fnf is pending and the active emp : left outer join
call the emp who fnf is pending : inner join
==========
student passed , reval failed
300 10 5
===================
--self join
joining the table with itself
will be peformed on the same table
there should be 2 cols which are related then only self join will be used
two replicas will be created for the same table
it takes time to execute self join
self join can not be used will all the tables

--eg display the name of the emp and his manager name
select a.ename,a.sal, b.ename mgr_name , b.sal mgr_sal
from emp a, emp b
where a.mgr=b.empno;

================
--diff between self join and corelated subquery
self join : we can display the details from emp b also
corelated subquery: we can not display the details from emp b

==================
ddl commands are autocommit : create , alter , drop, truncate

create table emp4(


empno int,
sal int);
set autocommit=0;
insert into emp4 values(1, 70000);
insert into emp4 values(2, 30000);
rollback;
select * from emp4;
insert into emp4 values(1, 70000);
commit;
select * from emp4;
insert into emp4 values(2, 70000);
insert into emp4 values(3, 70000);
rollback;
select * from emp4; # 1

--todo
set operator
union, union all, minus, intersect

You might also like