Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Server CC

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Bike stores- DDL

create table Categories(category_id int primary key,


category_name varchar(255) not null)
go
create table Brands(brand_id int primary key,brand_name varchar(255) not null)
go
insert into Categories values(1,'Children Bicycles')
insert into Categories values(2,'Comfort Bicycles')
insert into Categories values(3,'Cruisers Bicycles')
insert into Categories values(4,'Cyclocross Bicycles')
go
insert into Brands values(1,'Electra')
insert into Brands values(2,'Haro')
insert into Brands values(3,'Heller')
insert into Brands values (4,'Pure Cycles')
insert into Brands values (5,' Ritchey')
go

Bike Stores-DRL
SELECT TOP 1 B.brand_name AS Brand_name From Brands B
Join Products P ON B.brand_id=P.brand_id
Order BY P.list_Price desc
go

SELECT P.product_name AS Product_name,


P.model_year AS Model_year
FROM Products P JOIN Categories c ON p.Category_id=C.Category_id
WHERE C.Category_name Like '%children%'
Go

Bike stores-procedure
create proc usp_MaxNoOfProducts
as
begin
select b.brand_name as Brand_Name,c.category_name as Category_Name,p.product_name as
Product_Name
from Brands b,Categories c,Products p
where p.brand_id in
(select p.brand_id,count(p.brand_id)
from Produts p
group by p.brand_id
order by count(p.brand_id) desc
limit 1)
end
GO

Clothing store-DDL
create table Customers(
CustomerId int primary key,
FirstName varchar(20) ,
SurName varchar(20),
CustomerState varchar(20)
)
go
create table Clothing (
ItemCode bigint primary key,
Description varchar(100),
RetailPrice decimal(6,2),
Discount decimal(5,2)
)
go
create table Transactions(
TransactionID int primary key,
TransactionDate datetime,
CustomerId int ,
ItemCode bigint
)
go
alter table Clothing
add unique (Description)
go
alter table Transactions
add foreign key(CustomerId) references Customers(CustomerId)
go
alter table Transactions
add foreign key(ItemCode) references Clothing(ItemCode)
go
Clothing store-DRL
select FirstName+","+SurName as CustomerName
from Customers
where CustomerId in
(select CustomerId from(select Customers.CustomerId, count(Customers.CustomerId) count
from Customers
join Transactions
on Customers.CustomerId= Transactions.CustomerId
group by Customers.CustomerId) a
where a.count>1)
go

select Description,(RetailPrice -(RetailPrice*Discount)) as Cost_Price


from Clothing
go

Clothing store-procedure

create procedure usp_TransactionDetails


as
begin
select a.Firstname, c.RetailPrice as TotalPrice
from Customers a join Transactions b
on a.Customerid=b.Customerid
join Clothing c
on b.ItemCode=c.ItemCode
group by a.Firstname,c.RetailPrice
end

go

Lodging house-DDL

create table room_type(room_type_id varchar(6) primary key,description varchar(100))


GO

create table rate(room_type_id varchar(6) not null ,occupancy int not null,amount
decimal(10,2) default 0)
GO

create table room(room_id int primary key,room_type_id varchar(6),max_occupancy int)


GO

alter table room add foreign key(room_type_id) references room_type(room_type_id)


GO

alter table rate add constraint pk_name primary key(room_type_id,occupancy)


GO

alter table rate add foreign key(room_type_id) references room_type(room_type_id)


GO
Lodging house-DRL

select room_id"Room Number",room_type_id"Room Type" from room where room_id NOT IN


(select r.room_id from room r join booking b on r.room_id=b.room_no)and
room_type_id!="single"
go

select r2.room_type_id as "Room Type",count(r1.room_no) as "No Of Bookings" from


room_type r2 left outer join room r on
(r.room_type_id=r2.room_type_id)
left outer join booking r1 on(r.room_id=r1.room_no) group by r2.room_type_id order by
count(r1.room_no) desc
go

Lodging house-Procedure

create procedure usp_GetBookingCost


as
begin
declare @booking_date datetime
select a.room_id as[Room Number],b.room_type_id as [Room Type],
sum(c.amount) as [Total Booking Cost]
from room a,room_type b,rate c
join rate c
on b.room_type_id=c.room_type_id
join room a
on a.room_type_id=b.room_type_id
where @booking_date=booking.booking_date
group by room_type_id
end
GO

Pet clinic-DDL
CREATE TABLE PetOwner(
OwnerId int PRIMARY KEY,
Name char(50), Surname char(50),
StreetAddress char(100),City char(100),
State char(10),ZipCode int)
GO
CREATE TABLE Pet(
PetId char(10) PRIMARY KEY,
Name char(20),
Kind char(10),Gender char(6), Age int, OwnerId int)
GO
CREATE TABLE ProcedureHistory(
PetId char(10),ProcedureDate date, ProcedureType char(50),Description char(100))
GO
ALTER TABLE PetOwner
ADD CONSTRAINT state_michi
DEFAULT 'Michigan' FOR state
GO
ALTER TABLE Pet
ADD CONSTRAINT fh foreign KEY(OwnerId) REFERENCES
PetOwner(OwnerId)
GO
ALTER TABLE ProcedureHistory
ADD CONSTRAINT fh1 FOREIGN KEY(PetId) REFERENCES Pet(PetId)
GO

Pet clinic-DRL
select a.Name as[Pet Name],a.kind,b.Name as[Owner Name]
from Pet a join PetOwner b on a.OwnerId=b.OwnerId
ORDER BY a.Name ASC
Go
select
a.PetId as petid,
a.Name as NAME,
a.kind as kind,
a.Gender as gender
from Pet a join ProcedureHistory b on a.PetId=b.PetId
where 1=(select MONTH(b.ProcedureDate))
order by a.PetId asc
Go

Pet clinic-procedure
create proc usp_PetProcedureDetais(@ProcedureType varchar(255))
select p.PetID as petid,p.Name as [Pet Name],o.Name as [Owner Name],h.Description as
description
FROM Pet p join PetOwner o on p.OwnerID=o.OwnerID join ProcedureHistory h on
p.PetID=h.PetID
where h.ProcedureType=@ProcedureType
end
Go

Movies box office-DRL


select m.Title,m.Director,m.Year,b.Rating from
Movies m
join BoxOffice b
ON b.Movie_id=m.id
Where m.year between 2000 and 2005 and b.Rating > 5
go
select (title+" in the Year "+str(year,4))
as 'Movie and Release Year' From Movies
go

Movies box offices-procedure


create procedure usp_DirectorWithMaxMovies as
begin

select max(m.Director), m.Title, b.Rating from Movies m join


BoxOffice b on
m.Id = b.Movie_Id
group by m.Director, m.Title, b.Rating
order by m.Title
end

go

You might also like