Dbms Lab File

36
DATABASE MANAGEMENT SYSTEMS LAB FILE KAMAL MEMON BS (CS)-IV BAHRIA UNIVERSITY KARACHI CAMPUS FALL 2011

Transcript of Dbms Lab File

Page 1: Dbms Lab File

DATABASE MANAGEMENT SYSTEMS

LAB FILE

KAMAL MEMON

BS (CS)-IV

BAHRIA UNIVERSITY

KARACHI CAMPUSFALL 2011

Page 2: Dbms Lab File

INDEX LAB # TASK

#OBJECTIVE DATE SIGN

Page 3: Dbms Lab File

LAB 1The Foundation Statements of T-SQL

OBJECTIVES:1. Get Order id, product id, unit price from Orders details.2. Count total number of employees3. Get the most expensive product amount 4. Get the price of an order (by multiplying unit price by quantity)5. Display all cities that employees belong to but don’t allow repetition6. Find complete name of all employees.7. Display data of all employees those working as Sales Representative.8. Display complete name of customers those lives in New York City.

Task 1: Get Order id, product id, unit price from Orders details.

Query: Select OrderId, UnitPrice, ProductID FROM [Order Details];

Result:

Page 4: Dbms Lab File

Task 2: Count total number of employees.

Query: Select Count(EmployeeID) as [No. of Employees] FROM [Employees];

Result:

Task 3: Get the most expensive product amount.

Query: Select Max(UnitPrice) as [Most Expensive Product] FROM [Products];

Result:

Task 4: Get the price of an order (by multiplying unit price by quantity).

Query: Select UnitPrice * Quantity as Price from [Order Details] where ProductID= 11 and OrderID=10248;

Result:

Task 5: Display all cities that employees belong to but don’t allow repetition.

Query: Select distinct(City) From Employees;

Result:

Page 5: Dbms Lab File

Task 6: Find complete name of all employees.

Query: Select FirstName + ' ' + LastName as FullName From Employees;Result:

Task 7: Display data of all employees those working as Sales Representative.

Query: Select * From Employees where Title='Sales Representative';

Result:

Task 8: Display complete name of customers those lives in London.

Query: Select ContactName From Customers where City='London;

Result:

Page 6: Dbms Lab File

LAB 2Querying Database Tables

The WHERE Clause

OBJECTIVES:By using Northwind Database, formulate the following queries in SQL

1. Display Order Id’s whose quantity is greater than 2. 2. List name of all employees those first name is started with letter ‘A’.3. Display complete names of customers those are either Purchasing Manager or

living in New York city.4. Display names of order whose unit price lies in the range of 10$ to 40$.5. Display names of ORDER where discount is empty6. Display names of all ships ends with alphabet ‘a’

Task 1: Display Order Id’s whose quantity is greater than 2.

Query: Select OrderID from [Order Details] where Quantity > 2 ;

Result:

Page 7: Dbms Lab File

Task 2: List name of all employees those first name is started with letter ‘A’.

Query: Select FirstName + LastName as [Full name] from Employees where Firstname like 'A%';

Result:

Task 3: Display complete names of customers those are either Purchasing Manager or living in New York city.

Query: Select ContactName from Customers where ContactTitle = 'Purchasing Manager' or City = 'New York City';

Result:

Task 4: Display names of order whose unit price lies in the range of 10$ to 40$.

Page 8: Dbms Lab File

Query: Select OrderID from [Order Details] where UnitPrice between 10 and 40;

Result:

Task 5: Display names of ORDER where discount is empty.

Query: Select OrderID from [Order Details] where discount = 0;

Result:

Task 6: Display names of all ships ends with alphabet ‘a’.

Query: Select ShipName from Orders where ShipName like '%a';

Result:

Page 9: Dbms Lab File

LAB 3Built-in Functions (Transact-SQL)

OBJECTIVES:1) Make the average list price of products using distinct

2) Without DISTINCT, use the AVG function to find the average list price of all products in the Product table.

3) Find the total number of employees who work at Adventure Works Cycles.

4) Give an example to show that COUNT(*) can be combined with other aggregate functions in the select list.

5) Calculate the sum of the ListPrice and StandardCost for each color listed in the Product table.

6) Show the results of using the ABS function on three different numbers.

7) Calculate the number of degrees in an angle of PI/2 radians

Page 10: Dbms Lab File

8) Produce four different random numbers that are generated by the RAND function.

9) Get the ACOS of the specified number.

10)Calculate the exponential value of 10

