#Day 1:-
#SQL => Structure Query Language
# Database (OTP)(container) / Data Warehouse(OAP)
#Types of data
-- * Structured Data (Mysql)(rows & columns)
-- * UnStructured Data (NoSql)(image ,text audio)
-- * SemiStructured Data (both My & No)
##DDL => data definition language:-
-- create (name varchar (50) ,age int,location varchar(50),address varchar(50),contact_no int)
-- rename
-- alter
-- truncate
-- drop
##DML => Data manipulation language
-- insert into pd (name,age,location,address.contactno)
-- delete
-- update (set sql_safe_updates = False)
##DCL => Data control language
-- Grant
-- Revoke
##DQL => data query language
-- select
use sakila;
select * from actor;
##TCL => Transactional control language
-- commit
-- savepoint s1
savepoint s1;
rollback to s1;
create database sabari;
use sabari;
create table Student(id int, stu_name varchar(50), age int,location varchar(50));
insert into sabari.Student(id,Stu_name,age,location) values
(1,'Sabari',25,'Lic'),
(2,'Ramya',22,'Anna nagar'),
(3,'shalini',23,'Porur'),
(4,'Sandhya',22,'Vadapalani'),
(5,'Priya',24,'Mountroad');
select * from sabari.student;
#Alter:-
alter table sabari.student
add column mobile int;
alter table sabari.student
rename column mobile to ph_no;
alter table sabari.student
modify column ph_no bigint;
alter table sabari.student
drop column ph_no;
rename table student to College;
# description
desc college;
# truncate:-
truncate sabari.college;
# delete
set sql_safe_updates = False;
delete from sabari.college
where id = 3;
#update
insert into sabari.college(location) values ('Kolathur');
update sabari.college
set location = 'Kolathur'
where id = 1 and stu_name = 'Sabari';
delete from sabari.college
where location = 'kolathur';
# Tcl
set autocommit = 0;
savepoint s2;
delete from sabari.college
where id = 5;
rollback to s2;
#DCL
# Grant
# revoke
Day 2:-
##Functions:-
create table worker(worker_id int not null primary key auto_increment,First_name
varchar(50),Last_name varchar(50),
Salary int(15),Department varchar(25));
insert into worker(First_name,Last_name,Salary,Department) values
('Monika','Arora',100000,'Hr'),
('Niharika','Verma',80000,'Admin'),
('Vishal','Singhal',50000,'Hr'),
('Amitabh','singh',450000,'Manager'),
('Vivek','Bhati',300000,'TL'),
('Sam','Frank',350000,'Engineer'),
('Geetika','Chauhan',400000,'Admin'),
('Satish','Kumar',200000,'Sales'),
('Vipul','Diwan',250000,'Account'),
('Anvitha','Kumar',150000,'Manager');
# Conditions:- <=, >=,=
select * from sabari.worker
where salary >= 100000;
select * from sabari.worker
where salary <= 450000;
##and
select * from sabari.worker
where salary >=250000 and department = 'Account';
#or
select * from sabari.worker
where Salary >= 300000 or department = 'Hr';
#Null
select * from sabari.worker
where worker_id is null;
#Not null
select * from sabari.worker
where worker_id is not null;
# in
select * from sabari.worker
where Department in ('Hr','Admin');
# not in
select * from sabari.worker
where Department not in ('Hr','Admin');
#Between
select * from sabari.worker
where salary >=50000 and Salary <=200000;
select * from sabari.worker
where salary between 100000 and 500000;
##SET
create table sabari.set_table1
as
select * from sabari.worker
where salary <=200000;
create table sabari.set_table2
as
select * from sabari.worker
where salary >=200000;
##Union
select first_name,department,salary from sabari.set_table1
where first_name = 'satish'
union
select first_name,department,salary from sabari.set_table2
where first_name = 'satish';
##Union all
select first_name,department,salary from sabari.set_table1
where first_name = 'satish'
union all
#minus
select first_name,department,salary from sabari.set_table1
where first_name = 'satish'
minus
select first_name,department,salary from sabari.set_table2
where first_name = 'satish';
select first_name,department,salary from sabari.set_table1
where First_name = 'satish' and first_name not in(select First_name from sabari.set_table2
where first_name = 'satish');
#a-b may be newly added
select first_name,department,salary from sabari.set_table1
where First_name not in(select First_name from sabari.set_table2);
#B-A
select first_name,department,salary from sabari.set_table2
where First_name not in(select First_name from sabari.set_table1);
#Intersect -> which is common in both set
select first_name,department,salary from sabari.set_table1
where First_name = 'satish'
intersect
select first_name,department,salary from sabari.set_table2;
###String Functions:-
#Substring
select first_name,last_name,substring(first_name,1,2),substring(last_name,-2) from
sabari.worker;
#Concat
select first_name,last_name,concat(substring(first_name,1,2),substring(last_name,-
2),'@gmail.com')as email
from sabari.worker;
#Upper, Lower
select upper(first_name),lower(last_name) from sabari.worker
where lower(first_name) = 'Amitabh';
##Trim,Ltrim,Rtrin,Length
select ' Sabari Vishwa ',length(' Sabari Vishwa '),
trim(' Sabari Vishwa '),length(trim(' Sabari Vishwa ')),
ltrim(' Sabari Vishwa '),length(ltrim(' Sabari Vishwa ')),
rtrim(' Sabari Vishwa '),length(rtrim(' Sabari Vishwa '));
#replace
select replace(' Sabari Vishwa ',' ','$');
#Like
#Char%
select * from sabari.worker
where First_name like 'vi%';
#%char
select * from sabari.worker
where last_name like '%nk';
#%char%
select * from sabari.worker
where last_name like '%or%';
#Char_
select * from sabari.worker
where last_name like 'bhati';
#Date Function:-
select current_date(),current_time(),current_timestamp();
#Date add
alter table sabari.worker
add column joining_date timestamp default current_timestamp();
select date_add(current_date(),interval -2 year);
#extract
select extract(year from current_date());
#Str_to_date
select str_to_date('2023june14','%Y%M%D'),date_format(current_date,'%Y%M');
#Timestampdiff
select timestampdiff(month,date_add(current_date(),interval 1 - 2 year),current_date());
select timestampdiff(year,str_to_date('1978October01','%Y%M%D'),current_date()); -- age
calculator
Day 3:-
use sabari;
## Quater
select concat(extract(year from current_date()),'Q',quarter(current_date()));
###Aggregations(one line results)->
select count(*),min(salary),max(salary),avg(salary),sum(salary)from sabari.worker;
### Not Exists
create table if not exists sabari.worker(id int,name varchar(50));
#exists
drop table sabari.set_table1;
###Clause:-
# Distinct
#Count
##Distinct
select distinct department from sabari.worker;
#Count
select count(distinct department)from sabari.worker;
###Order by
select * from sabari.worker
order by salary desc;
##Limit:-
select *from sabari.worker
order by salary desc
limit 2;
##Offset:-
select * from sabari.worker
order by salary desc
limit 5
offset 2;
###Group by
select department,count(distinct first_name) from sabari.worker
group by Department;
##Having
select department,count(distinct first_name) from sabari.worker
group by department
having count(distinct First_name)>1;
###JOins
create table sabari.department(id int primary key,dept_name varchar(50));
insert into sabari.department values
(2,'CSE'),
(3,'IT'),
(4,'EEE'),
(6,'ECE'),
(5,'IIT');
create table sabari.Student(id int,name varchar(50),deptid int);
insert into sabari.student values
(1,'Priya',4),
(2,'Sabari',3),
(3,'Samuel',2),
(4,'Saya',1),
(5,'Jaseil',5);
### Inner Join-> to combine same data in two table.
select a.id,a.name,b.dept_name from sabari.student a
inner join sabari.department b
on a.deptid = b.id;
##Left join
select a.id,a.dept_name,b.deptid from sabari.department a
left join sabari.student b
on a.id = b.deptid;
## Right join
select a.id,a.name,b.dept_name from sabari.student a
right join sabari.department b
on a.deptid = b.id;
#full outer join
##select a.id,a.name,b.dept_name from sabari.student a
-- outer join sabari.department b
-- on a.deptid = b.id;
select a.id,a.name,b.dept_name from sabari.student a
left join sabari.department b
on a.deptid = b.id
union
select a.id,a.dept_name,b.deptid from sabari.department a
right join sabari.student b
on a.id = b.deptid;
##Self join
create table sabari.employees(id int, name varchar(50),manager_id int);
insert into sabari.employees values
(1,'John Doe',null),
(2,'Jane Smith',1),
(3,'Mark Johnson',2),
(4,'Sarah Davidson',3),
(5,'Michael Brown',4);
select a.id,a.name,b.name as manager_name from sabari.employees a
left join sabari.employees b
on a.manager_id = b.id;
##Cross join:-
create table sabari.ipl(team_name varchar(50) primary key);
insert into sabari.ipl values
('CSk'),
('RCB'),
('MI'),
('KKR'),
('SRH'),
('DC'),
('GT'),
('PBKS'),
('LSG'),
('RR');
select* from sabari.ipl a
cross join sabari.ipl b;
select * from sabari.ipl a
join sabari.ipl b
on a.team_name > b.team_name
where (a.team_name = 'MI' and b.team_name = 'CSK')
or(b.team_name = 'MI' and a.team_name = 'CSK');
##Windows Function :- -> rank,dense_rank,row
select * from(
select first_name,salary,
rank() over (order by salary desc) as rk,
dense_rank() over (order by salary desc) as dk,
row_number() over (order by salary desc) as rn from sabari.worker)a
where dk = 2;
select * from(
select first_name,salary,
rank() over (partition by department order by salary desc) as rk,
dense_rank() over (partition by department order by salary desc) as dk,
row_number() over (partition by department order by salary desc) as rn from
sabari.worker)a
where dk = 2;
##Row number:-
select * from(
select first_name,salary,
rank() over (partition by department order by salary desc) as rk,
dense_rank() over (partition by department order by salary desc) as dk,
row_number() over (partition by department order by salary desc) as rn from
sabari.worker)a
where rn >0;
select department,
sum(salary) over (partition by department order by salary desc) as sum,
min(salary) over (partition by department order by salary desc) as min,
max(salary) over (partition by department order by salary desc) as max from sabari.worker;
Day 4:-
###CTE -> Common table expressions:-
with temp_table
as
(
select a.id,a.name,b.dept_name from sabari.student a
left join sabari.department b
on a.deptid = b.id
union
select a.id,a.name,b.dept_name from sabari.student a
right join sabari.department b
on a.deptid = b.id
)
select * from temp_table;
-- views:-
create view sabari.fjoin
as
(
select a.id,a.name,b.dept_name from sabari.student a
left join sabari.department b
on a.deptid = b.id
union
select a.id,a.name,b.dept_name from sabari.student a
right join sabari.department b
on a.deptid = b.id
);
-- case statement:-
use sabari;
show databases;
show tables;
set sql_safe_updates = False;
update sabari.worker
set department = 'Python Developer';
update sabari.worker
set dept_name = (case when id = 2 then 'Python Developer'
when id = 2 then 'Python Developer'
when id = 2 then 'Python Developer'
when id = 2 then 'Python Developer'
else 'Python Developer' end);
#Get even and odd rows:-
select * from sabari.worker
where worker_id%2=1;
select * from
(select first_name,salary,
dense_rank() over (order by salary desc) as dk
from sabari.worker) a
where dk%2 = 0;
##combine first & last row:-
select * from
(select first_name,salary from sabari.worker
order by salary desc
limit 1)a
union
select * from
(select first_name,salary from sabari.worker
order by salary asc
limit 1)b;
###constraint:-
# not null:-
drop table constraints;
create table sabari.constraints(id int not null, name varchar (50));
insert into sabari.constraints(id,name) values
(1,'Sabari'),
(2,'Samuel'),
(3,'Saya'),
(4,'Jaseil'),
(5,'Thamarai');
select * from sabari.constraints;
-- modify the column id not null
# unique:-
alter table sabari.constraints
add constraint unique(id);
desc sabari.constraints;
###Check:-
alter table sabari.constraints
add column age int;
alter table sabari.constraints
add constraint check(age>18);
insert into sabari.constraints(id,name,age) values(6,'Jonas',23);
###Primary constraints:-
drop table department;
create table sabari.department(id int primary key, dept_name
varchar(50));
insert into sabari.department values
(1,'EEE'),
(2,'ECE'),
(3,'CSC'),
(4,'IIT'),
(5,'ITI');
drop table sabari.student;
create table sabari.student(id int, name varchar(50), deptid int ,
foreign key(deptid)
references department(id));
insert into sabari.student(id,name,deptid) values
(1,"Sabari",4),
(2,"Samuel",3),
(3,"Saya",2),
(4,"Jaseil",1),
(5,"Thamarai",null);
select * from student;
# withoout deleting foreign key , we cannot delete primary key
delete from sabari.department where id = 1;
drop table sabari.student;
drop table sabari.department;
# pivot table -> converting rows to columns:-
-- name products
-- sabari choc*500
-- sam toys*599
-- priya dressess*1099
-- spend
-- sabari 500
-- name choc toys dressess
-- sabari yes no no
-- sam no yes no
-- priya no no yes
create table sabari.pivot_case(name varchar(50),products
varchar(50));
insert into sabari.pivot_case(name,products) values
('Sabari','Chocolate'),
('Samuel','Toys'),
('Thamarai','Dresses'),
('Saya','Food');
select name,
max(case when products = 'Toys' then 'YES' else 'NO' end)as TOYS,
max(case when products = 'Dresses' then 'YES' else 'NO' end)as
DRESSES,
max(case when products = 'Chocolate' then 'YES' else 'NO' end)as
CHOCOLATE,
max(case when products = 'Food' then 'YES' else 'NO' end)as FOOD
from sabari.pivot_case
group by name;
-- SQL COMPLETED --