Monday 21 March 2016

Creating Database In SQL


METHOD 1

Create database rose

OR 

Create database roses on primary
(name='roses',
filename='D:\database\roses.mdf',
size=5mb,
maxsize=unlimited,
filegrowth=1mb)
log on
(name='roses_log',
filename='D:\database\roses.ldf',
size=5mb,
maxsize=unlimited,
filegrowth=10%)

METHOD 2

Create database srose on primary
(name='srose',filename='D:\database\srose.mdf',
size=5mb,maxsize=unlimited,filegrowth=1mb)

METHOD 3

Create database rose2 on primary
(name='rose2',
filename='D:\database\rose2.mdf',-----(main extention)
size=5mb,
maxsize=unlimited,
filegrowth=1mb),
filegroup myfilegrp1
(name='rosefilegrp1',
filename='D:\database\rosefilegrp1.ndf',----(for saving the extention)
size=5mb,
maxsize=unlimited,
filegrowth=1mb),
filegroup myfilegrp2
(name='rosefilegrp2',
filename='D:\database\rosefilegrp2.ndf',
size=5mb,
maxsize=unlimited,
filegrowth=1mb)
log on
(name='rose2_log',
filename='D:\database\rose2.ldf',
size=5mb,
maxsize=unlimited,
filegrowth=10%)

ENABLING FILESTREAM

exec sp_configure filestream_access_level,3
reconfigure
create database teksoft
on
primary(name=teksoft,
filename='D:\data\teksoft.mdf'),
filegroup filestreamgroup contains filestream(name=teksoft_data,
filename='D:\data\teksoft_data')
log on(name=log1,filename='D:\data\teksoft\ldf')
go

RENAME A USER-DEFINED DATABASE

sp_renamedb db1, db2
Here,
db1 is the current name of the database
db2 is the new name of the database

DELETE A USER-DEFINED DATABASE

Drop database Employee
Here,
Employee is the name of database



Tuesday 1 March 2016

NIIT SQL LAB@HOME 4


1.Display the Employee ID and the Hire Date of the Employees from the Employee table.The month and the year need to be displayed.

ANSWER: select Employee ID,                                                                            MONTH=DATENAME(mm,HireDate),YEAR =                              DATENAME(yy,Hiredate)
                   from HumanResources.Employee

2.Consider the following SQL query:
   Select ProductID, LineTotal as 'Total' from                  Sales.SalesOrderDetail 
  Group by Cube(Line Total)

ANSWER: The code will give following error message:
                  Msg 8120 ,Level 16 State 1 Line 1                                                     Column 'Sales.SalesOrderDetail.ProductID'
                  is invalid in the select list because it is not contained in
                  either an aggregate function or GROUP BY clause

                The correct code is: 

               select ProductID,SUM(LineTotal) as 'Total'
               from Sales.SalesOrderDetail
               Group by CUBE(ProductID)

3.Write a query to display the full name of a person in a column named Person Name.

ANSWER: select CONCAT(FirstName,MiddleName,LastName)as                    PersonName
                  from Person.Person

4.Display the details of all the orders

ANSWER: select 'Order No.'=SalesOrderID,TotalDue,'Day of                            Order'= DATEPART(dd,OrderDate),'Week                                        day'=DATENAME(dw,OrderDate)
                   from Sales.SalesOrderHeader

5.Display a report containing the sales order ID and the average value of the total amount greater than $5000.

ANSWER: select 'Average Value'=AVG(LineTotal),SalesOrderID
                   from Sales.SalesOrderDetail
                   group by SalesOrderID Having AVG(LineTotal)>5000

6.Display the customer ID, name,and sales person ID for all stores.According to the requirement, only first 15 letters of the customer name should be displayed.

ANSWER: selectCustomerID,Name=LEFT(Name,15),
                   SalesPersonID
                  from Sales.Store

7.Display the total value of all the orders put together.

ANSWER: select 'Total Value of all order'=SUM(TotalDue)
                   from Sales.SalesOrderHeader

8.What will be the output of the following code written to display the total order value for each order?
             Select SalesOrderID,ProductID,sum(LineTotal)
             From Sales.SalesOrderDetail
             Group by SalesOrderID

ANSWER: The code will give following error message:
                    Msg 8120 ,Level 16 State 1 Line 1                                                     Column 'Sales.SalesOrderDetail.ProductID'
                    is invalid in the select list because it is not contained in
                    either an aggregate function or GROUP BY clause

            The correct code is: 

               select SalesOrderID,ProductID,SUM(LineTotal) as 'Total'
               from Sales.SalesOrderDetail
               Group by SalesOrderID,ProductID

9.Display the Order ID of the top five orders based on the total amount due in the year 2001.

ANSWER: select top 5 SalesOrderID,OrderDate
                   from Sales.SalesOrderHeader
                   where DATEPART(yyyy,OrderDate)=2001
                   Order by TotalDue desc

10.Display the maximum,minimum,and the average rate of sales orders.