11)Use round function with following arguments ROUND(748.58, -1), ROUND(748.58, -2), ROUND(748.58, -3) and (748.58, 1), ROUND(748.58, 2), ROUND(748.58, 3)

12) Use all the remaining functions in appropriate examples.

SQL QUERIES

Task 1: Make the average list price of products using distinct.

Query: Select AVG(DISTINCT ListPrice) as [Average List Price] from Production.Product

Result:

Task 2: Without DISTINCT, use the AVG function to find the average list price of all products in the Product table.

Query: Select AVG(ListPrice) as [Average List Price] from Production.Product

Result:

Task 3: Find the total number of employees who work at Adventure Works Cycles.

Query: Select COUNT(EmployeeID)as [Total No. Of Employees] from HumanResources.Employee

Page 11: Dbms Lab File

Result:

Task 4: Give an example to show that COUNT(*) can be combined with other aggregate functions in the select list.

Query: Select Count(*) as [Total Columns] , AVG(ListPrice) as [Maximun List Price] from Production.Product

Result:

Task 5: Calculate the sum of the ListPrice and StandardCost for each color listed in the Product table.

Query: Select Color, Sum(ListPrice) as [Total List Price], Sum(StandardCost) as [Total Standard Cost] fromProduction.ProductGroup by ColorOrder by Color DESC

Result:

Task 6: Show the results of using the ABS function on three different numbers.

Page 12: Dbms Lab File

Query: Select Abs(-1234)Select Abs(1.234)Select Abs(-12.00034)

Result:

Task 7: Calculate the number of degrees in an angle of PI/2 radians.

Query: Select DEGREES(PI()/2)

Result:

Task 8: Produce four different random numbers that are generated by the RAND function.

Query: declare @countw int;SET @countw = 1;while (@countw <5)BEGINSelect RAND();SET @countw = @countw + 1;END

Result:

Page 13: Dbms Lab File

Task 9: Get the ACOS of the specified number.

Query: Select ACOS(0.00156)

Result:

Task 10: Calculate the exponential value of 10.

Query: Select EXP(10)

Result:

Task 11: Use round function with following arguments ROUND(748.58, -1), ROUND(748.58, -2), ROUND(748.58, -3) and (748.58, 1), ROUND(748.58, 2), ROUND(748.58, 3).

Query: Select ROUND(748.58, -1)Select ROUND(748.58, -2)-- Select ROUND(748.58, -3)Select ROUND (748.58, 1)

Page 14: Dbms Lab File

Select ROUND(748.58, 2)Select ROUND(748.58, 3)

Result:

Task 12: Use all the remaining functions in appropriate examples.

Query: Select ABS(-1) as [ABS],SIGN(-222) as [SIGN], CEILING(1.002) as [CEILING],FLOOR($75.66) as [FLOOR],SQRT(16) as [SQRT],SQUARE(4) as [SQUARE],SIN(10) as [SIN],COS(45) as [COS],TAN(60) as [TAN],COT(30) as [COT]Select ASIN(-1) as [ASIN],ATAN(-45) as [ATAN],ATN2(2.012,3.88) as [ATN2],LOG(10) as [LOG],LOG10(10) as [LOG10],

Page 15: Dbms Lab File

RADIANS(60) as [RADIANS],POWER(2,2) AS [POWER]

Result:

LAB 4Date and Time (Transact-SQL)

OBJECTIVES:1) The following example prints a listing of a time frame for orders in the

AdventureWorks database. This time frame represents the existing order date plus 21 days.

2) The following example determines the difference in days between the current date and the order date for products in the AdventureWorks database.

3) Using DATENAME() function get names of column startdate from Production.WorkOrder table AdventureWorks database

4) Run the following Queries and observe outputSELECT DATENAME(year,'1995-10-30 12:15:32.123');

Page 16: Dbms Lab File

SELECT DATENAME(yy,'1995-10-30 12:15:32.123');SELECT DATENAME(yyyy,'1995-10-30 12:15:32.123');

SELECT DATENAME(quarter,'1995-10-30 12:15:32.123');SELECT DATENAME(qq,'1995-10-30 12:15:32.123');SELECT DATENAME(q,'1995-10-30 12:15:32.123');

SELECT DATENAME(month,'1995-10-30 12:15:32.123');SELECT DATENAME(mm,'1995-10-30 12:15:32.123');SELECT DATENAME(m,'1995-10-30 12:15:32.123');

SELECT DATENAME(dayofyear,'1995-10-30 12:15:32.123');SELECT DATENAME(dy,'1995-10-30 12:15:32.123');SELECT DATENAME(y,'1995-10-30 12:15:32.123');

