SQL 3ri
SQL 3ri
---Describe---
desc employees;
---Concatenation---
select first_name||last_name from employees;
---Distinct Keyword---
select DISTINCT job_id from employees;
select first_name,last_name from employees where salary>15000;
---Between Operator---
select first_name,last_name,salary from employees where salary BETWEEN 10000 and 15000;
---And Operator---
select first_name,last_name,salary from employees where salary>10000 and salary<15000;
---in operator---
select first_name,job_id from employees where job_id in('AD_PRES','AD_VP','IT_PROG');
---Like Operator---
select first_name from employees where first_name Like 'S%';
---28/09/2022---
---not operator---
select first_name,job_id from employees where job_id!='IT_PROG';
---order by--
select first_name from employees order by first_name;
---lower---
select lower(first_name) from employees;
---upper---
select upper(first_name) from employees;
---length---
select LENGTH(first_name) from employees;
---29/09/2022---
select * from employees;
select upper('sql') from dual;
---initcap---
select upper(first_name),lower(last_name),initcap(email) from employees;
---Substring operator---
select substr('Welcome to java',5,3) As name from dual;
---instring operator---
select instr('vqvaq','q',3) from dual;
---left padding---
select lpad('java',10,'ABCD') from dual;
---right padding---
select rpad('java',10,'ABCD') from dual;
---Trim operator---
---leading---
select trim(leading '0' from '100000100000100') from dual;
---trailing---
select trim(trailing '0' from '0000010000000') from dual;
----both---
select trim(both '0' from '0000010000000') from dual;
---Replace Operator---
select replace('java','j','c') from dual;
---Numeric Function---
---round operator---
select round(45.9866,3) from dual;
---trunc operator---
select trunc(45.9866,3) from dual;
---mod Operator---
select mod(47,3) from dual;
---Ceio Operator---
select ceil(23.45) from dual;
---floor operator---
select floor(23.45)from dual;
---round Operator---
select round(first_name) from employees;
---30/09/2022---
---Date Function---
select sysdate from dual;
---add months---
select add_months(sysdate,6) from dual;
---Next Day---
select NEXT_DAY(SYSDATE, 'TUESDAY') from dual;
---Last Day---
select LAST_DAY('01-03-2022') from dual;
---Round Operator---
select round(sysdate,'dd') from dual;
---nvl operator---
select nvl(null,7) from dual;
---nvl2 Operator---
select first_name,COMMISSION_PCT, nvl2(COMMISSION_PCT,20,10) from employees;
---null if---
select nullif(5,5)from dual;
---Coalesce Operator---
select COALESCE(null,5,7,null) from dual;
---Assignment Questions---
---04/10/2022---
---max operation---
select max(salary) from employees;
---min Operation---
select min(salary) from employees;
---Count Operation---
select count(salary) from employees;
---sum operation---
select sum(salary) from employees;
---average Operation
select round(avg(salary)) from employees;
---Display min ans max salary from employee from Manager_id in ascending order
select min(salary), max(salary),manager_id
from employees
group by manager_id
order by manager_id;
select sum(salary),job_id
from employees
group by job_id
order by job_id;
---Display highest,lowest,sum and average of salary of all employees and label columns as
Maximum,minimum,Sum and Average and round your result to nearest whole number
---Deleting table----
drop table Student;
desc Student1;
---Assignment 4---
select max(salary),min(salary)
from employees
where job_id='IT_PROG';
select floor(avg(salary)),count(employee_id)
from employees
where department_id=90;
select count(employee_id),hire_date
from employees
group by hire_date
order by hire_date;
select sum(employee_id),department_id
from employees
group by department_id
order by department_id;
---06/10/2022---
---Decode----
SELECT first_name, job_id, salary,
DECODE(job_id, 'AD_PRES',salary+10000,'AD_VP', salary+20000,salary)
as Updated_salary
FROM employees;
select first_name,department_id,
decode(department_id,90,salary+(salary*2/100),100,salary-(salary-3/100),salary)
as Updated_salary
from employees;
---DML Operations---
desc Student;
---alter add---
alter table Student add (birth_date date);
desc Student;
---alter modify---
alter table Student modify(name varchar2(30));
desc Student;
---alter drop---
alter table Student drop(birth_date);
---Update data---
update Stud set name='Akshay Kumar' where id=1;
select * from Stud;
---Delete data---
delete from Stud where id=1;
select * from Stud;
delete from Stud;
---Rollback---
ROLLBACK;
---truncate table---
truncate table new_employee;
---drop table---
drop table new_employee;
---07/10/2022---
---Transaction Control---
desc Student;
---RollBack----
insert into Student values(1,'abc');
insert into Student values(2,'asd');
rollback;
----Commit-----
insert into Student values(1,'abc');
commit;
rollback;
delete from Student;
---Savepoint Creation---
insert into Student values(1,'abc');
SAVEPOINT A;
SAVEPOINT B;
ROLLBACK TO B;
---Comment Table-------
comment on table Student is 'To maintain record of students';
---applying filter---
select * from user_tab_comments where table_name='STUDENT';
---view all tables and comments present in system---
select * from all_tab_comments;
---applying filter---
select * from all_tab_comments where table_name='STUDENT';
---Comment on column----
comment on column Student.name is 'To maintain names of the Students';
---applying filter---
select * from user_col_comments where column_name='NAME';
---applying filter---
select * from all_col_comments where table_name='STUDENT' and column_name='NAME';
------------Constraints-----------
---Primary key---
create table clas(id number(10),name varchar(20),constraint c_pk PRIMARY KEY(id));
desc clas;
----Foreign Key----
create table sqlbatch(sid number(10),name varchar(20),
desc sqlbatch;
---11-10-2022---
---Not Null Constraint---
create table employee(id number(10),name varchar(20),email varchar2(20),salary number(10)not
null);
---Check Constraint---
create table emp5
(
id number(10),
name varchar(20) not null,
marks number constraint c check(marks>70)
);
---create table student having column id primark key, name not null, marks having greater that 70 and
email unique
---create table class ifo and make foreign key cust_id having referenec to id column of student table---
---Set Operations---
/*Rules:
1)You have to mention same numbers of columns in both statements.
2)You have to mention columns in both statements which are having same data types.
*/
---Creating First Table---
create table survey_1
(name varchar(10),
City Varchar(20),
occupation varchar(10),
salary number(10)
);
---insert data----
insert into survey_2 values('raj-d','pune','SE',2000);
insert into survey_2 values('soname-d','pune','SE',20000);
insert into survey_2 values('deep','pune','SE',20000);
insert into survey_2 values('kriya','pune','SE',20000);
---Union Operation---
---Prints all values and avoides duplication
select name,city from survey_1
union
select cust_name,city_name from survey_2;
---Union All---
---Prints all values alon with duplication
select name,city from survey_1
union all
select cust_name,city_name from survey_2;
---Intersect---
---Prints only common values from two tables
select name,city from survey_1
intersect
select cust_name,city_name from survey_2;
---Practice---
---table1---
create table students
(id number(10),
s_name varchar(20),
s_city varchar(15)
);
---table2---
create table class123
(c_id number(10),
c_name varchar(20)
);
---union----
select id,s_name from students
union
select c_id,c_name from class123;
---union all----
select id,s_name from students
union all
select c_id,c_name from class123;
---intersect----
select id,s_name from students
intersect
select c_id,c_name from class123;
---printing all job id from employees and jobs table along with duplicate values---
select job_id from employees
union all
select job_id from jobs;
---12-10-2022---
---View---
select * from employees;
select * from departments;
---Creating View---
create view v2 as
select employee_id from employees
union
select DEPARTMENT_ID from departments;
---Cascade----
create table customers(
cust_id number(10) primary key,
cust_name varchar(100)
);
--subquieries
--display the name of employee whose salary is greater than Neena's salary;
---Dispaly first_name and job_id from emplyees who are not managers
select first_name,job_id from employees
where job_id!=(select job_id from employees where job_id='FI_MGR' );
---Display fisrt name,job_id and hire_date of employees who are hired before 2016
SELECT first_name, hire_date
FROM employees
WHERE hire_date
----Assignment----
---1)Display the name and hire date of every employee who was hired in 2005.---
SELECT first_name, hire_date
FROM employees
WHERE hire_date LIKE '%05';
---13-10-2022---
---Display first name, commission and department id whose department id is 80---
select first_name,commission_pct,department_id from employees where commission_pct in(
select commission_pct from employees where department_id=80);
---Any Operator---
---Display name of employees whose salary is greater then steven or neena or lex---
select first_name,salary from employees where salary >any (
select salary from employees where first_name in('Steven','Neena','Lex' ));
---Join---
---inner join: Used to print common data from two tables
---Create table one with primary key
create table customers(
cust_id number(10) primary key,
cust_name varchar(100)
);
---Inner Join---
select c.cust_id,o.cust_id,o.order_id,o.item
from Customers c ---alias c is used for customers table
inner join orders o ---alias o is used for orders table
on c.cust_id=o.cust_id;
---Right Join---
select c.cust_id,o.cust_id,o.order_id,o.item
from Customers c ---alias c is used for customers table
right join orders o ---alias o is used for orders table
on c.cust_id=o.cust_id;
---14-10-2022---
---Joins---
drop table employee;
select * from employee;
---Craeting First Table---
create table employee
(emp_id number(10) primary key,
name varchar(50) );
---Inserting Values---
insert into employee values (1,'Anil');
insert into employee values (2,'Shripal');
insert into employee values (3,'Kriya');
insert into employee values (4,'Raj');
insert into employee values (5,'Rahul');
---Inserting Values---
insert into empDetails values(1,33,'Pune',40000);
insert into empDetails values(2,40,'Nasik',40000);
insert into empDetails values(3,56,'Pune',40000);
insert into empDetails values(4,28,'Mumbai',40000);
insert into empDetails values(8,37,'Delhi',40000);
insert into empDetails values(10,43,'Jaipur',40000);
--Inner join---
select e.emp_id,e.name,d.age,d.address,d.salary
from employee e
Inner join empDetails d
on(e.emp_id=d.emp_id);
--left join---
select e.emp_id,e.name,d.age,d.address,d.salary
from employee e
left join empDetails d
on(e.emp_id=d.emp_id);
--right join---
select e.emp_id,e.name,d.age,d.address,d.salary
from employee e
right join empDetails d
on(e.emp_id=d.emp_id);
--cross join
select e.emp_id,d.emp_id,e.name,d.age,d.address,d.salary
from employee e
cross join empDetails d;