Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

use org;;

select * from Worker


select * from Bonus
select * from Title
//innner join
SELECT w.Worker_ID,w.FIRST_NAME,b.BONUS_AMOUNT,b.WORKER_ID from Worker w
inner JOIN Bonus b
ON w.WORKER_ID=b.WORKER_ID;
//left join

SELECT w.Worker_ID, w.FIRST_NAME, b.BONUS_AMOUNT, b.WORKER_ID from


Worker w left JOIN Bonus b
ON w.WORKER_ID=b.WORKER_ID

//right join

SELECT w.Worker_ID, w.FIRST_NAME, b.BONUS_AMOUNT, b.WORKER_ID from


Worker w right JOIN Bonus b
ON w.WORKER_ID=b.WORKER_ID

//full join

SELECT w.Worker_ID, w.FIRST_NAME, b.BONUS_AMOUNT, b.WORKER_ID from


Worker w full JOIN Bonus b ON w.WORKER_ID=b.WORKER_ID
//cross join
SELECT w.Worker_ID, w.FIRST_NAME, b.BONUS_AMOUNT, b.WORKER_ID from
Worker w cross JOIN Bonus b
// Natural join
SELECT * FROM Worker NATURAL JOIN WorkerClone

SELECT * FROM worker NATURAL JOIN Bonus

//self join

SELECT W.FIRST_NAME, w.SALARY, w.Worker_ID


FROM Worker w join worker ww ON w.WORKER_ID = ww.worker_id

//

select * from tblAuthors


select * from tblbooks

//sub query with id<=500

select * from tblAuthors t,tblBooks tt where t.Id IN(select id from tblBooks where
id<=500)

// joins with id<=500

SELECT t.Author_name,t.country,t.Id,tt.Auhthor_id,tt.Edition,tt.Id,tt.Price
FROM tblAuthors t inner join tblBooks tt ON tt.id<=500

// conditions more

select * from tblAuthors t,tblBooks tt where t.Id IN(select id from tblBooks where
id<=500 and Price <60)

SELECT t.Author_name,t.country,t.Id,tt.Auhthor_id,tt.Edition,tt.Id,tt.Price
FROM tblAuthors t inner join tblBooks tt ON tt.id<=500 and tt.Price<60

//proof exist

select * from tblAuthors t,tblBooks where not exists(select * from tblBooks tt


where tt.id=t.Id )

//

Select w.Worker_ID, w.FIRST_NAME, w.SALARY,ISNULL(BONUS_AMOUNT,0) as BONUS_AMOUNT


from Worker w left join
Bonus B on w.WORKER_ID = b.WORKER_ID

//

//left join

SELECT t.Author_name,t.country,t.Id
FROM tblAuthors t left join tblBooks tt ON tt.Id=t.Id where tt.id is null

CREATE Table tblAuthors


(
Id int identity primary key,
Author_name nvarchar(50),
country nvarchar(50)
)
CREATE Table tblBooks
(
Id int identity primary key,
Auhthor_id int foreign key references tblAuthors(Id),
Price int,
Edition int

Declare @Id int


Declare @i int
Set @Id = 1

While @Id <= 700


Begin
Insert Into tblAuthors values ('Author - ' + CAST(@Id as nvarchar(10)),
'Country - ' + CAST(@Id as nvarchar(10)) + ' name')
Print @Id
Set @Id = @Id + 1
set @i=@i+1
End

Declare @RandomAuthorId int


Declare @RandomPrice int
Declare @RandomEdition int

Declare @LowerLimitForAuthorId int


Declare @UpperLimitForAuthorId int

Set @LowerLimitForAuthorId = 1
Set @UpperLimitForAuthorId = 600

Declare @LowerLimitForPrice int


Declare @UpperLimitForPrice int

Set @LowerLimitForPrice = 50
Set @UpperLimitForPrice = 100

Declare @LowerLimitForEdition int


Declare @UpperLimitForEdition int

Set @LowerLimitForEdition = 1
Set @UpperLimitForEdition = 10

Declare @count int


Set @count = 1

While @count <= 600


Begin

Select @RandomAuthorId = Round(((@UpperLimitForAuthorId -


@LowerLimitForAuthorId) * Rand()) + @LowerLimitForAuthorId, 0)
Select @RandomPrice = Round(((@UpperLimitForPrice - @LowerLimitForPrice) *
Rand()) + @LowerLimitForPrice, 0)
Select @RandomEdition = Round(((@UpperLimitForEdition - @LowerLimitForEdition) *
Rand()) + @LowerLimitForEdition, 0)

Insert Into tblBooks values (@RandomAuthorId, @RandomPrice, @RandomEdition)


Print @count
Set @count = @count + 1
End

//

You might also like