SELECT DATENAME(day,'1995-10-30 12:15:32.123');SELECT DATENAME(dd,'1995-10-30 12:15:32.123');SELECT DATENAME(d,'1995-10-30 12:15:32.123');

SELECT DATENAME(week,'1995-10-30 12:15:32.123');SELECT DATENAME(wk,'1995-10-30 12:15:32.123');SELECT DATENAME(ww,'1995-10-30 12:15:32.123');

SELECT DATENAME(weekday,'1995-10-30 12:15:32.123');SELECT DATENAME(dw,'1995-10-30 12:15:32.123');

SELECT DATENAME(hour,'1995-10-30 12:15:32.123');SELECT DATENAME(hour,'1995-10-30 12:15:32.123');SELECT DATENAME(hh,'10/30/1995 12:15:32.123 PM');SELECT DATENAME(hh,'10/30/1995 12:15:32.123 PM');

SELECT DATENAME(minute,'1995-10-30 12:15:32.123');SELECT DATENAME(mi,'1995-10-30 12:15:32.123');SELECT DATENAME(n,'1995-10-30 12:15:32.123');

SELECT DATENAME(second,'1995-10-30 12:15:32.123');SELECT DATENAME(ss,'1995-10-30 12:15:32.123');SELECT DATENAME(s,'1995-10-30 12:15:32.123');

SELECT DATENAME(millisecond,'1995-10-30 12:15:32.123');SELECT DATENAME(ms,'1995-10-30 12:15:32.123');

5) Use getdate() function to get system date then use datepart() function to extract month, year and date from it

Page 17: Dbms Lab File

6) Use getdate() function to get system date then use day(), month() and year() functions to extract month, year and date from it

SQL QUERIES

Task 1: The following example prints a listing of a time frame for orders in the AdventureWorks database. This time frame represents the existing order date plus 21 days.

Query: Select OrderDate, Dateadd(day,21,OrderDate) as TimeFrame from Sales.SalesOrderHeader

Result:

Task 2: The following example determines the difference in days between the current date and the order date for products in the AdventureWorks database.

Query: Select OrderDate, Datediff(day,OrderDate,Getdate()) as [Difference] from Sales.SalesOrderHeader

Result:

Page 18: Dbms Lab File

Task 3: Using DATENAME() function get names of column startdate from Production.WorkOrder table AdventureWorks database

Query: Select StartDate, DateName(weekday,StartDate) as DayName from Production.WorkOrder

Result:

Task 4: Run the following Queries and observe output

1)SELECT DATENAME(year,'1995-10-30 12:15:32.123');SELECT DATENAME(yy,'1995-10-30 12:15:32.123');SELECT DATENAME(yyyy,'1995-10-30 12:15:32.123');

2)SELECT DATENAME(quarter,'1995-10-30 12:15:32.123');SELECT DATENAME(qq,'1995-10-30 12:15:32.123');

Page 19: Dbms Lab File

SELECT DATENAME(q,'1995-10-30 12:15:32.123');

3)SELECT DATENAME(month,'1995-10-30 12:15:32.123');SELECT DATENAME(mm,'1995-10-30 12:15:32.123');SELECT DATENAME(m,'1995-10-30 12:15:32.123');

4)SELECT DATENAME(dayofyear,'1995-10-30 12:15:32.123');SELECT DATENAME(dy,'1995-10-30 12:15:32.123');SELECT DATENAME(y,'1995-10-30 12:15:32.123');

5)SELECT DATENAME(day,'1995-10-30 12:15:32.123');SELECT DATENAME(dd,'1995-10-30 12:15:32.123');SELECT DATENAME(d,'1995-10-30 12:15:32.123');

6)SELECT DATENAME(week,'1995-10-30 12:15:32.123');SELECT DATENAME(wk,'1995-10-30 12:15:32.123');SELECT DATENAME(ww,'1995-10-30 12:15:32.123');

7)SELECT DATENAME(weekday,'1995-10-30 12:15:32.123');SELECT DATENAME(dw,'1995-10-30 12:15:32.123');

8)SELECT DATENAME(hour,'1995-10-30 12:15:32.123');SELECT DATENAME(hour,'1995-10-30 12:15:32.123');SELECT DATENAME(hh,'10/30/1995 12:15:32.123 PM');SELECT DATENAME(hh,'10/30/1995 12:15:32.123 PM');

