For The Submission of Your Work
For The Submission of Your Work
From the 2nd question, you should use the database provided in the DBscript.sql file which has the
following database diagram. Please, run the provided script to create tables and insert data into your
database.
2 of 7 Paper No: 1
Question 1:
Create one database and then write SQL statements to create all tables derived from the
ERD given in Picture 1.1. Those tables have to locate under the database that you have just
created with appropriate attributes, primary keys and foreign keys.
NOTE that when creating the SQL commands as request, you MUST keep the name of
tables, relationship and attributes and data type of attributes as SAME as given in the above
ERD.
Attributes have written with underline are Primary Key of each entity.
When submitting the responses for this question, submit only SQL statements for creating
tables with corresponding keys and foreign keys. Do not use “create database” or “use
database_name” statements in your submission.
CREATE TABLE [Students] ( [StudentID] int NOT NULL PRIMARY KEY, [Name] nvarchar(50) NOT
NULL, [Address] nvarchar(200) NOT NULL, [Gender] char(1) NOT NULL ); CREATE TABLE
Teachers ( [TeacherID] int NOT NULL PRIMARY KEY, [Name] nvarchar(50) NOT NULL, [Address]
nvarchar(200) NOT NULL, [Gender] char(1) NOT NULL ); CREATE TABLE [Classes] ( [ClassID] int
NOT NULL PRIMARY KEY, [GroupID] char(6) NOT NULL, [CourseID] char(6) NOT NULL,
[NoCredits] int NOT NULL, [Semester] char(
10) NOT NULL, [Year] int NOT NULL, [TeacherID] int NOT NULL REFERENCES
Teachers(TeacherID) ); CREATE TABLE Attend ( [StudentID] int NOT NULL, [ClassID] int NOT
NULL, [Date] date NOT NULL, [Slot] int NOT NULL, [Attend] bit NOT NULL CONSTRAINT [check]
PRIMARY KEY ([StudentID],[ClassID],[Date],[Slot]) );
3 of 7 Paper No: 1
Picture 1.1
Question 2:
Write a query to display all customers who are ‘Consumer’ and are from Arlington city as
follows:
Picture 2.1
Question 3:
Write a query to display all customers having CustomerName starting with B and placed
orders in December 2017. Display the result by descending order of Segment and then by
ascending order of CustomerName.
select c.ID, CustomerName, Segment,Country,City,c.State,PostalCode,Region from Customer c,
Orders o where c.ID = o.CustomerID and MONTH(OrderDate)=12 and YEAR(OrderDate) =2017 and
CustomerName like 'B%' order by Segment desc, CustomerName
4 of 7 Paper No: 1
Picture 3.1
Question 4:
Write a query to display SubCategoryID, SubCategoryName and the corresponding number
of products (NumberOfProducts) in each sub-category having the number of products
greater than 100, by descending order of NumberOfProducts.
select p.SubCategoryID,s.SubCategoryName,p.NumberOfProducts from SubCategory s, (select
distinct SubCategoryID, count(*) as [NumberOfProducts] from Product group by SubCategoryID) p
where s.ID = p.SubCategoryID and NumberOfProducts >100 group by
p.SubCategoryID,s.SubCategoryName,p.NumberOfProducts order by NumberOfProducts desc
Picture 4.1
Question 5:
Write a query to display ProductID, ProductName, Quantity of all products which have the
highest Quantity in one order.
5 of 7 Paper No: 1
Picture 5.1
Question 6:
6 of 7 Paper No: 1
Picture 6.1
Question 7:
Display 5 products with the highest unit prices and 5 products with the smallest unit prices as
follows:
select * from ( select * from (select top 5 * from Product group by
ID,ProductName,UnitPrice,SubCategoryID order by UnitPrice desc) x union select * from (select top 5
* from Product group by ID,ProductName,UnitPrice,SubCategoryID order by UnitPrice ) y) A order by
UnitPrice desc
Picture 7.1
Question 8:
Write a stored procedure named CountProduct to calculate the number of different products
in an order with OrderID (nvarchar(255)) is input parameter and the NbProducts (int) is the
output parameter of the procedure.
7 of 7 Paper No: 1
For example, when we execute the following code, the result should be 1:
declare @t int
exec CountProduct 'CA-2014-100391', @t output
print @t
Question 9:
Create a trigger InsertProduct which will be activated by an insert statement into the Product
table. The trigger will display the ProductName and the SubCategoryName of the products
which have just been inserted by the insert statement.
Picture 9.1
Question 10:
Insert the following information:
- A category named 'Sports' into table Category
- A subcategory named 'Tennis' and a subcategory named 'Football' into table SubCategory,
both these two subcategories are subcategories of Category 'Sports'