day5 live(1)
day5 live(1)
Q1 : outer query
Q2 : inner 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);
--nested query
select ename , deptno from emp where deptno=
(select deptno from dept where dname= (fgdklfgkldfkg ))
--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
--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
--oracle
select instr('cdac course' , 'c',1,2);
|
occurance
select lpad('priti', 10, '$'); --> padding from left
$$$$$priti
=====================
--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
--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;
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
===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
--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
--14 rec
select ename , sal ,dname , loc, d.deptno
from emp e
left join
dept d
on (e.deptno=d.deptno);
--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
--todo
set operator
union, union all, minus, intersect