9)SELECT DATENAME(minute,'1995-10-30 12:15:32.123');SELECT DATENAME(mi,'1995-10-30 12:15:32.123');SELECT DATENAME(n,'1995-10-30 12:15:32.123');

10)SELECT DATENAME(second,'1995-10-30 12:15:32.123');SELECT DATENAME(ss,'1995-10-30 12:15:32.123');SELECT DATENAME(s,'1995-10-30 12:15:32.123');

11)SELECT DATENAME(millisecond,'1995-10-30 12:15:32.123');SELECT DATENAME(ms,'1995-10-30 12:15:32.123');

Page 20: Dbms Lab File

Output:

1) 19952) 43) October4) 3035) 306) 447) Monday8) 129) 1510)3211)123

Task 5: Use getdate() function to get system date then use datepart() function to extract month, year and date from it.

Query: Select Datepart(month,Getdate()) as [Month], Datepart(year,Getdate()) as [Year] ,Datepart(day,Getdate()) as [Day]

Result:

Task 6: Use getdate() function to get system date then use day(), month() and year() functions to extract month, year and date from it.

Query: Select Month(Getdate()) as [Month], Year(Getdate()) as [Year], Day(Getdate()) as [Day]

Result:

Page 21: Dbms Lab File

LAB 5Joining Tables

OBJECTIVES:

1) Get Shipper’s Company name for an order

2) Display Product’s name and there Supplier’s name

3) Display Which Customers were served by which Employees

4) Write a Query to display the following table

Customer Name Order Id Order Date

5) Write a query to display the following table for any order

Customer Name Product Name Unit Price Quantity Discount

6) Make a list of all the titles and there authors.

Page 22: Dbms Lab File

7) Name the store that offer initial customer discount.

8) Name all the employees working in New York.

9) Name the Titles that have been sold the most.

10) List all the employees working for “New Moon Books”.

11) Who is the production manager of “Lucerne Publishing”?

12) Make the list of all Authors sales.

13) Name the publisher that has authors whose name ends with “e”.

14) Which publisher has got most titles in store in Seattle?

15) List all the authors available in Barnum’s store.

SQL QUERIES

Task 1: Get Shipper’s Company name for an order

Query: Select CompanyName from Shippers as sinner join Orders as o on o.Shipvia=s.shipperid where orderid=10267

Result:

Task 2: Display Product’s name and there Supplier’s name

Query: Select Productname , CompanyName from Products as pinner join Suppliers as s on s.supplierid=p.supplierid

Result:

Page 23: Dbms Lab File

Task 3: Display Which Customers were served by which Employees

Query: Select ContactName, e.firstname + e.lastname as EmployeeName from Customers as cinner join orders as o on o.customerid = c.customerid inner join Employees as e on o.employeeid=e.employeeid

Result:

Task 4: Write a Query to display the following table

Customer Name Order Id Order Date

Query: Select contactname as [Customer Name] , o.OrderId ,o.OrderDate from Customers as cinner join Orders as o on o.customerid=c.customerid

Result:

Task 5: Write a query to display the following table for any order

Page 24: Dbms Lab File

Customer Name Product Name Unit Price Quantity Discount

Query: Select ContactName , p.productname, p.unitprice,p.quantityperunit,od.discount from Customers as cinner join orders as o on o.customerid = c.customeridinner join [Order Details] as od on od.orderid=o.orderidinner join products as p on p.productid= od.productid

Result:

Task 6: Make a list of all the titles and their authors.

Query: Select title as Titles , au_fname + au_lname as Authors from titles as tinner join titleauthor as ta on t.title_id =ta.title_idinner join authors as a on a.au_id=ta.au_id

Result:

Task 7: Name the store that offer initial customer discount.

Query: Select stor_name as Stores from Stores as sinner join discounts as d on d.stor_id=s.stor_idwhere Discounttype = 'intial customer '

Page 25: Dbms Lab File

Result:

Task 8: Name all the employees working in New York.

Query: Select fname + ' ' + minit + ' ' + lname as Names from Employee as einner join Publishers as p on p.pub_id=e.pub_id where p.city='New York'

Result:

Task 9: Name the Titles that have been sold the most.

Query: Select title from titles as t inner join Sales as s on s.title_id=t.title_idwhere s.qty = (Select Max(qty) from Sales)

Result:

Task 10: List all the employees working for “New Moon Books”.

Query: Select fname + ' ' + minit + ' ' + lname as Names from Employee as e inner join Publishers as p on p.pub_id=e.pub_idwhere p.pub_name='New Moon Books'

Result:

Page 26: Dbms Lab File

