Data Bases Lab 7-8
Data Bases Lab 7-8
--current date
--select getdate()
--Task no.2 (Select data as above (task no. 1), but the employment date should be
displayed as dd-mm
yyyy (i.e., use the Italian datetime format). The result should be as follows:)
--
--Task no.3 (Select the identifiers of employees and number of rentals for each
employee who has
made at least one rental:)
--
--Task no.4 (Select columns like above (i.e., in point no.3). Furthermore, for each
row display First
Name and Last Name. Note – use INNER JOIN)
--
--Task no.5 (At first, insert data for new client (i.e., for Kamil Maliszewski) as
below:
Afterwards, select clients – including their indetifiers, Last Names and First
Names – who
have not rented anything. Advice – use LEFT (or RIGHT) OUTER JOIN:)
--
--if we use only inner join here, there will be no Maliszewski, as he has no cars
rented, so is not in Rent table
--we need to use right outer join: take what is inner and something additional from
Client (in that case new row with Maliszewski)
--where excludes some rows
--if we group data, then only we can use having (can't be used without grouping)
--insert into Client values ('Kamil', 'Maliszewski', null, null, 'Legionowa', '3',
'Warszawa', '01-200', '555-233-256', '44-342-116' );
--Task no.6 (Select cars (including CarID, Brand, Model, Color, Engine capacity and
Number of
rentings), which have been never rented)
--
--Task no.7 (Select First Names and Last Names of clients and employees whose last
names ending
with ski. The result can contain identical rows, since client(s) and employee(s)
can have the
same first and last names. Sort data by last name (in an ascending order))
--
--use union all when two identical people can occur, union when there is no second
occurences
--Task no. 8 (Select cars (CarID, Brand and Model) that no client has ever rented.
Use subquery and
predicate IN:)
--
--Task no. 9 ( Select data for employees (FirstName, LastName, Department, Position
and Salary)
whose salary is higher than the value of average salary. Use subquery:)
--
--Task no. 10 (Select data for employees (FirstName, LastName, Department, Position
and Salary)
who earn more than average salary in each department)
--
--exam task
select FirstName, LastName, Department, Position, Salary
from Employee
--we must use all keyword to compare salary to every average (it has to be bigger
than the highest average)
where salary > all
(
select avg(Salary)
from Employee
group by department
)
--Task no. 11 (Select data for employees (FirstName, LastName, Department, Position
and Salary)
who earn more than average salary in any department:)
--
--exam task
select FirstName, LastName, Department, Position, Salary
from Employee
--we must use any keyword to compare salary to any average (has to be bigger than
the smallest average)
where salary > any
(
select avg(Salary)
from Employee
group by department
)
--Task no. 12 (Create view which contains data from columns: First Name, Lasta
Name, Company,
TaxID and City for each client who has got a company. View should be saved with the
name
as follwos view_FirstLetterOfName_LastName (each student using own personal
data). )
--
create view task_no_12 as --can be done only once, then the instance is already
created
select FirstName, LastName, Company, TaxID, City
from Client
where Company is not null
--removing data
delete New_Car
--2nd select
select *
from New_Car
--DROP - DDL
--1st select
select *
from New_Place
--Task no. 15 (delete clients which city starts with 'W' letter)
--1st select
select *
from New_Client
--removing data
delete New_Client
where City like 'w%'
--2nd select
select *
from New_Client
--Task no. 16
--1st select
select *
from New_Employee
--2nd select
select *
from New_Employee
--Task no. 17
declare @noOfSomething int
select @noOfSomething = count(*)
from Car
where Brand = 'Mercedes'
print 'In our company we have got ' +
--convert(varchar(5), @noOfSomething)
cast(@noOfSomething as varchar(5))
+ ' Mercedeses.'
--Task no. 18
declare @avgSal decimal
select @avgSal = avg(salary)
from Employee
--Task no. 19
CREATE PROCEDURE CheckClientRentals
@ClientID INT
AS
BEGIN
IF EXISTS (SELECT 1 FROM Rent WHERE ClientID = @ClientID)
BEGIN
DECLARE @CarCount INT
SELECT @CarCount = COUNT(*) FROM Rent WHERE ClientID = @ClientID
PRINT 'The customer with the given ID rented: ' + CAST(@CarCount AS
VARCHAR(10)) + ' cars.'
END
ELSE
BEGIN
PRINT 'The client with the given ID has not rented anything!'
END
END
GO
--Task ?
--how to create a procedure
create procedure RetMaxIncome
as
declare @salVal decimal
select @salVal = max(Salary)
from Employee
print 'Maximal salary in our company = '
+ convert (varchar(20), @salVal) + '(That is info from PROC)'