ANSWER: select 'Maximum'=MAX(TotalDue),                                                 'Minimum'=MIN(TotalDue),'Average'=AVG(TotalDue)
                   from Sales.SalesOrderHeader

11.Display the total amount collected from the orders for each order date.

ANSWER: select OrderDate,SUM(TotalDue)
                   from Sales.SalesOrderHeader
                   group by OrderDate

12.Display the sales order ID and the maximum and minimum values of the order based on the sales order ID. In addition, ensure that the order amount is greater than $5000.

ANSWER: select SalesOrderID, MIN(LineTotal)as Minimum,                            MAX(LineTotal)as Maximum
                   from Sales.SalesOrderDetail
                   where LineTotal>5000
                   group by SalesOrderID
                   
13.Consider the following SQL query containing the ROLLUP operator.
        select ProductID, LineTotal as 'Total'
        from Sales.SalesOrderDetail
        Group by ROLLUP(ProductID)    

ANSWER: The code will give following error message:
                   Msg 8120 ,Level 16 State 1 Line 1 Column                                        'Sales.SalesOrderDetail.ProductID'
                   is invalid in the select list because it is not contained in
                   either an aggregate function or GROUP BY clause

                 The correct code is: 

                 select ProductID,SUM(LineTotal) as 'Total'
                 from Sales.SalesOrderDetail
                 Group by ROLLUP(ProductID)


14.Display a report containing the product ID and the total cost of products for the product ID whose total cost is more than $10,000.

ANSWER: select ProductID,SUM(LineTotal) as Total
                   from Sales.SalesOrderDetail
                   group by ProductID Having SUM(LineTotal)>10000

15.Write a query to retrieve the list price of the products where the products price is between $360.00 and $499.00 and display the price in the following format:

The list price of "Product Name" is "Price" 

ANSWER: select 'list price of'+ Name + 'is'  + CAST(ListPrice as                      varchar(12))as PriceList
                   from Production.Product
                  where ListPrice BETWEEN 360.00 AND 490.00

16.Write a query to display a report that contains the Product ID and its availability status.

ANSWER: select ProductID,IIF(MakeFlag=1,'Available',
                   'NotAvailable')AS AvaibilityOfProduct
                   from Production.Product

17.Display the sum of sales amount earned by each sales person and the sum of sales amount earned by the all the sales persons.

ANSWER: select SalespersonID,SUM(SalesQuota)AS SalesQuota
                   from Sales.SalesPersonQuotaHistory
                   group by Rollup (SalesPersonID)

18.Display the total unit price and the total amount collected after selling the products, 774 and 777.In addition, calculate the total amount collected from these two products.

ANSWER: select ProductID,SUM(UnitPrice)as TotalUnitPrice,                          SUM(LineTotal)as TotalAmt
                   from Sales.SalesOrderDetail
                   where ProductID IN (777,774)
                   group by cube (ProductID)







NIIT  SQL LAB@HOME 3

1.Display all the territories whose name begin with 'N'.

ANSWER: Select *
                   From Sales.SalesTerritory
                   Where Name like 'N%'

2.Display the detail of those stores that have Bike in their name.

ANSWER: Select *
                   From Sales.Store
                   Where Name like '%Bike%'

3.Display the Salesperson Id, TerritoryID and Sales Quota for those sales persons who have been assigned a sales quota.

ANSWER: Select 'SalesPersonId' = SalesPersonID, 'TerritoryId' = Territory ID, 'Sales Quota' = Sales Quota
From Sales.SalesPerson
Where Sales Quota IS NOT NULL

4.Display the top three sales person based on bonus.

ANSWER: Select top 3*
                   From Sales.SalesPerson
                   Order by Bonus desc

5.Display a report that contains the employee ID, login ID, and the title of employees.The report should display the records for 10 employees after excluding the records of the first five employees.

ANSWER: Select Employee ID, Login ID,Title
                   From HumanResources.Employee
                   Order by Emp Id
                   OFFSET 5 Rows
                   Fetch next 10 Rows only 

6.Display the different types of the credit cards used for purchasing products.

ANSWER: Select distinct Cardtype
                   From Sales.CreditCard

7.Each time the Salary slip for an employee is generated, the referral bonus(if present) has to be calculated and printed in the salary slip.

ANSWER: 




8.New Heights is a training institute that provides courses on various nontechnical subjects, such as personality improvement and foreign languages. Xuan, the Database Designer, has made the following relations to represent the data about students, and modules covered in the batches:


  • STUD-ID: Student's id (unique)
  • NAME:Name of students
  • BATCH-NO: Batch number(one student can belong to only one batch)
  • SLOT: Time and day when the batch of the students attends the class
  • MODULE: Module or subject(one batch will do several modules)
  • MARKS: Marks obtained in a module test

Xuan now needs to simplify the above relations by normalizing them.

ANSWER:



9.
         

ANSWER:





10. Consider the following Student Table.




 ANSWER: 








11.Consider the following Purchase Details table.




ANSWER: