0% found this document useful (0 votes)
3 views5 pages

SQL Class 5

Ok

Uploaded by

rahul2050
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
3 views5 pages

SQL Class 5

Ok

Uploaded by

rahul2050
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 5

-- CASE WHEN STATEMENT

SELECT *,
CASE WHEN Marks >= 80 THEN 'Top_Student'
WHEN Marks >= 60 AND Marks < 80 THEN 'Average Student'
ELSE 'Below Average'
END as 'Bin'
from Student_Details

SELECT *,
CASE WHEN Marks >= 80 THEN 'Top Students'
WHEN Marks >= 60 and Marks < 80 THEN 'Average Students'
ELSE 'Below Average'
END as Buckets
FROM Student_Details

SELECT * FROM Student_Details

SELECT *,
CASE WHEN subject = 'Maths' THEN Marks + 10
WHEN subject = 'SQL' THEN Marks+ 5
ELSE Marks
END as Total_Marks
FROM Student_Details

SELECT *,
AVG(Marks) OVER(Partition by Subject) as 'Average_Marks',
RANK() OVER(Partition by Subject order by Marks Desc) as 'RAnk',
CASE WHEN Marks >= 80 THEN 'Top_Student'
WHEN Marks >= 60 AND Marks < 80 THEN 'Average Student'
ELSE 'Below Average'
END as 'Bin'
from Student_Details

/*

TRANSACTION CONTROL LANGUAGE -->

1) Commit - This is is used to save transactions permanently


2) Rollback - This will restores the system tom its previous state prior the
changes you made to database.

*/
Select * FROM Student_Details

Begin Transaction
INSERT INTO Student_Details Values ('11','Amit','SQLw','99')

commit

Rollback

/*

DATA CONTROL LANGUAGE -->

1) GRANT - It is used to grant privaledges (Permissions) to the specific user .


2) REVOKE - It is used to revoke the permissions from the user

*/

GRANT SELECT ,UPDATE, INSERT,DELETE ON Student_Table to Akshay;


GRANT SELECT ON Student_Table to Akshay;
REVOKE SELECT, UPDATE, INSERT , DELETE ON Student_Table to Akshay;

/*

JOINS - It is used to join the tables


whenever we want to fetch the data from more than one table then joins are used

1) Inner Join
2) Outer Join
i) Left
ii) Right
iii) Full Outer join
3) Cross join
4) Self Join

*/

CREATE TABLE Dept_tbl (


Dept_id int Primary Key,
Dept_Name varchar(20),
Dept_Location varchar(20),
Dept_Head varchar (20)
)

CREATE TABLE Emp_tbl (


Eid int Primary Key,
Name varchar(20),
Salary int,
Dept_id int references Dept_tbl(Dept_id)
)

INSERT INTO Dept_tbl Values ('1','IT','Hyderabad','Rama'),


('2','HR','Chennai','Sita'),
('3','Finance','Mumbai','Patel'),
('4','Sales','Amravati','Yadav')

INSERT INTO Emp_tbl Values ('1','Aashrith','19000','1'),


('2','Mahati','20000','2'),
('3','Karthik','25000','3'),
('4','Saamya','18000','2'),
('5','Scott','19000','3'),
('6','James','18000','1'),
('7','Wasif','19000',Null),
('8','Ramesh','18000',Null)

SELECT * FROM Emp_tbl


SELECT * FROM Dept_tbl

-- INNER JOIN
SELECT * FROM Emp_tbl
INNER JOIN Dept_tbl ON
Emp_tbl.Dept_id = Dept_tbl.Dept_id

SELECT D.Dept_id FROM Emp_tbl E


INNER JOIN Dept_tbl D ON
E.Dept_id = D.Dept_id

SELECT Eid, Name, Salary, Dept_Name FROM Emp_tbl E


INNER JOIN Dept_tbl D ON
E.Dept_id = D.Dept_id
where salary > 20000

-- OUTER JOIN
-- 1) LEFT JOIN

SELECT E.Name,E.Salary,D.Dept_Name,D.Dept_Location FROM Emp_tbl E


LEFT JOIN Dept_tbl D ON
E.Dept_id = D.Dept_id

-- 2) RIGHT JOIN

SELECT * FROM Emp_tbl E


RIGHT JOIN Dept_tbl D ON
E.Dept_id = D.Dept_id

SELECT Eid, Name, Salary, Dept_Name FROM Emp_tbl E


RIGHT JOIN Dept_tbl D ON
E.Dept_id = D.Dept_id

-- 3) FULL JOIN

SELECT * FROM Emp_tbl E


FULL JOIN Dept_tbl D ON
E.Dept_id = D.Dept_id

SELECT Eid, Name, Salary, Dept_Name FROM Emp_tbl E


FULL JOIN Dept_tbl D ON
E.Dept_id = D.Dept_id

-- CROSS JOIN - It returns the cartesian products of sets of records from two or
more joined tables
-- Cartesian product -- M*N
select COUNT(*) FROm Emp_tbl
select count(*) FROM Dept_tbl

SELECT * FROM Emp_tbl E


CROSS JOIN Dept_tbl D

SELECT Eid, Name, Salary, Dept_Name FROM Emp_tbl E


CROSS JOIN Dept_tbl D

-- SELF JOIN

CREATE TABLE Self_join_tbl (


Emp_id int,
Name varchar(20),
Mgr_id int
)

INSERT into Self_join_tbl Values ('1','Vishu','3'),


('2','Sekhar','2'),
('3','Kumar',Null),
('4','Raghu','1')

SELECT * FROM Self_join_tbl

/* Team Hierarchy

2
3
1
4

*/

SELECT
E.Name as Employee ,
M.Name as manager
from
self_join_tbl E left join Self_join_tbl M
on E.mgr_id = m.Emp_id

SELECT
E.Name as Employee, M.Name as Manager
FROM
self_join_tbl E left join self_join_tbl M
ON E.Emp_ID = M.mgr_id
--- SUBQUERIES
-- Query inside a Query

SELECT * FROM Emp_tbl


SELECT * FROM Dept_tbl

-- WAQ to find out Employee Details having Second highest max Salary

SELECT * FROM Emp_tbl where Salary = (


SELECT max(Salary) FROM Emp_tbl WHERE Salary < (
SELECT MAx(Salary) FROM Emp_tbl
))

-- WAQ to find out the Name of Emplys working in IT

SELECT Name FROM Emp_tbl Where Dept_id = (


SELECT Dept_id from Dept_tbl where Dept_Name = 'IT'
)

-- WAQ to display employee Details whose salary is less than salary of Employee
Wasif

SELECT * FROM Emp_tbl where Salary <


( Select Salary from Emp_tbl where Name = 'Wasif')

-- WAQ to Display the employee details whose salary is less than maximum salary

-- WAQ to display the minimum salary of Employee Details

---

You might also like