0% found this document useful (0 votes)
12 views3 pages

1516 Exam SQL February SOLUTION

The document contains instructions for a database exam with three questions. Question 1 asks to write SQL for creating a database table called Apps. Question 2 asks to write a SQL query to select category names and descriptions from categories where products are supplied from Italy. Question 3 asks to write a stored procedure to calculate the average discount for a given product ID and provide the code to call it, passing in 102 as 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)
12 views3 pages

1516 Exam SQL February SOLUTION

The document contains instructions for a database exam with three questions. Question 1 asks to write SQL for creating a database table called Apps. Question 2 asks to write a SQL query to select category names and descriptions from categories where products are supplied from Italy. Question 3 asks to write a stored procedure to calculate the average discount for a given product ID and provide the code to call it, passing in 102 as 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
AUTOMÁTICA Signature:

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

Column Name Details Domain Value Examples

Name String of max length 60. This value must be necessarily ‘Gravity Panda’, ‘Fluffy Hen’, ‘Evernote’
provided. It has to be a unique value in the table.

CompanyName String of max length 60. This value must be necessarily. It is a ‘EA’, ‘Apple’, ‘OMDSource’
foreign key of the Companies table referencing its primaryKey
CompName.

TypeApp String of fix length 3. It can not be null. All characters must be ‘GAM’, ’PRO’, ‘CAS’
upper case letters (A-Z)

Price Money value. Default value is 0.0 It can not be negative. 2.99, 0.0, 50.99

Store String of max length 20. It can not be null. The unique posible ‘AppStore’,’Both’, ‘PlayStore’
values are ‘AppStore’, ‘PlayStore’, and ‘Both’

Date_of_publish Date of publish. It can not be a higher date than the current date. ‘2015/01/03’, ‘2002/12/11’
It can be null.

AppId Integer. Autoincremental value which start in 0 and has an step 0, 100, 200, 300,…
of 100. It is the primary key.

CREATE TABLE Apps(


Name varchar(60) not null constraint UQ_name UNIQUE,
CompanyName varchar(60) not null CONSTRAINT fk_company FOREIGN KEY REFERENCES
Companies(CompName),
TypeApp char(3) not null CHECK (TypeApp like ‘[A-Z][A-Z][A-Z]’),
Price money not null DEFAULT 0.0 CHECK( Price>=0),
Store varchar(20) not null CHECK( Store IN (‘AppStore’, ‘PlayStore’,’Both’)),
Date_of_publish date null CHECK(Date_of_publish<getDate()),
AppId Integer identity(0,100) CONSTRAINT pk_Apps Primary key
)

II. This question relates to the Northwind database (See over for its E-R diagram). Write an SQL query to show
the CategoryName and the Description of all the product Categories which has a Supplier from the country
‘Italy’. (35 points).
Notes:
1. Each product has an specific category.
2. You must use 3 tables in your query.

1
(Esta pregunta está relacionada con la base de datos Northwind (Mirad el diagrama E-R). Escribid una consulta SQL para
obtener el CategoryName y el Description de Categories, de los productos que son vendidos por Suppliers cuyo Country es
‘Italy’
Notas:
1. Cada producto tiene una categoría.
2. Se deben usar tres tablas en la consulta.

SELECT CategoryName, Description


FROM Categories
WHERE CategoryId IN (
SELECT CategoryId
FROM products INNER JOIN Suppliers ON Products.SupplierID=Suppliers.SupplierID
WHERE Country='Italy'
)

III. This question relates to the Northwind database (See its E-R diagram). Write a stored procedure named
AverageDiscount that shows the average discount of Order Details for a specific ProductID passed as
parameter (ProductID is an integer). Then, write the SQL sentence that invokes the stored procedure and that
passes it the actual input parameter 102. (30 points).
(Esta pregunta está relacionada con la base de datos Northwind (Mirad el diagrama E-R). Escribid un procedimiento
almacenado llamado “AverageDiscount” que muestre el descuento medio (discount) de [Order Details] cuyo ProductID sea
el que se pasa como parámetro al procedimiento (ProductID es un entero). A continuación, escribid la sentencia SQL que
invoca este procedimiento almacenado, pasándole como parámetro el valor 102.
CREATE PROCEDURE AverageDiscount @productID int
AS
SELECT AVG(Discount)
FROM [Order Details]
WHERE ProductID =@productID
RETURN
EXEC AverageDiscount 102

2
NORTHWIND Database
Schema

You might also like