0% found this document useful (0 votes)
6 views12 pages

SQL

FGR

Uploaded by

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

SQL

FGR

Uploaded by

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

#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

You might also like