0% found this document useful (0 votes)
8 views

1516 Exam SQL January SOLUTION

The document describes a database exam with three questions. Question 1 asks to write SQL code to create a Films database table with several columns. Question 2 asks to write a SQL query to retrieve customer phone numbers and employee names for customers who ordered from a specific shipper. Question 3 asks to write a stored procedure that calculates order totals matching a discount parameter and invoke it, passing the parameter.

Uploaded by

apereb07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

1516 Exam SQL January SOLUTION

The document describes a database exam with three questions. Question 1 asks to write SQL code to create a Films database table with several columns. Question 2 asks to write a SQL query to retrieve customer phone numbers and employee names for customers who ordered from a specific shipper. Question 3 asks to write a stored procedure that calculates order totals matching a discount parameter and invoke it, passing the parameter.

Uploaded by

apereb07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DATABASES

Third Exam (b) January 26th, 2016


Duration: 30 minutes.

First Name:
Second Name:
Departamento de Ingenierías
DNI.:
ELÉCTRICA y de SISTEMAS Y
Signature:
AUTOMÁTICA

I. Write the Transact SQL code necessary for creating the following DB table named Films: (35 points)
(Escribe el código en Transact SQL necesario para crear la siguiente tabla en la base de datos llamada Films)

Column Name Details Domain Value Examples

Name String of max length 60. This value must be necessarily ‘Lo que el viento se llevó’, ‘Matrix II’, ‘A-Cuerdos’
provided and (Name, DirectorID) is the primary key

DirectorID String of max length 60. This value must be necessarily ‘Luis Buñuel’, ‘Steven Spielberg’, ‘El fary’
provided (Name, DirectorID) is the primary key. It is a foreign
key of the Directors table referencing its primaryKey DirId.

Date_of_premiere Date of the premiere. It can not be null and the default value is ‘15/01/2015’,’03/10/2002’
the current date.

FilmLanguage String of max length 20. It can not be null. ‘English’,’Japanese’

FilmLength Integer with the length in minutes of the film. The value can 90, 100, 120
not be negative and can not be bigger than 200. It can be null

FilmNumber Number which store the inserted order into the dataset. It has 1,2,3,4,…
to be an auto incremental value.

FilmRank Number between 0 and 99 with two decimals digits This value 54.75, 99.99, 4.12
must be >=0 and can be null

CREATE TABLE Films(


Name varchar(60) not null,
DirectorID varchar(60) not null constraint FK_Director foreign key REFERENCES Directors(DirId),
Date_of_Premiere date not null default getdate(),
FilmLanguage varchar(20) not null,
FilmLength int null check (FilmLength>0 and FilmLength<200),
FilmNumber int identity(1,1),
FilmRank numeric(2,2) check (FilmRank>0) null,
constraint PK_Films primary key(Name,DirectorID)
)

II. This question relates to the Northwind database (See over for its E-R diagram). Write a SQL query to obtain
the Phone of all the Customers who made an Order using the Shipper “Speedy Express”and also the name
and last name of the employees who carried out the sales (35 points).
Notes:
1. Orders.ShipVia is the foreign key of the Shippers table.
2. The name of the Shipper is specified in the field Shippers.CompanyName.
3. You should use JOIN operations.

1
4. The output must contain three columns: Customers.Phone, Employees.Name and
Employee.LastName.
(Esta pregunta está relacionada con la base de datos Northwind (Mirad el diagrama E-R). Escribid una consulta SQL para
obtener el Phone de todos los Customers que hayan realizado algún pedido a través de la shipper “Speedy Express” y además
el nombre y apellidos de los vendedores que realizaron las ventas.
Notas:
1. Orders.ShipVia es la clave ajena de la tabla Shippers.
2. El nombre de la empresa de envío es especificado en el campo Shippers.CompanyName
3. Se recomienda utilizar operadores JOIN para realizar la consulta
4. La salida debe estar formada por tres columnas: Customers.Phone, Employees.Name y Employee.LastName

SELECT Customers.Phone, Employees.FirstName, Employees.LastName


FROM Customers INNER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
INNER JOIN Shippers ON Orders.ShipVia=Shippers.ShipperID
INNER JOIN Employees ON Employees.EmployeeID=Orders.EmployeeID
WHERE Shippers.CompanyName='Speedy Express';

III. This question relates to the Northwind database (See its E-R diagram). Write a stored procedure named
TotalPrice that shows the multiplication of [Order Details].UnitPrice and [Order Details].Quantity which
discount is equal to the real number that gets passed to the procedure sorted by the OrderID field ins
ascending order . Then, write the SQL sentence that invokes the stored procedure and that passes it the actual
input parameter 0.15. (30 points).
(Esta pregunta está relacionada con la base de datos Northwind (Mirad el diagrama E-R). Escribid un procedimiento
almacenado llamado “TotalPrice” que muestre la multiplicación de [Order Details].UnitPrice y [Order Details].Quantity
cuyo descuento sea igual al valor real que se pasa como parámetro ordenados por el campo OrderID de manera ascendente.
A continuación, escribid la sentencia SQL que invoca este procedimiento almacenado, pasándole como parámetro el valor
0.15.
CREATE PROCEDURE TotalPrice @discount real
AS
SELECT UnitPrice*Quantity
FROM [Order Details]
WHERE discount =@discount
ORDER BY OrderID asc
RETURN
EXEC TotalPrice 0.15

2
NORTHWIND Database
Schema

You might also like