0% found this document useful (0 votes)
81 views7 pages

For The Submission of Your Work

The document provides instructions for submitting assignments for a database course. Students should create folders named their roll number and name and include all files for each question. Questions involve writing SQL queries to retrieve and manipulate data from tables based on the given ERD diagrams. The questions cover topics like creating tables, joining tables, aggregating results, and writing stored procedures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views7 pages

For The Submission of Your Work

The document provides instructions for submitting assignments for a database course. Students should create folders named their roll number and name and include all files for each question. Questions involve writing SQL queries to retrieve and manipulate data from tables based on the given ERD diagrams. The questions cover topics like creating tables, joining tables, aggregating results, and writing stored procedures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1 of 7 Paper No: 1

For the submission of your work:


- Create a folder named RollNo_Name_DBI202_PaperNo, e.g.
se01245_LongNT_DBI202_01. All file created will be located in the above folder.
- For each question, you are required to write a database script. Create a file with the name
corresponding to the index of the question. For example, for question 1, we will create a file
named Q1.sql and create a file Q2.sql for question 2. These files contain only the required
workflow commands that do not include other commands including the database context
switch (not including use [database name]).
- On completion, import your work by browsing to the above folder.

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

select od.ProductID, p.ProductName, Quantity from Product p, OrderDetails od where p.ID =


od.ProductID and Quantity >= (select max(Quantity) from OrderDetails)

Picture 5.1
Question 6:
6 of 7 Paper No: 1

Write a query to display CustomerID, CustomerName and the number of orders


(NumberOfOrders) of customers who have the highest number of orders.
select o.CustomerID,CustomerName,NumberOfOrder
from Customer c, (select distinct CustomerID,count(*) as NumberOfOrder
from Orders group by CustomerID) o
where c.ID = o.CustomerID and
NumberOfOrder = (select max(NumberOfOrder) from (select count(*) as NumberOfOrder
from Orders group by CustomerID) o)
group by o.CustomerID,CustomerName,NumberOfOrder

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.

For example, when we run:


insert into Product(ProductName, UnitPrice, SubCategoryID)
values ('Craft paper', 0.5, 3)
The result should be as follows:

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'

You might also like