Skip to content

Join, union, having clause, update, delete, aliasing and partition

Notifications You must be signed in to change notification settings

obhujerpencil/SQL_Intermediate

Repository files navigation

SQL_Intermediate

Join, union, having clause, update, delete, aliasing and partition

Initial Tables

Initial_Table_1 Initial_Table_2

Venn representation of join

Sql_Join

WhatsApp Image 2022-08-29 at 9 31 45 AM

Inner join

select FirstName,Job,Salary from EmployeeDemographics as demo
inner join EmployeeSalary as sal
on demo.EmployeeID=sal.EmployeeID

inner_join

Full outer join

select FirstName,Job,Salary from EmployeeDemographics as demo
full outer join EmployeeSalary as sal
on demo.EmployeeID=sal.EmployeeID

full_outer_join

Right outer join

select FirstName,Job,Salary from EmployeeDemographics as demo
right outer join EmployeeSalary as sal
on demo.EmployeeID=sal.EmployeeID

right_outer_join

Left outer join

select FirstName,Job,Salary from EmployeeDemographics as demo
left outer join EmployeeSalary as sal
on demo.EmployeeID=sal.EmployeeID

left_outer_join

Join

Join is inner join by default.

select FirstName,Job,Salary from EmployeeDemographics as demo
join EmployeeSalary as sal
on demo.EmployeeID=sal.EmployeeID

join

Union

New Table

-- Creating new Table to show Union.

create table EmployeeDemographics2(
EmployeeID int,
FirstName varchar(20),
LastName varchar(20),
Age int,
Gender varchar(10)
)
insert into EmployeeDemographics2 values
(1012,'Mukta','Chakraborty',21,'Female'),
(1013,'Poonam','Saha',20,'Female')

new_table

Opertion

select FirstName,Age from EmployeeDemographics
union
select FirstName,Age from EmployeeDemographics2
order by Age asc

Union

Case statement

Case Statement is like conditional statement. It can be used to create additional columns based on logic.

select FirstName,Age,Salary,
case
when salary<50000 then salary+(salary*0.1)
else salary
end as Salary_Update
from EmployeeDemographics dem
join EmployeeSalary sal 
on dem.EmployeeID=sal.EmployeeID

case

Having clause

Where clause throws error while working with aggregate like min, max & count so we need to use the having clause

Use group by with having clause.

select Job,count(Job) as Vaccancies 
from EmployeeSalary
group by Job
having avg(salary)>50000
order by count(job) desc

Having

Update

update EmployeeDemographics
set age=22 
where EmployeeID=1001

select * from EmployeeDemographics

Update

Delete

Delete from EmployeeDemographics 
where EmployeeID=1001

select * from EmployeeDemographics

delete

Partion

Group satements cuts down the number of rows by rolling them up but with the help of partition we can get all the rows and aggregate things corrosponding to them.

select FirstName,Gender,
count(EmployeeID) over (partition by Gender) as Total_Gender
from EmployeeDemographics

partition

Aliasing

select FirstName+' '+LastName as Full_Name, Age 
from EmployeeDemographics

Alia singing in real life

download

Aliasing in Database

aliasing

Bye bye.,...

lol

About

Join, union, having clause, update, delete, aliasing and partition

Topics

Resources

Stars

Watchers

Forks

Languages