Task 11: Who is the production manager of “Lucerne Publishing”?

Query: Select fname + ' ' + minit + ' ' + lname as [Name] from Employee as einner join Publishers as pon p.pub_id=e.pub_idinner join Jobs as j on j.job_id=e.job_idwhere p.pub_name='Lucerne Publishing' and j.job_desc = 'productions manager '

Result:

Task 12: Make the list of all Authors sales.

Query: Select DISTINCT au_fname + ' ' + au_lname as [Names] , Sum(S.qty) as [Quantity] from Authors as A Inner Join titleauthor as TA On A.au_id = TA.au_idInner Join titles as T On T.title_id = TA.title_id inner join Sales as S on T.title_id=S.title_idGroup by au_fname + ' ' + au_lnameOrder by Names

Result:

Page 27: Dbms Lab File

Task 13: Name the publisher that has authors whose name ends with “e”.

Query: Select pub_name as [Publisher Name], a.au_fname + ' ' + a.au_lname as [Author Names] from Publishers as p inner join titles as t on t.pub_id=p.pub_idinner join titleauthor as ta on t.title_id =ta.title_id inner join authors as a on a.au_id=ta.au_id where a.au_fname + ' ' + a.au_lname like '%e'

Result:

Task 14: Which publisher has got most titles in store in Seattle?

Query: Select pub_name as [Publisher name] from publishers as pinner join Titles as t on t.pub_id=p.pub_idinner join Sales as s on s.title_id=t.title_idinner join Stores as st on st.stor_id=s.stor_idwhere s.qty = ( Select Max(s.qty) from Sales as sinner join Stores as st on st.stor_id=s.stor_id where city='Seattle')

Result:

Page 28: Dbms Lab File

Task 15: List all the authors available in Barnum’s store.

Query: Select DISTINCT au_fname + ' ' + au_lname as [Names] from authors as aInner Join titleauthor as TA On A.au_id = TA.au_idInner Join titles as T On T.title_id = TA.title_id inner join Sales as S on T.title_id=S.title_idinner join Stores as st on st.stor_id=s.stor_idwhere stor_name='Barnum''s'

Result:

LAB 6Manipulate the data of tables through DML Queries

OBJECTIVES:

Page 29: Dbms Lab File

1) Insert the following values to table stores

Store Id Store Name Store Address city state zip

TEST1 Test 1 Store 1234 Anywhere Street Here NY 00319

TEST2 Test 2 Store 567 Somewhere Street There NY 00313

TEST3 Test 3 Store 1234 Anywhere Street Here NY 00319

2) Add a new publisher whose name is Scotty and is in London UK.

3) Update address of “Eric the Read Books” to “700 Catamaugus Ave”.

4) Increment all discounts by 10%.

5) Delete employee whose id is “VPA30890F”.

6) Delete all Authors whose first name starts with M.

SQL QUERIES

Task 1: Insert the following values to table stores

Store Id Store Name Store Address city state zip

TEST1 Test 1 Store 1234 Anywhere Street Here NY 00319

TEST2 Test 2 Store 567 Somewhere Street There NY 00313

Page 30: Dbms Lab File

TEST3 Test 3 Store 1234 Anywhere Street Here NY 00319

Query:

Insert into stores (Stor_Id,Stor_Name,Stor_Address,city,state,zip)Values ('TT1','Test Store','1234 Anywhere Street','Here','NY',00319)

Insert into stores (Stor_Id,Stor_Name,Stor_Address,city,state,zip)Values ('TT2','Test Store','567 Somewhere Street','There','NY',00313)

Insert into stores (Stor_Id,Stor_Name,Stor_Address,city,state,zip)Values ('TT3','Test 3 Store','1234 Anywhere Street','Here','NY',00319)

Result:

Task 2: Add a new publisher whose name is Scotty and is in London UK.

Query:

Insert into Publishers values (9909,'Scotty','London','EG','UK')

Result:

Page 31: Dbms Lab File

Task 3: Update address of “Eric the Read Books” to “700 Catamaugus Ave”.

Query:

Update stores set stor_address ='700 Catamangus Ave'where stor_name='Eric The Read Books'

Result:

Task 4: Increment all discounts by 10%.

Query:

Update discounts set discount = discount + discount* .1

Result:

Task 5: Delete employee whose id is “VPA30890F”.

Query:

Delete from Employee where emp_id='VPA30890F'

Page 32: Dbms Lab File

Task 6: Delete all Authors whose first name starts with M.

Query:

Delete from Authors where au_fname like 'M%'